Skip to content

Commit

Permalink
Merge pull request #38 from idoknow/feat-preview-image
Browse files Browse the repository at this point in the history
feat: 图片压缩预览
  • Loading branch information
RockChinQ authored Sep 17, 2024
2 parents e9ff54e + 73e3889 commit ab75951
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
7 changes: 6 additions & 1 deletion backend/controller/postapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ func (pr *PostRouter) DownloadImage(c *gin.Context) {
}

key := c.Param("key")
_, isPreview := c.GetQuery("preview")

buf := bytes.NewBuffer(nil)

err = pr.PostService.DownloadImage(key, buf)
if isPreview {
err = pr.PostService.PreviewImage(key, buf)
} else {
err = pr.PostService.DownloadImage(key, buf)
}

if err != nil {
pr.Fail(c, 1, err.Error())
Expand Down
1 change: 1 addition & 0 deletions backend/oss/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "io"

type BaseOSSProvider interface {
UploadFromIO(io.Reader, string) (string, error)
UploadFromIOWithKey(io.Reader, string, string) (string, error)
DownloadToIO(string, io.Writer) error
CheckObjectExist(string) (bool, error)
}
12 changes: 9 additions & 3 deletions backend/oss/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ func NewLocalStorage() *LocalStorage {
func (l *LocalStorage) UploadFromIO(ioReader io.Reader, suffix string) (string, error) {
objectName := generateObjectName()

key, err := l.UploadFromIOWithKey(ioReader, objectName, suffix)

return key, err
}

func (l *LocalStorage) UploadFromIOWithKey(ioReader io.Reader, key string, suffix string) (string, error) {
if suffix != "" {
objectName += "." + suffix
key += "." + suffix
}

file, err := os.Create(filepath.Join(l.dir, objectName))
file, err := os.Create(filepath.Join(l.dir, key))
if err != nil {
return "", err
}
defer file.Close()

_, err = io.Copy(file, ioReader)
return objectName, err
return key, err
}

func (l *LocalStorage) DownloadToIO(objectName string, ioWriter io.Writer) error {
Expand Down
18 changes: 14 additions & 4 deletions backend/oss/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ func generateObjectName() string {

// 从io.Reader上传文件
func (m *MinioClient) UploadFromIO(ioReader io.Reader, suffix string) (string, error) {

objectName := generateObjectName()

key, err := m.UploadFromIOWithKey(ioReader, objectName, suffix)

return key, err
}

func (m *MinioClient) UploadFromIOWithKey(ioReader io.Reader, key string, suffix string) (string, error) {
if suffix != "" {
objectName += "." + suffix
key += "." + suffix
}

_, err := m.client.PutObject(context.Background(), m.bucket, key, ioReader, -1, minio.PutObjectOptions{})

if err != nil {
return "", err
}

_, err := m.client.PutObject(context.Background(), m.bucket, objectName, ioReader, -1, minio.PutObjectOptions{})
return objectName, err
return key, nil
}

// 下载文件到io.Writer
Expand Down
44 changes: 44 additions & 0 deletions backend/service/post.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package service

import (
"bytes"
"errors"
"io"
"strings"

"github.com/RockChinQ/Campux/backend/database"
"github.com/RockChinQ/Campux/backend/mq"
Expand Down Expand Up @@ -40,6 +42,48 @@ func (ps *PostService) DownloadImage(key string, ioWriter io.Writer) error {
return ps.OSS.DownloadToIO(key, ioWriter)
}

func (ps *PostService) PreviewImage(key string, ioWriter io.Writer) error {
thumbnailKey := strings.Split(key, ".")[0] + "_thumbnail"
thumbnailKeySuffix := thumbnailKey + ".jpg"

exist, err := ps.OSS.CheckObjectExist(thumbnailKeySuffix)

if err != nil {
return err
}

if exist {
// download the thumbnail directly
return ps.OSS.DownloadToIO(thumbnailKeySuffix, ioWriter)
}

// compress the image and then upload.

buf := new(bytes.Buffer)
err = ps.OSS.DownloadToIO(key, buf)

if err != nil {
return err
}

// compress the image
reader, err := util.CompressImage(buf.Bytes(), 10)

if err != nil {
ioWriter.Write(buf.Bytes())
return nil
}

// CompressImage() should return a JPEG format image
_, err = ps.OSS.UploadFromIOWithKey(reader, thumbnailKey, "jpg")

if err != nil {
return err
}

return ps.OSS.DownloadToIO(thumbnailKeySuffix, ioWriter)
}

func (ps *PostService) PostNew(uuid string, uin int64, text string, images []string, anon bool) (int, error) {

// 检查这个用户是否有未过审的帖子
Expand Down
26 changes: 26 additions & 0 deletions backend/util/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package util

import (
"bytes"
"image"
_ "image/gif" // register gif format
"image/jpeg"
_ "image/png" // register png format
"io"
)

func CompressImage(input []byte, quality int) (io.Reader, error) {
img, _, err := image.Decode(bytes.NewReader(input))
if err != nil {
return nil, err
}

var buf bytes.Buffer

err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
if err != nil {
return nil, err
}

return bytes.NewReader(buf.Bytes()), nil
}
4 changes: 3 additions & 1 deletion frontend/src/pages/post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ export default {
// 将images中的baseurl去掉
this.post.images = this.post.images.map(image => {
return image.replace(this.$store.state.base_url + "/v1/post/download-image/", '')
.replace("?preview=1", "")
})
this.$axios.post('/v1/post/post-new', this.post)
Expand Down Expand Up @@ -286,7 +288,7 @@ export default {
})
.then(res => {
if (res.data.code === 0) {
let url = this.$store.state.base_url + '/v1/post/download-image/' + res.data.data.key
let url = this.$store.state.base_url + '/v1/post/download-image/' + res.data.data.key + "?preview=1"
console.log(url)
this.post.images.push(url)
this.fetchLatestImageToImageBlobs(url)
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/world.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default {
p[i].created_at = date.toLocaleString()
p[i].status = this.$store.state.statusMap[p[i].status]
for (let j = 0; j < p[i].images.length; j++) {
p[i].images[j] = this.$store.state.base_url + "/v1/post/download-image/" + p[i].images[j]
p[i].images[j] = this.$store.state.base_url + "/v1/post/download-image/" + p[i].images[j] + "?preview=1"
}
}
console.log(p)
Expand Down Expand Up @@ -250,7 +250,7 @@ export default {
p[i].created_at = date.toLocaleString()
p[i].status = this.$store.state.statusMap[p[i].status]
for (let j = 0; j < p[i].images.length; j++) {
p[i].images[j] = this.$store.state.base_url + "/v1/post/download-image/" + p[i].images[j]
p[i].images[j] = this.$store.state.base_url + "/v1/post/download-image/" + p[i].images[j] + "?preview=1"
}
}
console.log(p)
Expand Down

0 comments on commit ab75951

Please sign in to comment.