Skip to content

Commit d5a0551

Browse files
committed
feat: 响应标头新增 Last-Modified
1 parent 5af35e0 commit d5a0551

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

internal/controllers/objectController/get.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func GetFile(c *gin.Context) {
6363
objectKey := objectService.CleanLocation(data.ObjectKey)
6464

6565
if data.Thumbnail {
66-
thumbnail, size, err := objectService.GetThumbnail(data.Bucket, objectKey)
66+
thumbnail, info, err := objectService.GetThumbnail(data.Bucket, objectKey)
6767
if errors.Is(err, oss.ErrResourceNotExists) || errors.Is(err, image.ErrFormat) {
6868
c.AbortWithStatus(http.StatusNotFound)
6969
return
@@ -76,7 +76,10 @@ func GetFile(c *gin.Context) {
7676
_ = thumbnail.Close()
7777
}()
7878

79-
c.DataFromReader(http.StatusOK, size, "image/jpeg", thumbnail, nil)
79+
headers := map[string]string{
80+
"Last-Modified": info.LastModified.Format(http.TimeFormat),
81+
}
82+
c.DataFromReader(http.StatusOK, info.ContentLength, info.ContentType, thumbnail, headers)
8083
} else {
8184
bucket, err := oss.Buckets.GetBucket(data.Bucket)
8285
if err != nil {
@@ -97,7 +100,10 @@ func GetFile(c *gin.Context) {
97100
_ = obj.Close()
98101
}()
99102

100-
c.DataFromReader(http.StatusOK, content.ContentLength, content.ContentType, obj, nil)
103+
headers := map[string]string{
104+
"Last-Modified": content.LastModified.Format(http.TimeFormat),
105+
}
106+
c.DataFromReader(http.StatusOK, content.ContentLength, content.ContentType, obj, headers)
101107
}
102108
}
103109

internal/services/objectService/service.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strings"
1414
"sync"
15+
"time"
1516

1617
"cube-go/pkg/config"
1718
"cube-go/pkg/oss"
@@ -74,15 +75,20 @@ func ConvertToWebP(reader io.Reader) (*bytes.Reader, error) {
7475
}
7576

7677
// GetThumbnail 获取缩略图
77-
func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error) {
78+
func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, *oss.GetObjectInfo, error) {
7879
filename := bucket + "-" + objectKey
7980
cachePath := filepath.Join(config.Config.GetString("oss.thumbnailDir"), filename+".jpg")
8081

8182
// 尝试从缓存中读取
8283
if stat, err := os.Stat(cachePath); err == nil {
8384
file, err := os.Open(cachePath)
8485
if err == nil {
85-
return file, stat.Size(), nil
86+
info := oss.GetObjectInfo{
87+
ContentType: "image/jpeg",
88+
ContentLength: stat.Size(),
89+
LastModified: stat.ModTime(),
90+
}
91+
return file, &info, nil
8692
}
8793
}
8894

@@ -93,21 +99,26 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
9399
if stat, err := os.Stat(cachePath); err == nil {
94100
file, err := os.Open(cachePath)
95101
if err == nil {
96-
return file, stat.Size(), nil
102+
info := oss.GetObjectInfo{
103+
ContentType: "image/jpeg",
104+
ContentLength: stat.Size(),
105+
LastModified: stat.ModTime(),
106+
}
107+
return file, &info, nil
97108
}
98109
}
99-
return nil, 0, oss.ErrResourceNotExists
110+
return nil, nil, oss.ErrResourceNotExists
100111
}
101112
defer done()
102113

103114
// 从 OSS 获取源文件
104115
provider, err := oss.Buckets.GetBucket(bucket)
105116
if err != nil {
106-
return nil, 0, err
117+
return nil, nil, err
107118
}
108119
object, _, err := provider.GetObject(objectKey)
109120
if err != nil {
110-
return nil, 0, err
121+
return nil, nil, err
111122
}
112123
defer func() {
113124
_ = object.Close()
@@ -116,7 +127,7 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
116127
// 解码图片
117128
img, err := imaging.Decode(object, imaging.AutoOrientation(true))
118129
if err != nil {
119-
return nil, 0, err
130+
return nil, nil, err
120131
}
121132
img = removeAlpha(img)
122133
finalImg := imaging.Fit(img, maxLongEdge, maxLongEdge, imaging.CatmullRom)
@@ -129,15 +140,20 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
129140
Quality: config.Config.GetInt("oss.thumbnailQuality"),
130141
})
131142
if err != nil {
132-
return nil, 0, err
143+
return nil, nil, err
133144
}
134145

135146
// 写入缓存文件
136147
if err := os.MkdirAll(filepath.Dir(cachePath), os.ModePerm); err == nil {
137148
_ = os.WriteFile(cachePath, buf.Bytes(), 0644)
138149
}
139150

140-
return io.NopCloser(bytes.NewReader(buf.Bytes())), int64(buf.Len()), nil
151+
info := oss.GetObjectInfo{
152+
ContentType: "image/jpeg",
153+
ContentLength: int64(buf.Len()),
154+
LastModified: time.Now(),
155+
}
156+
return io.NopCloser(bytes.NewReader(buf.Bytes())), &info, nil
141157
}
142158

143159
func waitForPath(p string) (first bool, done func()) {

pkg/oss/local.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (p *LocalStorageProvider) GetObject(objectKey string) (io.ReadCloser, *GetO
111111
info := &GetObjectInfo{
112112
ContentLength: stat.Size(),
113113
ContentType: getMimeType(relativePath),
114+
LastModified: stat.ModTime(),
114115
}
115116

116117
// 读取文件

pkg/oss/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package oss
33
import (
44
"errors"
55
"io"
6+
"time"
67
)
78

89
// StorageProvider 定义存储服务接口
@@ -26,6 +27,7 @@ type FileListElement struct {
2627
type GetObjectInfo struct {
2728
ContentType string
2829
ContentLength int64
30+
LastModified time.Time
2931
}
3032

3133
var (

pkg/oss/s3.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (p *S3StorageProvider) GetObject(objectKey string) (io.ReadCloser, *GetObje
9898
info := &GetObjectInfo{
9999
ContentLength: aws.ToInt64(result.ContentLength),
100100
ContentType: aws.ToString(result.ContentType),
101+
LastModified: aws.ToTime(result.LastModified),
101102
}
102103
return result.Body, info, nil
103104
}

0 commit comments

Comments
 (0)