feat: 添加媒体资源管理相关API接口及数据结构,包括列表和详情查询
This commit is contained in:
@@ -11,12 +11,16 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
tenant_dto "quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
pkgerrors "github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
"go.ipao.vip/gen/types"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -44,7 +48,7 @@ func newObjectKey(tenantID, userID int64, assetType consts.MediaAssetType, now t
|
||||
|
||||
// AdminUploadInit creates a MediaAsset record and returns upload parameters.
|
||||
// 当前版本为“stub 上传初始化”:只负责生成 asset 与 object_key,不对接外部存储签名。
|
||||
func (s *mediaAsset) AdminUploadInit(ctx context.Context, tenantID, operatorUserID int64, form *dto.AdminMediaAssetUploadInitForm, now time.Time) (*dto.AdminMediaAssetUploadInitResponse, error) {
|
||||
func (s *mediaAsset) AdminUploadInit(ctx context.Context, tenantID, operatorUserID int64, form *tenant_dto.AdminMediaAssetUploadInitForm, now time.Time) (*tenant_dto.AdminMediaAssetUploadInitResponse, error) {
|
||||
if tenantID <= 0 || operatorUserID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("tenant_id/operator_user_id must be > 0")
|
||||
}
|
||||
@@ -105,7 +109,7 @@ func (s *mediaAsset) AdminUploadInit(ctx context.Context, tenantID, operatorUser
|
||||
}).Info("services.media_asset.admin.upload_init")
|
||||
|
||||
// 约定:upload_url 先返回空或内部占位;后续接入真实存储签名后再补齐。
|
||||
return &dto.AdminMediaAssetUploadInitResponse{
|
||||
return &tenant_dto.AdminMediaAssetUploadInitResponse{
|
||||
AssetID: m.ID,
|
||||
Provider: m.Provider,
|
||||
Bucket: m.Bucket,
|
||||
@@ -124,7 +128,7 @@ func (s *mediaAsset) AdminUploadInit(ctx context.Context, tenantID, operatorUser
|
||||
func (s *mediaAsset) AdminUploadComplete(
|
||||
ctx context.Context,
|
||||
tenantID, operatorUserID, assetID int64,
|
||||
form *dto.AdminMediaAssetUploadCompleteForm,
|
||||
form *tenant_dto.AdminMediaAssetUploadCompleteForm,
|
||||
now time.Time,
|
||||
) (*models.MediaAsset, error) {
|
||||
if tenantID <= 0 || operatorUserID <= 0 || assetID <= 0 {
|
||||
@@ -226,3 +230,117 @@ func (s *mediaAsset) AdminUploadComplete(
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// AdminPage 分页查询租户内媒体资源(租户管理)。
|
||||
func (s *mediaAsset) AdminPage(ctx context.Context, tenantID int64, filter *tenant_dto.AdminMediaAssetListFilter) (*requests.Pager, error) {
|
||||
if tenantID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("tenant_id must be > 0")
|
||||
}
|
||||
if filter == nil {
|
||||
filter = &tenant_dto.AdminMediaAssetListFilter{}
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"type": lo.FromPtr(filter.Type),
|
||||
"status": lo.FromPtr(filter.Status),
|
||||
"created_at_from": filter.CreatedAtFrom,
|
||||
"created_at_to": filter.CreatedAtTo,
|
||||
"sort_asc_fields": filter.AscFields(),
|
||||
"sort_desc_fields": filter.DescFields(),
|
||||
}).Info("services.media_asset.admin.page")
|
||||
|
||||
filter.Pagination.Format()
|
||||
|
||||
tbl, query := models.MediaAssetQuery.QueryContext(ctx)
|
||||
|
||||
conds := []gen.Condition{
|
||||
tbl.TenantID.Eq(tenantID),
|
||||
tbl.DeletedAt.IsNull(),
|
||||
}
|
||||
if filter.Type != nil {
|
||||
conds = append(conds, tbl.Type.Eq(*filter.Type))
|
||||
}
|
||||
if filter.Status != nil {
|
||||
conds = append(conds, tbl.Status.Eq(*filter.Status))
|
||||
}
|
||||
if filter.CreatedAtFrom != nil {
|
||||
conds = append(conds, tbl.CreatedAt.Gte(*filter.CreatedAtFrom))
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
conds = append(conds, tbl.CreatedAt.Lte(*filter.CreatedAtTo))
|
||||
}
|
||||
|
||||
// 排序白名单:避免把任意字符串拼进 SQL 导致注入或慢查询。
|
||||
orderBys := make([]field.Expr, 0, 4)
|
||||
allowedAsc := map[string]field.Expr{
|
||||
"id": tbl.ID.Asc(),
|
||||
"created_at": tbl.CreatedAt.Asc(),
|
||||
"updated_at": tbl.UpdatedAt.Asc(),
|
||||
}
|
||||
allowedDesc := map[string]field.Expr{
|
||||
"id": tbl.ID.Desc(),
|
||||
"created_at": tbl.CreatedAt.Desc(),
|
||||
"updated_at": tbl.UpdatedAt.Desc(),
|
||||
}
|
||||
for _, f := range filter.AscFields() {
|
||||
f = strings.TrimSpace(f)
|
||||
if f == "" {
|
||||
continue
|
||||
}
|
||||
if ob, ok := allowedAsc[f]; ok {
|
||||
orderBys = append(orderBys, ob)
|
||||
}
|
||||
}
|
||||
for _, f := range filter.DescFields() {
|
||||
f = strings.TrimSpace(f)
|
||||
if f == "" {
|
||||
continue
|
||||
}
|
||||
if ob, ok := allowedDesc[f]; ok {
|
||||
orderBys = append(orderBys, ob)
|
||||
}
|
||||
}
|
||||
if len(orderBys) == 0 {
|
||||
orderBys = append(orderBys, tbl.ID.Desc())
|
||||
} else {
|
||||
orderBys = append(orderBys, tbl.ID.Desc())
|
||||
}
|
||||
|
||||
items, total, err := query.Where(conds...).Order(orderBys...).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AdminDetail 查询租户内媒体资源详情(租户管理)。
|
||||
func (s *mediaAsset) AdminDetail(ctx context.Context, tenantID, assetID int64) (*models.MediaAsset, error) {
|
||||
if tenantID <= 0 || assetID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("tenant_id/asset_id must be > 0")
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"asset_id": assetID,
|
||||
}).Info("services.media_asset.admin.detail")
|
||||
|
||||
tbl, query := models.MediaAssetQuery.QueryContext(ctx)
|
||||
m, err := query.Where(
|
||||
tbl.TenantID.Eq(tenantID),
|
||||
tbl.ID.Eq(assetID),
|
||||
tbl.DeletedAt.IsNull(),
|
||||
).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound.WithMsg("media asset not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user