91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
// Package sys
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
// @Copyright Copyright (c) 2026 HotGo
|
|
// @Author Ms <133814250@qq.com>
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
package sys
|
|
|
|
import (
|
|
"context"
|
|
"hotgo/internal/library/storager"
|
|
"hotgo/internal/model/input/sysin"
|
|
"hotgo/internal/service"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
func loadImPublicUploads(ctx context.Context, fileIds []string) (files []*sysin.ImPublicUploadModel, err error) {
|
|
fileIds = compactStringSlice(fileIds)
|
|
if len(fileIds) == 0 {
|
|
return []*sysin.ImPublicUploadModel{}, nil
|
|
}
|
|
if err = g.DB().Model("im_public_uploads").Safe().Ctx(ctx).
|
|
Fields(
|
|
"id",
|
|
"file_id",
|
|
"purpose",
|
|
"mime_type",
|
|
"size",
|
|
"etag",
|
|
"location",
|
|
"expires_at",
|
|
"created_at",
|
|
"uploaded_at",
|
|
).
|
|
WhereIn("file_id", fileIds).
|
|
OrderAsc("id").
|
|
Scan(&files); err != nil {
|
|
return nil, gerror.Wrap(err, "获取公共上传文件信息失败,请稍后重试!")
|
|
}
|
|
|
|
drive := ""
|
|
if conf, confErr := service.SysConfig().GetUpload(ctx); confErr == nil && conf != nil {
|
|
drive = conf.Drive
|
|
}
|
|
for _, file := range files {
|
|
file.Url = storager.LastUrl(ctx, file.Location, drive)
|
|
}
|
|
return
|
|
}
|
|
|
|
func firstImPublicUpload(ctx context.Context, fileId string) (file *sysin.ImPublicUploadModel, err error) {
|
|
files, err := loadImPublicUploads(ctx, []string{fileId})
|
|
if err != nil || len(files) == 0 {
|
|
return
|
|
}
|
|
return files[0], nil
|
|
}
|
|
|
|
func parseJSONStringSlice(raw string) []string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" || raw == "null" {
|
|
return nil
|
|
}
|
|
|
|
var ids []string
|
|
if err := gjson.DecodeTo([]byte(raw), &ids); err == nil && len(ids) > 0 {
|
|
return compactStringSlice(ids)
|
|
}
|
|
return compactStringSlice([]string{raw})
|
|
}
|
|
|
|
func compactStringSlice(values []string) []string {
|
|
res := make([]string, 0, len(values))
|
|
seen := make(map[string]struct{}, len(values))
|
|
for _, value := range values {
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[value]; ok {
|
|
continue
|
|
}
|
|
seen[value] = struct{}{}
|
|
res = append(res, value)
|
|
}
|
|
return res
|
|
}
|