美文网首页
改造sgfs开源项目,使之符合bifangback的文件上传下载

改造sgfs开源项目,使之符合bifangback的文件上传下载

作者: 万州客 | 来源:发表于2021-01-08 10:24 被阅读0次

    在构架中,远程文件服务器,是为了解耦编译软件包和salt之间的部署关联。sgfs是一个简易的文件上传下载开源服务器,连nginx都节省了。但其原生的功能在上传的同时,增加了更改文件名,防止命令冲突,增加日期目录这些功能,用于bifangback是不合适的。所以改造一下,把功能收缩。

    项目地址:
    https://github.com/LinkinStars/sgfs

    一,部署

    官网有两进制下载,不提。
    使用startup.sh脚本即可启动。

    二,上传

    1,不指定子目录,file是相对绝对目录均可

    curl -F "file=@ppp.sql" -F "token=654321" http://1.2.3.4:9001/upload-file
    返回值

    {"code":1,"message":"Save file success.","data":"//2021-01-08/20210108100512_3019917070.sql"}
    

    2,指定子目录

    curl -F "file=@/docker/p.sql" -F "token=654321" -F "uploadSubPath=/5/3/" http://1.2.3.4:9001/upload-file

    三,访问

    http://1.2.3.4:9002

    # /
    
    *   [2021-01-08](http://1.2.3.4:9002/2021-01-08), dir, last modified 2021-01-08 02:05:12 +0000 UTC
    *   [555](http://1.2.3.49002/555), dir, last modified 2021-01-08 02:02:58 +0000 UTC
    *   [ab](http://1.2.3.4:9002/ab), dir, last modified 2021-01-08 02:02:23 +0000 UTC
    *   [abc](http://1.2.3.4:9002/abc), dir, last modified 2021-01-08 01:07:33 +0000 UTC
    *   [pic](http://1.2.3.4:9002/pic), dir, last modified 2021-01-08 01:06:58 +0000 UTC
    
    

    四,修改源代码

    只修改项目目录下service/upload.go文件,已有注释

    package service
    
    import (
        "path"
        "strings"
    
        "github.com/LinkinStars/golang-util/gu"
        "github.com/valyala/fasthttp"
        "go.uber.org/zap"
    
        "github.com/LinkinStars/sgfs/config"
        "github.com/LinkinStars/sgfs/util/date_util"
    )
    
    func UploadFileHandler(ctx *fasthttp.RequestCtx) {
        // Get the file from the form
        header, err := ctx.FormFile("file")
        if err != nil {
            SendResponse(ctx, -1, "No file was found.", nil)
            return
        }
    
        // Check File Size
        if header.Size > int64(config.GlobalConfig.MaxUploadSize) {
            SendResponse(ctx, -1, "File size exceeds limit.", nil)
            return
        }
    
        // authentication
        token := string(ctx.FormValue("token"))
        if strings.Compare(token, config.GlobalConfig.OperationToken) != 0 {
            SendResponse(ctx, -1, "Token error.", nil)
            return
        }
    
        // Check upload File Path
        uploadSubPath := string(ctx.FormValue("uploadSubPath"))
        // 注释掉, 之前加了日期目录
        // visitPath := "/" + uploadSubPath + "/" + date_util.GetCurTimeFormat(date_util.YYYYMMDD)
        visitPath := "/" + uploadSubPath
        dirPath := config.GlobalConfig.UploadPath + visitPath
        if err := gu.CreateDirIfNotExist(dirPath); err != nil {
            zap.S().Error(err)
            SendResponse(ctx, -1, "Failed to create folder.", nil)
            return
        }
        // 注释掉,不要取什么后缀,直接取文件名
        // suffix := path.Ext(header.Filename)
        // filename := createFileName(suffix)
        filename := header.Filename
    
        fileAllPath := dirPath + "/" + filename
    
        /*
            注释掉,有同名,就报错
            // Guarantee that the filename does not duplicate
            for {
                if !gu.CheckPathIfNotExist(fileAllPath) {
                    break
                }
                filename = createFileName(suffix)
                fileAllPath = dirPath + "/" + filename
            }
        */
    
        // Save file
        if err := fasthttp.SaveMultipartFile(header, fileAllPath); err != nil {
            zap.S().Error(err)
            SendResponse(ctx, -1, "Save file fail.", err.Error())
        }
    
        SendResponse(ctx, 1, "Save file success.", visitPath+"/"+filename)
        return
    }
    
    /*
    注释掉,不需要重命名文件
    func createFileName(suffix string) string {
        // Date and Time + _ + Random Number + File Suffix
        return date_util.GetCurTimeFormat(date_util.YYYYMMddHHmmss) + "_" + gu.GenerateRandomNumber(10) + suffix
    }
    */
    
    

    相关文章

      网友评论

          本文标题:改造sgfs开源项目,使之符合bifangback的文件上传下载

          本文链接:https://www.haomeiwen.com/subject/vqbxaktx.html