Skip to content

Commit e2e9940

Browse files
committed
chore: 规范附件服务
1 parent f7dd19d commit e2e9940

File tree

9 files changed

+89
-20
lines changed

9 files changed

+89
-20
lines changed

app/admin/resources/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (p *Image) Fields(ctx *quark.Context) []interface{} {
4444
return []interface{}{
4545
field.ID("id", "ID"),
4646
field.Text("path", "显示", func(row map[string]interface{}) interface{} {
47-
return "<img src='" + service.NewAttachmentService().GetImagePath(row["id"]) + "' width=50 height=50 />"
47+
return "<img src='" + service.NewAttachmentService().GetImageUrl(row["id"]) + "' width=50 height=50 />"
4848
}),
4949
field.Text("name", "名称").SetEllipsis(true),
5050
field.Text("size", "大小").SetSorter(true),

app/admin/uploads/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p *File) AfterHandle(ctx *quark.Context, result *quark.FileInfo) error {
8989

9090
// 重写url
9191
if driver == quark.LocalStorage {
92-
result.Url = service.NewAttachmentService().GetFilePath(result.Url)
92+
result.Url = service.NewAttachmentService().GetFileUrl(result.Url)
9393
}
9494

9595
adminInfo, err := service.NewAuthService(ctx).GetAdmin()

app/admin/uploads/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (p *Image) Crop(ctx *quark.Context) error {
250250

251251
// 重写url
252252
if driver == quark.LocalStorage {
253-
result.Url = service.NewAttachmentService().GetImagePath(result.Url)
253+
result.Url = service.NewAttachmentService().GetImageUrl(result.Url)
254254
}
255255

256256
extra := ""
@@ -322,7 +322,7 @@ func (p *Image) AfterHandle(ctx *quark.Context, result *quark.FileInfo) error {
322322

323323
// 重写url
324324
if driver == quark.LocalStorage {
325-
result.Url = service.NewAttachmentService().GetImagePath(result.Url)
325+
result.Url = service.NewAttachmentService().GetImageUrl(result.Url)
326326
}
327327

328328
adminInfo, err := service.NewAuthService(ctx).GetAdmin()

app/tool/upload/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (p *File) AfterHandle(ctx *quark.Context, result *quark.FileInfo) error {
8888

8989
// 重写url
9090
if driver == quark.LocalStorage {
91-
result.Url = service.NewAttachmentService().GetFilePath(result.Url)
91+
result.Url = service.NewAttachmentService().GetFileUrl(result.Url)
9292
}
9393

9494
extra := ""

app/tool/upload/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (p *Image) AfterHandle(ctx *quark.Context, result *quark.FileInfo) error {
7777

7878
// 重写url
7979
if driver == quark.LocalStorage {
80-
result.Url = service.NewAttachmentService().GetPath(result.Url)
80+
result.Url = service.NewAttachmentService().GetUrl(result.Url)
8181
}
8282

8383
extra := ""

engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
AppName = "QuarkGo"
2424

2525
// 版本号
26-
Version = "3.9.0"
26+
Version = "3.9.1"
2727

2828
// 包名
2929
PkgName = "github.com/quarkcloudio/quark-go/v3"

service/attachment.go

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (p *AttachmentService) GetListBySearch(adminId interface{}, attachmentType
4747
Find(&attachments)
4848

4949
for k, v := range attachments {
50-
v.Url = p.GetPath(v.Url) + "?timestamp=" + strconv.Itoa(int(time.Now().Unix()))
50+
v.Url = p.GetUrl(v.Url) + "?timestamp=" + strconv.Itoa(int(time.Now().Unix()))
5151
attachments[k] = v
5252
}
5353

@@ -91,8 +91,8 @@ func (p *AttachmentService) GetInfoByHash(hash string) (attachment model.Attachm
9191
return attachment, err
9292
}
9393

94-
// 获取附件路径,GetPath(1) 或者 GetPath("FILE", 1)
95-
func (p *AttachmentService) GetPath(params ...interface{}) string {
94+
// 获取附件访问Url,GetUrl(1) 或者 GetUrl("FILE", 1)
95+
func (p *AttachmentService) GetUrl(params ...interface{}) string {
9696
var id, attachmentType interface{}
9797
if len(params) == 1 {
9898
id = params[0]
@@ -170,18 +170,18 @@ func (p *AttachmentService) GetPath(params ...interface{}) string {
170170
return ""
171171
}
172172

173-
// 获取文件路径
174-
func (p *AttachmentService) GetFilePath(id interface{}) string {
175-
return p.GetPath("FILE", id)
173+
// 获取文件访问Url
174+
func (p *AttachmentService) GetFileUrl(id interface{}) string {
175+
return p.GetUrl("FILE", id)
176176
}
177177

178-
// 获取图片路径
179-
func (p *AttachmentService) GetImagePath(id interface{}) string {
180-
return p.GetPath("IMAGE", id)
178+
// 获取图片访问Url
179+
func (p *AttachmentService) GetImageUrl(id interface{}) string {
180+
return p.GetUrl("IMAGE", id)
181181
}
182182

183-
// 获取多文件路径
184-
func (p *AttachmentService) GetPaths(id interface{}) []string {
183+
// 获取多文件访问Url
184+
func (p *AttachmentService) GetUrls(id interface{}) []string {
185185
var paths []string
186186
http, path := "", ""
187187
webSiteDomain := NewConfigService().GetValue("WEB_SITE_DOMAIN")
@@ -221,6 +221,75 @@ func (p *AttachmentService) GetPaths(id interface{}) []string {
221221
return paths
222222
}
223223

224+
// 获取附件存储路径,GetPath(1) 或者 GetPath(1)
225+
func (p *AttachmentService) GetPath(id interface{}) string {
226+
path := ""
227+
if getId, ok := id.(string); ok {
228+
if strings.Contains(getId, "//") && !strings.Contains(getId, "{") {
229+
return getId
230+
}
231+
if strings.Contains(getId, "./") && !strings.Contains(getId, "{") {
232+
return getId
233+
}
234+
if strings.Contains(getId, "/") && !strings.Contains(getId, "{") {
235+
return getId
236+
}
237+
238+
// json字符串
239+
if strings.Contains(getId, "{") {
240+
var jsonData interface{}
241+
json.Unmarshal([]byte(getId), &jsonData)
242+
if mapData, ok := jsonData.(map[string]interface{}); ok {
243+
id = mapData["id"].(string)
244+
}
245+
246+
// 如果为数组,返回第一个key的path
247+
if arrayData, ok := jsonData.([]map[string]interface{}); ok {
248+
id = arrayData[0]["id"].(string)
249+
}
250+
}
251+
if strings.Contains(path, "//") {
252+
return path
253+
}
254+
if strings.Contains(path, "./") {
255+
return path
256+
}
257+
}
258+
259+
attachment := model.Attachment{}
260+
db.Client.Where("id", id).Where("status", 1).First(&attachment)
261+
262+
return attachment.Path
263+
}
264+
265+
// 获取文件存储路径
266+
func (p *AttachmentService) GetFilePath(id interface{}) string {
267+
return p.GetPath(id)
268+
}
269+
270+
// 获取图片存储路径
271+
func (p *AttachmentService) GetImagePath(id interface{}) string {
272+
return p.GetPath(id)
273+
}
274+
275+
// 获取多文件存储路径
276+
func (p *AttachmentService) GetPaths(id interface{}) []string {
277+
var paths []string
278+
if getId, ok := id.(string); ok {
279+
if strings.Contains(getId, "{") {
280+
var jsonData []map[string]interface{}
281+
err := json.Unmarshal([]byte(getId), &jsonData)
282+
if err == nil {
283+
for _, v := range jsonData {
284+
paths = append(paths, p.GetPath(v["id"]))
285+
}
286+
}
287+
}
288+
}
289+
290+
return paths
291+
}
292+
224293
// 获取Excel文件数据
225294
func (p *AttachmentService) GetExcelData(fileId int) (data [][]interface{}, Error error) {
226295
file := model.Attachment{}

service/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (p *UserService) GetInfoById(id interface{}) (user model.User, Error error)
2323
func (p *UserService) GetInfoByUsername(username string) (user model.User, Error error) {
2424
err := db.Client.Where("status = ?", 1).Where("username = ?", username).First(&user).Error
2525
if user.Avatar != "" {
26-
user.Avatar = NewAttachmentService().GetImagePath(user.Avatar) // 获取头像地址
26+
user.Avatar = NewAttachmentService().GetImageUrl(user.Avatar) // 获取头像地址
2727
}
2828
return user, err
2929
}

template/admin/resource/requests/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (p *IndexRequest) performsList(ctx *quark.Context, lists []map[string]inter
197197

198198
// 单独解析图片、图片选择器组件
199199
if component == "imageField" || component == "imagePickerField" {
200-
fieldValue = service.NewAttachmentService().GetImagePath(v[name].(string))
200+
fieldValue = service.NewAttachmentService().GetImageUrl(v[name].(string))
201201
}
202202

203203
fields[name] = fieldValue

0 commit comments

Comments
 (0)