mirror of
https://github.com/cloudreve/cloudreve.git
synced 2025-11-01 19:13:39 +08:00
Init V4 community edition (#2265)
* Init V4 community edition * Init V4 community edition
This commit is contained in:
@ -1,80 +1,145 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/cluster/routes"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// ShareBatchService 分享批量操作服务
|
||||
type ShareBatchService struct {
|
||||
ID []uint `json:"id" binding:"min=1"`
|
||||
}
|
||||
const (
|
||||
shareUserIDCondition = "share_user_id"
|
||||
shareFileIDCondition = "share_file_id"
|
||||
)
|
||||
|
||||
// Delete 删除文件
|
||||
func (service *ShareBatchService) Delete(c *gin.Context) serializer.Response {
|
||||
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Share{}).Error; err != nil {
|
||||
return serializer.DBErr("Failed to delete share record", err)
|
||||
}
|
||||
return serializer.Response{}
|
||||
}
|
||||
func (s *AdminListService) Shares(c *gin.Context) (*ListShareResponse, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
shareClient := dep.ShareClient()
|
||||
hasher := dep.HashIDEncoder()
|
||||
|
||||
// Shares 列出分享
|
||||
func (service *AdminListService) Shares() serializer.Response {
|
||||
var res []model.Share
|
||||
total := 0
|
||||
var (
|
||||
err error
|
||||
userID int
|
||||
fileID int
|
||||
)
|
||||
|
||||
tx := model.DB.Model(&model.Share{})
|
||||
if service.OrderBy != "" {
|
||||
tx = tx.Order(service.OrderBy)
|
||||
}
|
||||
|
||||
for k, v := range service.Conditions {
|
||||
tx = tx.Where(k+" = ?", v)
|
||||
}
|
||||
|
||||
if len(service.Searches) > 0 {
|
||||
search := ""
|
||||
for k, v := range service.Searches {
|
||||
search += k + " like '%" + v + "%' OR "
|
||||
if s.Conditions[shareUserIDCondition] != "" {
|
||||
userID, err = strconv.Atoi(s.Conditions[shareUserIDCondition])
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeParamErr, "Invalid share user ID", err)
|
||||
}
|
||||
search = strings.TrimSuffix(search, " OR ")
|
||||
tx = tx.Where(search)
|
||||
}
|
||||
|
||||
// 计算总数用于分页
|
||||
tx.Count(&total)
|
||||
|
||||
// 查询记录
|
||||
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
|
||||
|
||||
// 查询对应用户,同时计算HashID
|
||||
users := make(map[uint]model.User)
|
||||
hashIDs := make(map[uint]string, len(res))
|
||||
for _, file := range res {
|
||||
users[file.UserID] = model.User{}
|
||||
hashIDs[file.ID] = hashid.HashID(file.ID, hashid.ShareID)
|
||||
if s.Conditions[shareFileIDCondition] != "" {
|
||||
fileID, err = strconv.Atoi(s.Conditions[shareFileIDCondition])
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeParamErr, "Invalid share file ID", err)
|
||||
}
|
||||
}
|
||||
|
||||
userIDs := make([]uint, 0, len(users))
|
||||
for k := range users {
|
||||
userIDs = append(userIDs, k)
|
||||
ctx := context.WithValue(c, inventory.LoadShareFile{}, true)
|
||||
ctx = context.WithValue(ctx, inventory.LoadShareUser{}, true)
|
||||
|
||||
res, err := shareClient.List(ctx, &inventory.ListShareArgs{
|
||||
PaginationArgs: &inventory.PaginationArgs{
|
||||
Page: s.Page - 1,
|
||||
PageSize: s.PageSize,
|
||||
OrderBy: s.OrderBy,
|
||||
Order: inventory.OrderDirection(s.OrderDirection),
|
||||
},
|
||||
UserID: userID,
|
||||
FileID: fileID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to list shares", err)
|
||||
}
|
||||
|
||||
var userList []model.User
|
||||
model.DB.Where("id in (?)", userIDs).Find(&userList)
|
||||
siteUrl := dep.SettingProvider().SiteURL(c)
|
||||
|
||||
for _, v := range userList {
|
||||
users[v.ID] = v
|
||||
}
|
||||
return &ListShareResponse{
|
||||
Pagination: res.PaginationResults,
|
||||
Shares: lo.Map(res.Shares, func(share *ent.Share, _ int) GetShareResponse {
|
||||
var (
|
||||
uid string
|
||||
shareLink string
|
||||
)
|
||||
|
||||
if share.Edges.User != nil {
|
||||
uid = hashid.EncodeUserID(hasher, share.Edges.User.ID)
|
||||
}
|
||||
|
||||
shareLink = routes.MasterShareUrl(siteUrl, hashid.EncodeShareID(hasher, share.ID), share.Password).String()
|
||||
|
||||
return GetShareResponse{
|
||||
Share: share,
|
||||
UserHashID: uid,
|
||||
ShareLink: shareLink,
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
|
||||
return serializer.Response{Data: map[string]interface{}{
|
||||
"total": total,
|
||||
"items": res,
|
||||
"users": users,
|
||||
"ids": hashIDs,
|
||||
}}
|
||||
}
|
||||
|
||||
type (
|
||||
SingleShareService struct {
|
||||
ShareID int `uri:"id" binding:"required"`
|
||||
}
|
||||
SingleShareParamCtx struct{}
|
||||
)
|
||||
|
||||
func (s *SingleShareService) Get(c *gin.Context) (*GetShareResponse, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
shareClient := dep.ShareClient()
|
||||
hasher := dep.HashIDEncoder()
|
||||
|
||||
ctx := context.WithValue(c, inventory.LoadShareFile{}, true)
|
||||
ctx = context.WithValue(ctx, inventory.LoadShareUser{}, true)
|
||||
share, err := shareClient.GetByID(ctx, s.ShareID)
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to get share", err)
|
||||
}
|
||||
|
||||
var (
|
||||
uid string
|
||||
shareLink string
|
||||
)
|
||||
|
||||
if share.Edges.User != nil {
|
||||
uid = hashid.EncodeShareID(hasher, share.Edges.User.ID)
|
||||
}
|
||||
|
||||
siteUrl := dep.SettingProvider().SiteURL(c)
|
||||
shareLink = routes.MasterShareUrl(siteUrl, hashid.EncodeShareID(hasher, share.ID), share.Password).String()
|
||||
|
||||
return &GetShareResponse{
|
||||
Share: share,
|
||||
UserHashID: uid,
|
||||
ShareLink: shareLink,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type (
|
||||
BatchShareService struct {
|
||||
ShareIDs []int `json:"ids" binding:"required"`
|
||||
}
|
||||
BatchShareParamCtx struct{}
|
||||
)
|
||||
|
||||
func (s *BatchShareService) Delete(c *gin.Context) error {
|
||||
dep := dependency.FromContext(c)
|
||||
shareClient := dep.ShareClient()
|
||||
|
||||
if err := shareClient.DeleteBatch(c, s.ShareIDs); err != nil {
|
||||
return serializer.NewError(serializer.CodeDBError, "Failed to delete shares", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user