You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.3 KiB
105 lines
3.3 KiB
package fileuploaddownload
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"time"
|
|
|
|
"gin_server_admin/commonus"
|
|
"gin_server_admin/model/common/response"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type FileUploadDownload struct{}
|
|
|
|
type urlData struct {
|
|
Type int `json:"type"`
|
|
Sign string `json:"sign"`
|
|
}
|
|
|
|
// 上传文件(远程)
|
|
func (f *FileUploadDownload) LongRangeFileUpload(c *gin.Context) {
|
|
_, fileHeader, fileErr := c.Request.FormFile("file")
|
|
typePostForm := c.PostForm("type")
|
|
|
|
// fmt.Printf("FileSize--------->%v\n", fileHeader.Size)
|
|
|
|
var urlData urlData
|
|
tageExt := path.Ext(fileHeader.Filename)
|
|
// fmt.Printf("Type------------------->%v\n", typePostForm)
|
|
if urlData.Type == 0 {
|
|
if typePostForm != "" {
|
|
typePostFormInt, _ := strconv.Atoi(typePostForm)
|
|
if typePostFormInt != 0 {
|
|
urlData.Type = typePostFormInt
|
|
} else {
|
|
urlData.Type = commonus.JudgeUpFileType(tageExt)
|
|
}
|
|
} else {
|
|
// urlData.Type = 1
|
|
urlData.Type = commonus.JudgeUpFileType(tageExt)
|
|
}
|
|
|
|
}
|
|
// fmt.Printf("Type------------------->%v\n", urlData.Type)
|
|
if fileErr != nil {
|
|
response.Result(101, fileHeader, "获取文件错误!", c)
|
|
return
|
|
}
|
|
|
|
// fileNameAry := strings.Split(fileHeader.Filename, ".") //拆分文件名称,获取类型及文件原名
|
|
// if len(fileNameAry) < 2 {
|
|
// response.Result(0, fileHeader, "文件上传错误!", c)
|
|
// return
|
|
// }
|
|
|
|
transitionFileName := fmt.Sprintf("%v%v", commonus.GetFileNumberEs(), tageExt)
|
|
// fmt.Printf("转接------------>%v\n", transitionFileName)
|
|
// fmt.Printf("文件名长度------------>%v\n", len(fileHeader.Filename))
|
|
// localFileSave := "uploads/" + fileHeader.Filename
|
|
localFileSave := fmt.Sprintf("uploads/%v", transitionFileName)
|
|
// fmt.Printf("文件保存地址--1111->%v\n", localFileSave)
|
|
c.SaveUploadedFile(fileHeader, localFileSave)
|
|
// filePath := "G:/goobject/src/git_public/gin-vue-admin/gin_server_admin/go.mod"
|
|
// strinUrl := "http://docu.hxgk.net/uploadfileing/uploadimging"
|
|
// fmt.Printf("文件保存地址--->%v\n", localFileSave)
|
|
str, _ := os.Getwd()
|
|
filePath := str + "/" + localFileSave //文件转接物理地址
|
|
|
|
// fmt.Printf("文件物理保存地址--->%v\n", filePath)
|
|
|
|
outPut := commonus.MapOut()
|
|
|
|
// outPut["fileAttr"] = fileAttr
|
|
|
|
fileCallBack := postFormDataWithSingleFile(filePath, urlData.Type)
|
|
// outPut["msg"] = urlData
|
|
if fileCallBack.Code != 0 && fileCallBack.Code != 1 {
|
|
response.Result(102, fileCallBack, fileCallBack.Msg, c)
|
|
return
|
|
}
|
|
|
|
// var fileAttr FileAttribute
|
|
//获取文件基础属性
|
|
|
|
var upLoadFileStruct UpLoadFileStruct
|
|
upLoadFileStruct.ID = commonus.GetFileNumberEs()
|
|
upLoadFileStruct.CreatedAt = time.Now()
|
|
upLoadFileStruct.UpdatedAt = time.Now()
|
|
upLoadFileStruct.Key = fileCallBack.Data.NewName
|
|
upLoadFileStruct.Name = fileHeader.Filename
|
|
upLoadFileStruct.Tag = tageExt
|
|
upLoadFileStruct.Url = fileCallBack.Data.Callbackurl
|
|
upLoadFileStruct.PhysicsPath = fileCallBack.Data.Physicspath
|
|
upLoadFileStruct.FileSize = fileCallBack.Data.GetSize
|
|
upLoadFileStruct.Type = urlData.Type
|
|
upLoadFileStruct.Size = commonus.FormatFileSize(fileCallBack.Data.GetSize)
|
|
// outPut["CallBackFileIng"] = postFormDataWithSingleFile(filePath, urlData.Type)
|
|
|
|
os.RemoveAll(filePath) //删除转接文件
|
|
outPut["file"] = upLoadFileStruct
|
|
// fmt.Printf("返回值====》%v\n", outPut)
|
|
response.Result(0, upLoadFileStruct, fileCallBack.Msg, c)
|
|
}
|
|
|