Feat: delete file in dashboard

pull/247/head
HFO4 5 years ago
parent 768ffa24ec
commit 9166a4ecd6

@ -356,3 +356,25 @@ func AdminGetFile(c *gin.Context) {
c.JSON(200, ErrorResponse(err))
}
}
// AdminDeleteFile 批量删除文件
func AdminDeleteFile(c *gin.Context) {
var service admin.FileBatchService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Delete(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminListShare 列出分享
func AdminListShare(c *gin.Context) {
var service admin.AdminListService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Shares()
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}

@ -385,6 +385,14 @@ func InitMasterRouter() *gin.Engine {
file.POST("list", controllers.AdminListFile)
// 预览文件
file.GET("preview/:id", controllers.AdminGetFile)
// 删除
file.POST("delete", controllers.AdminDeleteFile)
}
share := admin.Group("share")
{
// 列出分享
share.POST("list", controllers.AdminListShare)
}
}

@ -3,6 +3,7 @@ package admin
import (
"context"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/service/explorer"
@ -15,6 +16,58 @@ type FileService struct {
ID uint `uri:"id" json:"id" binding:"required"`
}
// FileBatchService 文件批量操作服务
type FileBatchService struct {
ID []uint `json:"id" binding:"min=1"`
}
// Delete 删除文件
func (service *FileBatchService) Delete(c *gin.Context) serializer.Response {
files, err := model.GetFilesByIDs(service.ID, 0)
if err != nil {
return serializer.DBErr("无法列出待删除文件", err)
}
// 根据用户分组
userFile := make(map[uint][]model.File)
for i := 0; i < len(files); i++ {
if _, ok := userFile[files[i].UserID]; !ok {
userFile[files[i].UserID] = []model.File{}
}
userFile[files[i].UserID] = append(userFile[files[i].UserID], files[i])
}
// 异步执行删除
go func(files map[uint][]model.File) {
for uid, file := range files {
user, err := model.GetUserByID(uid)
if err != nil {
continue
}
fs, err := filesystem.NewFileSystem(&user)
if err != nil {
fs.Recycle()
continue
}
// 汇总文件ID
ids := make([]uint, 0, len(file))
for i := 0; i < len(file); i++ {
ids = append(ids, file[i].ID)
}
// 执行删除
fs.Delete(context.Background(), []uint{}, ids)
fs.Recycle()
}
}(userFile)
// 分组执行删除
return serializer.Response{}
}
// Get 预览文件
func (service *FileService) Get(c *gin.Context) serializer.Response {
file, err := model.GetFilesByIDs([]uint{service.ID}, 0)

Loading…
Cancel
Save