From 77867dd3d075a3067975091614861c1ca90ff44e Mon Sep 17 00:00:00 2001 From: i-curve <2517094646@qq.com> Date: Mon, 26 Sep 2022 17:12:32 +0800 Subject: [PATCH] =?UTF-8?q?#LSITE-3=20#LSITE-4=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F=E4=B8=AD=E9=97=B4=E4=BB=B6=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=96=87=E4=BB=B6=E5=90=8Dmd5=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/filesystem.go | 2 +- main.go | 24 ++++++++++++++++++++++-- util/util.go | 17 ++++------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/handle/filesystem.go b/handle/filesystem.go index 5599a7d..eadd4f6 100644 --- a/handle/filesystem.go +++ b/handle/filesystem.go @@ -28,7 +28,7 @@ func GetFile(ctx *gin.Context) { var base string if request.ShortURL != "" { base = request.ShortURL - ctx.JSON(http.StatusOK, gin.H{"code": 200, "url": path.Join(config.BaseURL, util.PathToUrl(base))}) + ctx.JSON(http.StatusOK, gin.H{"code": 200, "url": path.Join(config.BaseURL, base)}) } else { ctx.JSON(http.StatusOK, gin.H{"code": 400, "err": "路径不能为空", "msg": "路径不能为空"}) } diff --git a/main.go b/main.go index 600d20b..92be09b 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( ) // 版本信息 -var version = "1.0.1" +var version = "1.1.0" func init() { config.Init() @@ -19,6 +19,26 @@ func main() { gin.SetMode("release") router := gin.Default() + // 跨域中间件 + router.Use(func() gin.HandlerFunc { + return func(c *gin.Context) { + method := c.Request.Method + origin := c.Request.Header.Get("Origin") + if origin != "" { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") + c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization") + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type") + c.Header("Access-Control-Allow-Credentials", "true") + } + + if method == "OPTIONS" { + c.AbortWithStatus(http.StatusNoContent) + } + c.Next() + } + }()) + // 获取版本信息 router.GET("/version", func(ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{"version": version}) @@ -32,7 +52,7 @@ func main() { routes := router.Group("", handle.Middleware) { routes.POST("/upload", handle.UploadFile) // 文件上传 - routes.GET("/file", handle.GetFile) // 获取文件url + routes.GET("/file", handle.GetFile) // 获取文件 routes.POST("/copy", handle.CopyFile) // 文件复制 routes.POST("/move", handle.MoveFile) // 文件转移 routes.DELETE("/file", handle.DeleteFile) // 文件删除 diff --git a/util/util.go b/util/util.go index 091ff72..4aafc98 100644 --- a/util/util.go +++ b/util/util.go @@ -1,8 +1,6 @@ package util import ( - "crypto/md5" - "encoding/hex" "errors" "filesystem/config" "filesystem/model" @@ -15,20 +13,13 @@ import ( "github.com/i-curve/coding" ) -func PathToUrl(short_url string) string { - filename := path.Base(short_url) - hash := md5.New() - hash.Write([]byte(filename)) - return hex.EncodeToString(hash.Sum(nil)) + path.Ext(short_url) -} - // 文件上传 func WriteFile(base string, file *multipart.FileHeader) (*model.Reply, coding.Code) { if file == nil { return nil, coding.New(coding.StatusOK, 400, "上传文件不能为空") } base = TernaryExpr(base != "", base, file.Filename) - filename := PathToUrl(base) + filename := base f, _ := file.Open() bys, _ := io.ReadAll(f) err := os.WriteFile("/data/"+filename, bys, 0666) @@ -37,8 +28,8 @@ func WriteFile(base string, file *multipart.FileHeader) (*model.Reply, coding.Co // 复制文件 func CopyFile(oldPath, newPath string) coding.Code { - oldFilename := "/data/" + PathToUrl(oldPath) - newFilename := "/data/" + PathToUrl(newPath) + oldFilename := "/data/" + oldPath + newFilename := "/data/" + newPath bys, err := os.ReadFile(oldFilename) if err != nil { coding.New(http.StatusOK, 400, "原始文件不存在") @@ -58,7 +49,7 @@ func MoveFile(oldPath, newPath string) coding.Code { // 文件删除 func DeleteFile(base string) coding.Code { - filename := "/data/" + PathToUrl(base) + filename := "/data/" + base if _, err := os.Stat(filename); err != nil { return coding.New(coding.StatusOK, 400, "文件不存在") }