🚧 optimized the api for obtaining image list
This commit is contained in:
@@ -35,7 +35,7 @@ func (l *ModifyFaceLibraryTypeLogic) ModifyFaceLibraryType(req *types.ModifyFace
|
||||
return nil, err
|
||||
}
|
||||
storageInfo := l.svcCtx.DB.ScaStorageInfo
|
||||
resultInfo, err := storageInfo.Where(storageInfo.FaceID.In(req.IDs...)).Update(storageInfo.Hide, req.FaceType)
|
||||
resultInfo, err := storageInfo.Where(storageInfo.FaceID.In(req.IDs...)).Update(storageInfo.Show, req.FaceType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"math/rand"
|
||||
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
|
||||
@@ -16,8 +17,6 @@ import (
|
||||
storageConfig "schisandra-album-cloud-microservices/common/storage/config"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryAllImageListLogic struct {
|
||||
@@ -39,7 +38,7 @@ func (l *QueryAllImageListLogic) QueryAllImageList(req *types.AllImageListReques
|
||||
if !ok {
|
||||
return nil, errors.New("user_id not found")
|
||||
}
|
||||
// 从缓存获取数据
|
||||
// 缓存获取数据 v1.0.0
|
||||
cacheKey := fmt.Sprintf("%s%s:%s:%s:%t", constant.ImageListPrefix, uid, req.Provider, req.Bucket, req.Sort)
|
||||
// 尝试从缓存获取
|
||||
cachedResult, err := l.svcCtx.RedisClient.Get(l.ctx, cacheKey).Result()
|
||||
@@ -60,9 +59,20 @@ func (l *QueryAllImageListLogic) QueryAllImageList(req *types.AllImageListReques
|
||||
// 数据库查询文件信息列表
|
||||
var storageInfoQuery query.IScaStorageInfoDo
|
||||
if req.Sort {
|
||||
storageInfoQuery = storageInfo.Where(storageInfo.UserID.Eq(uid), storageInfo.Provider.Eq(req.Provider), storageInfo.Bucket.Eq(req.Bucket), storageInfo.AlbumID.IsNull()).Order(storageInfo.CreatedAt.Desc())
|
||||
storageInfoQuery = storageInfo.Where(
|
||||
storageInfo.UserID.Eq(uid),
|
||||
storageInfo.Provider.Eq(req.Provider),
|
||||
storageInfo.Bucket.Eq(req.Bucket),
|
||||
storageInfo.Type.Eq(req.Type),
|
||||
storageInfo.AlbumID.IsNull()).
|
||||
Order(storageInfo.CreatedAt.Desc())
|
||||
} else {
|
||||
storageInfoQuery = storageInfo.Where(storageInfo.UserID.Eq(uid), storageInfo.Provider.Eq(req.Provider), storageInfo.Bucket.Eq(req.Bucket)).Order(storageInfo.CreatedAt.Desc())
|
||||
storageInfoQuery = storageInfo.Where(
|
||||
storageInfo.UserID.Eq(uid),
|
||||
storageInfo.Provider.Eq(req.Provider),
|
||||
storageInfo.Bucket.Eq(req.Bucket),
|
||||
storageInfo.Type.Eq(req.Type)).
|
||||
Order(storageInfo.CreatedAt.Desc())
|
||||
}
|
||||
storageInfoList, err := storageInfoQuery.Find()
|
||||
if err != nil {
|
||||
@@ -109,6 +119,8 @@ func (l *QueryAllImageListLogic) QueryAllImageList(req *types.AllImageListReques
|
||||
URL: url,
|
||||
FileSize: dbFileInfo.FileSize,
|
||||
CreatedAt: dbFileInfo.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
Width: dbFileInfo.Width,
|
||||
Height: dbFileInfo.Height,
|
||||
})
|
||||
|
||||
// 重新存储更新后的图像列表
|
||||
@@ -127,6 +139,7 @@ func (l *QueryAllImageListLogic) QueryAllImageList(req *types.AllImageListReques
|
||||
resp = &types.AllImageListResponse{
|
||||
Records: imageList,
|
||||
}
|
||||
|
||||
// 缓存结果
|
||||
if data, err := json.Marshal(resp); err == nil {
|
||||
expireTime := 7*24*time.Hour - time.Duration(rand.Intn(60))*time.Minute
|
||||
|
@@ -63,7 +63,6 @@ func (l *UploadFileLogic) UploadFile(r *http.Request) (resp string, err error) {
|
||||
return "", err
|
||||
}
|
||||
var faceId int64 = 0
|
||||
var className string
|
||||
bytes, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -78,12 +77,12 @@ func (l *UploadFileLogic) UploadFile(r *http.Request) (resp string, err error) {
|
||||
faceId = face.GetFaceId()
|
||||
}
|
||||
|
||||
// 图像分类
|
||||
classification, err := l.svcCtx.AiSvcRpc.TfClassification(l.ctx, &pb.TfClassificationRequest{Image: bytes})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
className = classification.GetClassName()
|
||||
//// 图像分类
|
||||
//classification, err := l.svcCtx.AiSvcRpc.TfClassification(l.ctx, &pb.TfClassificationRequest{Image: bytes})
|
||||
//if err != nil {
|
||||
// return "", err
|
||||
//}
|
||||
//className = classification.GetClassName()
|
||||
}
|
||||
|
||||
// 解析 EXIF 信息
|
||||
@@ -118,7 +117,7 @@ func (l *UploadFileLogic) UploadFile(r *http.Request) (resp string, err error) {
|
||||
}
|
||||
|
||||
// 将 EXIF 和文件信息存入数据库
|
||||
if err = l.saveFileInfoToDB(uid, bucket, provider, header, result, originalDateTime, gpsString, locationString, exif, faceId, className, filePath); err != nil {
|
||||
if err = l.saveFileInfoToDB(uid, bucket, provider, header, result, originalDateTime, gpsString, locationString, exif, faceId, filePath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -280,15 +279,12 @@ func (l *UploadFileLogic) uploadFileToOSS(uid string, header *multipart.FileHead
|
||||
}
|
||||
|
||||
// 将 EXIF 和文件信息存入数据库
|
||||
func (l *UploadFileLogic) saveFileInfoToDB(uid, bucket, provider string, header *multipart.FileHeader, result types.File, originalDateTime, gpsString, locationString string, exif map[string]interface{}, faceId int64, className, filePath string) error {
|
||||
func (l *UploadFileLogic) saveFileInfoToDB(uid, bucket, provider string, header *multipart.FileHeader, result types.File, originalDateTime, gpsString, locationString string, exif map[string]interface{}, faceId int64, filePath string) error {
|
||||
exifJSON, err := json.Marshal(exif)
|
||||
if err != nil {
|
||||
return errors.New("marshal exif failed")
|
||||
}
|
||||
var landscape string
|
||||
if result.Landscape != "none" {
|
||||
landscape = result.Landscape
|
||||
}
|
||||
typeName := l.classifyFile(result.FileType, result.IsScreenshot)
|
||||
scaStorageInfo := &model.ScaStorageInfo{
|
||||
UserID: uid,
|
||||
Provider: provider,
|
||||
@@ -297,8 +293,8 @@ func (l *UploadFileLogic) saveFileInfoToDB(uid, bucket, provider string, header
|
||||
FileSize: strconv.FormatInt(header.Size, 10),
|
||||
FileType: result.FileType,
|
||||
Path: filePath,
|
||||
Landscape: landscape,
|
||||
Objects: strings.Join(result.ObjectArray, ", "),
|
||||
Landscape: result.Landscape,
|
||||
Tags: strings.Join(result.ObjectArray, ","),
|
||||
Anime: strconv.FormatBool(result.IsAnime),
|
||||
Category: result.TopCategory,
|
||||
Screenshot: strconv.FormatBool(result.IsScreenshot),
|
||||
@@ -307,7 +303,9 @@ func (l *UploadFileLogic) saveFileInfoToDB(uid, bucket, provider string, header
|
||||
Location: locationString,
|
||||
Exif: string(exifJSON),
|
||||
FaceID: faceId,
|
||||
Tags: className,
|
||||
Type: typeName,
|
||||
Width: result.Width,
|
||||
Height: result.Height,
|
||||
}
|
||||
|
||||
err = l.svcCtx.DB.ScaStorageInfo.Create(scaStorageInfo)
|
||||
@@ -376,3 +374,33 @@ func (l *UploadFileLogic) getOssConfigFromCacheOrDb(cacheKey, uid, provider stri
|
||||
|
||||
return ossConfig, nil
|
||||
}
|
||||
|
||||
func (l *UploadFileLogic) classifyFile(mimeType string, isScreenshot bool) string {
|
||||
// 使用map存储MIME类型及其对应的分类
|
||||
typeMap := map[string]string{
|
||||
"image/jpeg": "image",
|
||||
"image/png": "image",
|
||||
"image/gif": "gif",
|
||||
"image/bmp": "image",
|
||||
"image/tiff": "image",
|
||||
"image/webp": "image",
|
||||
"video/mp4": "video",
|
||||
"video/avi": "video",
|
||||
"video/mpeg": "video",
|
||||
"video/quicktime": "video",
|
||||
"video/x-msvideo": "video",
|
||||
"video/x-flv": "video",
|
||||
"video/x-matroska": "video",
|
||||
}
|
||||
|
||||
// 根据MIME类型从map中获取分类
|
||||
if classification, exists := typeMap[mimeType]; exists {
|
||||
return classification
|
||||
}
|
||||
|
||||
// 如果isScreenshot为true,则返回"screenshot"
|
||||
if isScreenshot {
|
||||
return "screenshot"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
@@ -11,4 +11,6 @@ type File struct {
|
||||
TopCategory string `json:"topCategory"`
|
||||
IsScreenshot bool `json:"isScreenshot"`
|
||||
Exif any `json:"exif"`
|
||||
Width float64 `json:"width"`
|
||||
Height float64 `json:"height"`
|
||||
}
|
||||
|
@@ -160,12 +160,14 @@ type FaceSampleLibraryListResponse struct {
|
||||
}
|
||||
|
||||
type ImageMeta struct {
|
||||
ID int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
FilePath string `json:"file_path"`
|
||||
URL string `json:"url"`
|
||||
FileSize string `json:"file_size"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
ID int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
FilePath string `json:"file_path"`
|
||||
URL string `json:"url"`
|
||||
FileSize string `json:"file_size"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Width float64 `json:"width"`
|
||||
Height float64 `json:"height"`
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
|
Reference in New Issue
Block a user