@@ -12,6 +12,7 @@ import (
12
12
"regexp"
13
13
"strings"
14
14
"sync"
15
+ "time"
15
16
16
17
"cube-go/pkg/config"
17
18
"cube-go/pkg/oss"
@@ -74,15 +75,20 @@ func ConvertToWebP(reader io.Reader) (*bytes.Reader, error) {
74
75
}
75
76
76
77
// GetThumbnail 获取缩略图
77
- func GetThumbnail (bucket string , objectKey string ) (io.ReadCloser , int64 , error ) {
78
+ func GetThumbnail (bucket string , objectKey string ) (io.ReadCloser , * oss. GetObjectInfo , error ) {
78
79
filename := bucket + "-" + objectKey
79
80
cachePath := filepath .Join (config .Config .GetString ("oss.thumbnailDir" ), filename + ".jpg" )
80
81
81
82
// 尝试从缓存中读取
82
83
if stat , err := os .Stat (cachePath ); err == nil {
83
84
file , err := os .Open (cachePath )
84
85
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
86
92
}
87
93
}
88
94
@@ -93,21 +99,26 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
93
99
if stat , err := os .Stat (cachePath ); err == nil {
94
100
file , err := os .Open (cachePath )
95
101
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
97
108
}
98
109
}
99
- return nil , 0 , oss .ErrResourceNotExists
110
+ return nil , nil , oss .ErrResourceNotExists
100
111
}
101
112
defer done ()
102
113
103
114
// 从 OSS 获取源文件
104
115
provider , err := oss .Buckets .GetBucket (bucket )
105
116
if err != nil {
106
- return nil , 0 , err
117
+ return nil , nil , err
107
118
}
108
119
object , _ , err := provider .GetObject (objectKey )
109
120
if err != nil {
110
- return nil , 0 , err
121
+ return nil , nil , err
111
122
}
112
123
defer func () {
113
124
_ = object .Close ()
@@ -116,7 +127,7 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
116
127
// 解码图片
117
128
img , err := imaging .Decode (object , imaging .AutoOrientation (true ))
118
129
if err != nil {
119
- return nil , 0 , err
130
+ return nil , nil , err
120
131
}
121
132
img = removeAlpha (img )
122
133
finalImg := imaging .Fit (img , maxLongEdge , maxLongEdge , imaging .CatmullRom )
@@ -129,15 +140,20 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
129
140
Quality : config .Config .GetInt ("oss.thumbnailQuality" ),
130
141
})
131
142
if err != nil {
132
- return nil , 0 , err
143
+ return nil , nil , err
133
144
}
134
145
135
146
// 写入缓存文件
136
147
if err := os .MkdirAll (filepath .Dir (cachePath ), os .ModePerm ); err == nil {
137
148
_ = os .WriteFile (cachePath , buf .Bytes (), 0644 )
138
149
}
139
150
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
141
157
}
142
158
143
159
func waitForPath (p string ) (first bool , done func ()) {
0 commit comments