🚧 optimized the api for obtaining image list

This commit is contained in:
2025-02-10 01:06:43 +08:00
parent 7e81a4ff96
commit 1d27c6ee8a
8 changed files with 125 additions and 68 deletions

View File

@@ -503,12 +503,14 @@ type (
}
// 所有图片列表响应参数
ImageMeta {
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"`
}
AllImageDetail {
Date string `json:"date"`

View File

@@ -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
}

View File

@@ -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

View File

@@ -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"
}

View File

@@ -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"`
}

View File

@@ -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 {

View File

@@ -22,19 +22,21 @@ type ScaStorageInfo struct {
FileName string `gorm:"column:file_name;type:varchar(100);comment:文件名称" json:"file_name"` // 文件名称
FileSize string `gorm:"column:file_size;type:varchar(50);comment:文件大小" json:"file_size"` // 文件大小
FileType string `gorm:"column:file_type;type:varchar(50);comment:文件类型" json:"file_type"` // 文件类型
Width float64 `gorm:"column:width;type:double;comment:宽" json:"width"` // 宽
Height float64 `gorm:"column:height;type:double;comment:高" json:"height"` // 高
Category string `gorm:"column:category;type:varchar(50);comment:分类" json:"category"` // 分类
Tags string `gorm:"column:tags;type:varchar(255);comment:标签" json:"tags"` // 标签
Type string `gorm:"column:type;type:varchar(50);comment:类型" json:"type"` // 类型
Location string `gorm:"column:location;type:varchar(100);comment:地址" json:"location"` // 地址
Hash string `gorm:"column:hash;type:varchar(255);comment:哈希值" json:"hash"` // 哈希值
Anime string `gorm:"column:anime;type:varchar(50);comment:是否是动漫图片" json:"anime"` // 是否是动漫图片
FaceID int64 `gorm:"column:face_id;type:bigint(20);comment:人像ID" json:"face_id"` // 人像ID
Landscape string `gorm:"column:landscape;type:varchar(50);comment:风景类型" json:"landscape"` // 风景类型
Objects string `gorm:"column:objects;type:varchar(50);comment:对象识别" json:"objects"` // 对象识别
OriginalTime string `gorm:"column:original_time;type:varchar(50);comment:拍摄时间" json:"original_time"` // 拍摄时间
Gps string `gorm:"column:gps;type:varchar(255);comment:GPS" json:"gps"` // GPS
Screenshot string `gorm:"column:screenshot;type:varchar(50);comment:是否是截图" json:"screenshot"` // 是否是截图
Exif string `gorm:"column:exif;type:json;comment:exif 信息" json:"exif"` // exif 信息
Hide int64 `gorm:"column:hide;type:int(11) unsigned zerofill;comment:是否隐藏0 不隐藏 1 隐藏)" json:"hide"` // 是否隐藏0 不隐藏 1 隐藏)
Show string `gorm:"column:show;type:varchar(50);comment:是否隐藏0 不隐藏 1 隐藏)" json:"show"` // 是否隐藏0 不隐藏 1 隐藏)
AlbumID int64 `gorm:"column:album_id;type:bigint(20);comment:相册ID" json:"album_id"` // 相册ID
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;autoUpdateTime;comment:更新时间" json:"updated_at"` // 更新时间

View File

@@ -35,19 +35,21 @@ func newScaStorageInfo(db *gorm.DB, opts ...gen.DOOption) scaStorageInfo {
_scaStorageInfo.FileName = field.NewString(tableName, "file_name")
_scaStorageInfo.FileSize = field.NewString(tableName, "file_size")
_scaStorageInfo.FileType = field.NewString(tableName, "file_type")
_scaStorageInfo.Width = field.NewFloat64(tableName, "width")
_scaStorageInfo.Height = field.NewFloat64(tableName, "height")
_scaStorageInfo.Category = field.NewString(tableName, "category")
_scaStorageInfo.Tags = field.NewString(tableName, "tags")
_scaStorageInfo.Type = field.NewString(tableName, "type")
_scaStorageInfo.Location = field.NewString(tableName, "location")
_scaStorageInfo.Hash = field.NewString(tableName, "hash")
_scaStorageInfo.Anime = field.NewString(tableName, "anime")
_scaStorageInfo.FaceID = field.NewInt64(tableName, "face_id")
_scaStorageInfo.Landscape = field.NewString(tableName, "landscape")
_scaStorageInfo.Objects = field.NewString(tableName, "objects")
_scaStorageInfo.OriginalTime = field.NewString(tableName, "original_time")
_scaStorageInfo.Gps = field.NewString(tableName, "gps")
_scaStorageInfo.Screenshot = field.NewString(tableName, "screenshot")
_scaStorageInfo.Exif = field.NewString(tableName, "exif")
_scaStorageInfo.Hide = field.NewInt64(tableName, "hide")
_scaStorageInfo.Show = field.NewString(tableName, "show")
_scaStorageInfo.AlbumID = field.NewInt64(tableName, "album_id")
_scaStorageInfo.CreatedAt = field.NewTime(tableName, "created_at")
_scaStorageInfo.UpdatedAt = field.NewTime(tableName, "updated_at")
@@ -62,31 +64,33 @@ type scaStorageInfo struct {
scaStorageInfoDo
ALL field.Asterisk
ID field.Int64 // 主键
UserID field.String // 用户ID
Provider field.String // 供应商
Bucket field.String // 存储桶
Path field.String // 路径
FileName field.String // 文件名称
FileSize field.String // 文件大小
FileType field.String // 文件类型
Category field.String // 分类
Tags field.String // 标签
Location field.String // 地址
Hash field.String // 哈希值
Anime field.String // 是否是动漫图片
FaceID field.Int64 // 人像ID
Landscape field.String // 风景类型
Objects field.String // 对象识别
OriginalTime field.String // 拍摄时间
Gps field.String // GPS
Screenshot field.String // 是否是截图
Exif field.String // exif 信息
Hide field.Int64 // 是否隐藏0 不隐藏 1 隐藏)
AlbumID field.Int64 // 相册ID
CreatedAt field.Time // 创建时间
UpdatedAt field.Time // 更新时间
DeletedAt field.Field // 删除时间
ID field.Int64 // 主键
UserID field.String // 用户ID
Provider field.String // 供应商
Bucket field.String // 存储桶
Path field.String // 路径
FileName field.String // 文件名称
FileSize field.String // 文件大小
FileType field.String // 文件类型
Width field.Float64 //
Height field.Float64 //
Category field.String // 分类
Tags field.String // 标签
Type field.String // 类型
Location field.String // 地址
Hash field.String // 哈希值
Anime field.String // 是否是动漫图片
FaceID field.Int64 // 人像ID
Landscape field.String // 风景类型
OriginalTime field.String // 拍摄时间
Gps field.String // GPS
Screenshot field.String // 是否是截图
Exif field.String // exif 信息
Show field.String // 是否隐藏0 不隐藏 1 隐藏)
AlbumID field.Int64 // 相册ID
CreatedAt field.Time // 创建时间
UpdatedAt field.Time // 更新时间
DeletedAt field.Field // 删除时间
fieldMap map[string]field.Expr
}
@@ -111,19 +115,21 @@ func (s *scaStorageInfo) updateTableName(table string) *scaStorageInfo {
s.FileName = field.NewString(table, "file_name")
s.FileSize = field.NewString(table, "file_size")
s.FileType = field.NewString(table, "file_type")
s.Width = field.NewFloat64(table, "width")
s.Height = field.NewFloat64(table, "height")
s.Category = field.NewString(table, "category")
s.Tags = field.NewString(table, "tags")
s.Type = field.NewString(table, "type")
s.Location = field.NewString(table, "location")
s.Hash = field.NewString(table, "hash")
s.Anime = field.NewString(table, "anime")
s.FaceID = field.NewInt64(table, "face_id")
s.Landscape = field.NewString(table, "landscape")
s.Objects = field.NewString(table, "objects")
s.OriginalTime = field.NewString(table, "original_time")
s.Gps = field.NewString(table, "gps")
s.Screenshot = field.NewString(table, "screenshot")
s.Exif = field.NewString(table, "exif")
s.Hide = field.NewInt64(table, "hide")
s.Show = field.NewString(table, "show")
s.AlbumID = field.NewInt64(table, "album_id")
s.CreatedAt = field.NewTime(table, "created_at")
s.UpdatedAt = field.NewTime(table, "updated_at")
@@ -144,7 +150,7 @@ func (s *scaStorageInfo) GetFieldByName(fieldName string) (field.OrderExpr, bool
}
func (s *scaStorageInfo) fillFieldMap() {
s.fieldMap = make(map[string]field.Expr, 25)
s.fieldMap = make(map[string]field.Expr, 27)
s.fieldMap["id"] = s.ID
s.fieldMap["user_id"] = s.UserID
s.fieldMap["provider"] = s.Provider
@@ -153,19 +159,21 @@ func (s *scaStorageInfo) fillFieldMap() {
s.fieldMap["file_name"] = s.FileName
s.fieldMap["file_size"] = s.FileSize
s.fieldMap["file_type"] = s.FileType
s.fieldMap["width"] = s.Width
s.fieldMap["height"] = s.Height
s.fieldMap["category"] = s.Category
s.fieldMap["tags"] = s.Tags
s.fieldMap["type"] = s.Type
s.fieldMap["location"] = s.Location
s.fieldMap["hash"] = s.Hash
s.fieldMap["anime"] = s.Anime
s.fieldMap["face_id"] = s.FaceID
s.fieldMap["landscape"] = s.Landscape
s.fieldMap["objects"] = s.Objects
s.fieldMap["original_time"] = s.OriginalTime
s.fieldMap["gps"] = s.Gps
s.fieldMap["screenshot"] = s.Screenshot
s.fieldMap["exif"] = s.Exif
s.fieldMap["hide"] = s.Hide
s.fieldMap["show"] = s.Show
s.fieldMap["album_id"] = s.AlbumID
s.fieldMap["created_at"] = s.CreatedAt
s.fieldMap["updated_at"] = s.UpdatedAt