mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 23:26:38 +08:00
[优化] 结构优化
This commit is contained in:
@@ -1,160 +0,0 @@
|
|||||||
package controller
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
|
||||||
"openflare/common"
|
|
||||||
"openflare/model"
|
|
||||||
"openflare/utils"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetAllFiles(c *gin.Context) {
|
|
||||||
p, _ := strconv.Atoi(c.Query("p"))
|
|
||||||
if p < 0 {
|
|
||||||
p = 0
|
|
||||||
}
|
|
||||||
files, err := model.GetAllFiles(p*common.ItemsPerPage, common.ItemsPerPage)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": files,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func SearchFiles(c *gin.Context) {
|
|
||||||
keyword := c.Query("keyword")
|
|
||||||
files, err := model.SearchFiles(keyword)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": files,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func UploadFile(c *gin.Context) {
|
|
||||||
form, err := c.MultipartForm()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
uploadPath := common.UploadPath
|
|
||||||
description := c.PostForm("description")
|
|
||||||
if description == "" {
|
|
||||||
description = "无描述信息"
|
|
||||||
}
|
|
||||||
uploader := c.GetString("username")
|
|
||||||
if uploader == "" {
|
|
||||||
uploader = "访客用户"
|
|
||||||
}
|
|
||||||
uploaderId := c.GetInt("id")
|
|
||||||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
|
||||||
files := form.File["file"]
|
|
||||||
for _, file := range files {
|
|
||||||
filename := filepath.Base(file.Filename)
|
|
||||||
ext := filepath.Ext(filename)
|
|
||||||
link := utils.GetUUID() + ext
|
|
||||||
savePath := filepath.Join(uploadPath, link) // both parts are checked, so this path should be safe to use
|
|
||||||
if err := c.SaveUploadedFile(file, savePath); err != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// save to database
|
|
||||||
fileObj := &model.File{
|
|
||||||
Description: description,
|
|
||||||
Uploader: uploader,
|
|
||||||
UploadTime: currentTime,
|
|
||||||
UploaderId: uploaderId,
|
|
||||||
Link: link,
|
|
||||||
Filename: filename,
|
|
||||||
}
|
|
||||||
err = fileObj.Insert()
|
|
||||||
if err != nil {
|
|
||||||
_ = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteFile(c *gin.Context) {
|
|
||||||
fileIdStr := c.Param("id")
|
|
||||||
fileId, err := strconv.Atoi(fileIdStr)
|
|
||||||
if err != nil || fileId == 0 {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fileObj := &model.File{
|
|
||||||
Id: fileId,
|
|
||||||
}
|
|
||||||
model.DB.Where("id = ?", fileId).First(&fileObj)
|
|
||||||
if fileObj.Link == "" {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"message": "文件不存在!",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = fileObj.Delete()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
message := "文件删除成功"
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
"message": message,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func DownloadFile(c *gin.Context) {
|
|
||||||
path := c.Param("file")
|
|
||||||
fullPath := filepath.Join(common.UploadPath, path)
|
|
||||||
if !strings.HasPrefix(fullPath, common.UploadPath) {
|
|
||||||
// We may being attacked!
|
|
||||||
c.Status(403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.File(fullPath)
|
|
||||||
// Update download counter
|
|
||||||
go func() {
|
|
||||||
model.UpdateDownloadCounter(path)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
@@ -85,14 +85,6 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
updateRoute.POST("/manual-upgrade", controller.ConfirmManualServerUpgrade)
|
updateRoute.POST("/manual-upgrade", controller.ConfirmManualServerUpgrade)
|
||||||
updateRoute.POST("/upgrade", controller.UpgradeServer)
|
updateRoute.POST("/upgrade", controller.UpgradeServer)
|
||||||
}
|
}
|
||||||
fileRoute := apiRouter.Group("/file")
|
|
||||||
fileRoute.Use(middleware.AdminAuth())
|
|
||||||
{
|
|
||||||
fileRoute.GET("/", controller.GetAllFiles)
|
|
||||||
fileRoute.GET("/search", controller.SearchFiles)
|
|
||||||
fileRoute.POST("/", middleware.UploadRateLimit(), controller.UploadFile)
|
|
||||||
fileRoute.POST("/:id/delete", controller.DeleteFile)
|
|
||||||
}
|
|
||||||
proxyRoute := apiRouter.Group("/proxy-routes")
|
proxyRoute := apiRouter.Group("/proxy-routes")
|
||||||
proxyRoute.Use(middleware.AdminAuth())
|
proxyRoute.Use(middleware.AdminAuth())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user