266 lines
7.2 KiB
Go
266 lines
7.2 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
"strconv"
|
|
)
|
|
|
|
func registerMediaRoutes(group *gin.RouterGroup, svc *service.MediaService) {
|
|
files := group.Group("/fileUploadAndDownload")
|
|
files.POST("/upload", func(c *gin.Context) {
|
|
claims := currentClaims(c)
|
|
header, err := c.FormFile("file")
|
|
if claims == nil || err != nil {
|
|
fail(c, "接收文件失败")
|
|
return
|
|
}
|
|
opened, err := header.Open()
|
|
if err != nil {
|
|
fail(c, "读取文件失败")
|
|
return
|
|
}
|
|
defer opened.Close()
|
|
category, _ := strconv.Atoi(c.DefaultPostForm("classId", "0"))
|
|
save := c.DefaultQuery("noSave", "0") == "0"
|
|
item, err := svc.Upload(c.Request.Context(), claims.ID, header.Filename, header.Header.Get("Content-Type"), category, opened, save)
|
|
if err != nil {
|
|
fail(c, "上传失败:"+err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"file": item}, "上传成功")
|
|
})
|
|
files.POST("/getFileList", func(c *gin.Context) {
|
|
var req struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Keyword string `json:"keyword"`
|
|
ClassID int `json:"classId"`
|
|
Tag string `json:"tag"`
|
|
UserID uint `json:"userId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := svc.MediaList(c.Request.Context(), req.Page, req.PageSize, req.Keyword, req.ClassID, req.Tag, req.UserID)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
})
|
|
files.POST("/deleteFile", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.Delete(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
files.POST("/deleteFiles", func(c *gin.Context) {
|
|
var req struct {
|
|
IDs []uint `json:"ids"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
failed := []uint{}
|
|
for _, id := range req.IDs {
|
|
if svc.Delete(c.Request.Context(), id) != nil {
|
|
failed = append(failed, id)
|
|
}
|
|
}
|
|
if len(failed) > 0 {
|
|
writeResult(c, codeSuccess, gin.H{"failedIds": failed}, "部分文件删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
files.GET("/findFile", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
item, err := svc.Media(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, item, "获取成功")
|
|
})
|
|
files.POST("/editFileName", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
Name string `json:"name"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.Rename(c.Request.Context(), req.ID, req.Name); err != nil {
|
|
fail(c, "编辑失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
files.POST("/importURL", func(c *gin.Context) {
|
|
var req []struct {
|
|
Name string `json:"name"`
|
|
ClassID int `json:"classId"`
|
|
URL string `json:"url"`
|
|
Tag string `json:"tag"`
|
|
Key string `json:"key"`
|
|
Size int64 `json:"size"`
|
|
Mime string `json:"mime"`
|
|
MD5 string `json:"md5"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
claims := currentClaims(c)
|
|
items := make([]*biz.MediaFile, 0, len(req))
|
|
for _, v := range req {
|
|
items = append(items, &biz.MediaFile{Name: v.Name, CategoryID: v.ClassID, URL: v.URL, Tag: v.Tag, Key: v.Key, Size: v.Size, Mime: v.Mime, MD5: v.MD5, UserID: claims.ID})
|
|
}
|
|
if err := svc.ImportURLs(c.Request.Context(), items); err != nil {
|
|
fail(c, "导入失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
files.POST("/listOssFiles", func(c *gin.Context) {
|
|
var req struct {
|
|
Prefix string `json:"prefix"`
|
|
Cursor string `json:"cursor"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, next, more, err := svc.Storage(c.Request.Context(), req.Prefix, req.Cursor, req.Limit)
|
|
if err != nil {
|
|
fail(c, "列举失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"list": items, "nextCursor": next, "hasMore": more}, "获取成功")
|
|
})
|
|
categories := group.Group("/attachmentCategory")
|
|
categories.GET("/getCategoryList", func(c *gin.Context) {
|
|
items, err := svc.Categories(c.Request.Context())
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, items, "获取成功")
|
|
})
|
|
categories.POST("/addCategory", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
Name string `json:"name"`
|
|
PID uint `json:"pid"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil || req.Name == "" {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SaveCategory(c.Request.Context(), &biz.AttachmentCategory{ID: req.ID, Name: req.Name, ParentID: req.PID}); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
categories.POST("/deleteCategory", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteCategory(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
upload := group.Group("/mediaUpload")
|
|
upload.POST("/init", func(c *gin.Context) {
|
|
claims := currentClaims(c)
|
|
var req struct {
|
|
FileName string `json:"fileName"`
|
|
FileHash string `json:"fileHash"`
|
|
FileSize int64 `json:"fileSize"`
|
|
ChunkSize int64 `json:"chunkSize"`
|
|
ChunkTotal int `json:"chunkTotal"`
|
|
}
|
|
if claims == nil || c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
result, err := svc.InitUpload(c.Request.Context(), claims.ID, req.FileName, req.FileHash, req.FileSize, req.ChunkSize, req.ChunkTotal)
|
|
if err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, result, "获取成功")
|
|
})
|
|
upload.POST("/chunk", func(c *gin.Context) {
|
|
claims := currentClaims(c)
|
|
uploadID, _ := strconv.ParseUint(c.PostForm("uploadId"), 10, 64)
|
|
index, _ := strconv.Atoi(c.PostForm("chunkIndex"))
|
|
header, err := c.FormFile("chunk")
|
|
if claims == nil || err != nil {
|
|
fail(c, "接收分片失败")
|
|
return
|
|
}
|
|
opened, err := header.Open()
|
|
if err != nil {
|
|
fail(c, "读取分片失败")
|
|
return
|
|
}
|
|
defer opened.Close()
|
|
if err = svc.SaveChunk(c.Request.Context(), claims.ID, uint(uploadID), index, c.PostForm("chunkHash"), opened); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
upload.POST("/complete", func(c *gin.Context) {
|
|
claims := currentClaims(c)
|
|
var req struct {
|
|
UploadID uint `json:"uploadId"`
|
|
}
|
|
if claims == nil || c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
item, err := svc.CompleteUpload(c.Request.Context(), claims.ID, req.UploadID)
|
|
if err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"media": item}, "上传成功")
|
|
})
|
|
upload.DELETE("/:uploadId", func(c *gin.Context) {
|
|
claims := currentClaims(c)
|
|
id, _ := strconv.ParseUint(c.Param("uploadId"), 10, 64)
|
|
if claims == nil {
|
|
fail(c, "未登录")
|
|
return
|
|
}
|
|
if err := svc.CancelUpload(c.Request.Context(), claims.ID, uint(id)); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|