mirror of https://github.com/cloudreve/Cloudreve
Feat: preview file in dashboard
parent
27c0b6e886
commit
768ffa24ec
|
@ -326,3 +326,33 @@ func AdminBanUser(c *gin.Context) {
|
||||||
c.JSON(200, ErrorResponse(err))
|
c.JSON(200, ErrorResponse(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AdminListFile 列出文件
|
||||||
|
func AdminListFile(c *gin.Context) {
|
||||||
|
var service admin.AdminListService
|
||||||
|
if err := c.ShouldBindJSON(&service); err == nil {
|
||||||
|
res := service.Files()
|
||||||
|
c.JSON(200, res)
|
||||||
|
} else {
|
||||||
|
c.JSON(200, ErrorResponse(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AdminGetFile 获取文件
|
||||||
|
func AdminGetFile(c *gin.Context) {
|
||||||
|
var service admin.FileService
|
||||||
|
if err := c.ShouldBindUri(&service); err == nil {
|
||||||
|
res := service.Get(c)
|
||||||
|
// 是否需要重定向
|
||||||
|
if res.Code == -301 {
|
||||||
|
c.Redirect(301, res.Data.(string))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 是否有错误发生
|
||||||
|
if res.Code != 0 {
|
||||||
|
c.JSON(200, res)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, ErrorResponse(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -379,6 +379,14 @@ func InitMasterRouter() *gin.Engine {
|
||||||
user.PATCH("ban/:id", controllers.AdminBanUser)
|
user.PATCH("ban/:id", controllers.AdminBanUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file := admin.Group("file")
|
||||||
|
{
|
||||||
|
// 列出文件
|
||||||
|
file.POST("list", controllers.AdminListFile)
|
||||||
|
// 预览文件
|
||||||
|
file.GET("preview/:id", controllers.AdminGetFile)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用户
|
// 用户
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
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,
|
||||||
|
}}
|
||||||
|
}
|
Loading…
Reference in New Issue