From c8e2c3620e6fcd08ee2bf48d46a43a5225d3a965 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 25 May 2026 16:12:22 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openflare_server/controller/file.go | 160 -------------------------- openflare_server/router/api-router.go | 8 -- 2 files changed, 168 deletions(-) delete mode 100644 openflare_server/controller/file.go diff --git a/openflare_server/controller/file.go b/openflare_server/controller/file.go deleted file mode 100644 index 371fe381..00000000 --- a/openflare_server/controller/file.go +++ /dev/null @@ -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) - }() -} diff --git a/openflare_server/router/api-router.go b/openflare_server/router/api-router.go index 9ac9b389..25e4a09e 100644 --- a/openflare_server/router/api-router.go +++ b/openflare_server/router/api-router.go @@ -85,14 +85,6 @@ func SetApiRouter(router *gin.Engine) { updateRoute.POST("/manual-upgrade", controller.ConfirmManualServerUpgrade) 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.Use(middleware.AdminAuth()) {