You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.9 KiB
85 lines
1.9 KiB
5 years ago
|
package admin
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
model "github.com/HFO4/cloudreve/models"
|
||
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||
|
"github.com/HFO4/cloudreve/service/explorer"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// FileService 文件ID服务
|
||
|
type FileService struct {
|
||
|
ID uint `uri:"id" json:"id" binding:"required"`
|
||
|
}
|
||
|
|
||
|
// Get 预览文件
|
||
|
func (service *FileService) Get(c *gin.Context) serializer.Response {
|
||
|
file, err := model.GetFilesByIDs([]uint{service.ID}, 0)
|
||
|
if err != nil {
|
||
|
return serializer.Err(serializer.CodeNotFound, "文件不存在", err)
|
||
|
}
|
||
|
|
||
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, &file[0])
|
||
|
var subService explorer.FileIDService
|
||
|
res := subService.PreviewContent(ctx, c, false)
|
||
|
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
// Files 列出文件
|
||
|
func (service *AdminListService) Files() serializer.Response {
|
||
|
var res []model.File
|
||
|
total := 0
|
||
|
|
||
|
tx := model.DB.Model(&model.File{})
|
||
|
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 "
|
||
|
}
|
||
|
search = strings.TrimSuffix(search, " OR ")
|
||
|
tx = tx.Where(search)
|
||
|
}
|
||
|
|
||
|
// 计算总数用于分页
|
||
|
tx.Count(&total)
|
||
|
|
||
|
// 查询记录
|
||
|
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
|
||
|
|
||
|
// 查询对应用户
|
||
|
users := make(map[uint]model.User)
|
||
|
for _, file := range res {
|
||
|
users[file.UserID] = model.User{}
|
||
|
}
|
||
|
|
||
|
userIDs := make([]uint, 0, len(users))
|
||
|
for k := range users {
|
||
|
userIDs = append(userIDs, k)
|
||
|
}
|
||
|
|
||
|
var userList []model.User
|
||
|
model.DB.Where("id in (?)", userIDs).Find(&userList)
|
||
|
|
||
|
for _, v := range userList {
|
||
|
users[v.ID] = v
|
||
|
}
|
||
|
|
||
|
return serializer.Response{Data: map[string]interface{}{
|
||
|
"total": total,
|
||
|
"items": res,
|
||
|
"users": users,
|
||
|
}}
|
||
|
}
|