Skip to content

Commit

Permalink
#LSITE-3 #LSITE-4 添加跨域中间件 删除文件名md5加密
Browse files Browse the repository at this point in the history
  • Loading branch information
i-curve committed Sep 26, 2022
1 parent 3d496a7 commit 77867dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion handle/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "路径不能为空"})
}
Expand Down
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// 版本信息
var version = "1.0.1"
var version = "1.1.0"

func init() {
config.Init()
Expand All @@ -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})
Expand All @@ -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) // 文件删除
Expand Down
17 changes: 4 additions & 13 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util

import (
"crypto/md5"
"encoding/hex"
"errors"
"filesystem/config"
"filesystem/model"
Expand All @@ -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)
Expand All @@ -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, "原始文件不存在")
Expand All @@ -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, "文件不存在")
}
Expand Down

0 comments on commit 77867dd

Please sign in to comment.