123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package controller
- import (
- "cold-logistics/common/file_store"
- "cold-logistics/conf"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "gogs.baozhida.cn/zoie/OAuth-core/api"
- "os"
- "path/filepath"
- "strings"
- "time"
- )
- type UploadController struct {
- api.Api
- }
- // FilesUpload 文件上传
- // @Summary 文件上传
- // @Description 文件上传
- // @Tags 工具
- // @Success 200 {object} response.Response{data=string} "{"code": 200, "data": [...]}"
- // @Router /api/upload [get]
- func (e UploadController) FilesUpload(c *gin.Context) {
- err := e.MakeContext(c).Errors
- if err != nil {
- e.Error(500, err, "服务初始化失败!")
- return
- }
- // 处理文件上传逻辑
- file, err := c.FormFile("file")
- uploadFileName := file.Filename // 获取文件名
- fileType := filepath.Ext(uploadFileName) // 获取文件类型(扩展名)
- if err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- filePath := file_store.GetCurrentDirectory() + "/uploads/" + uploadFileName
- err = c.SaveUploadedFile(file, filePath)
- if err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- defer func() {
- os.Remove(filePath)
- }()
- filename := fmt.Sprintf("%s/%s%s", time.Now().Format("2006-01-02"), strings.Replace(uuid.New().String(), "-", "", -1), fileType)
- err = file_store.QiniuFileStore.UpLoad(filename, filePath)
- if err != nil {
- c.JSON(400, gin.H{"error": "文件上传失败"})
- return
- }
- e.Custom(gin.H{
- "code": 200,
- "data": conf.ExtConfig.Qiniu.Endpoint + filename,
- "msg": "success",
- })
- }
- // ApkUpload 文件上传
- // @Summary 文件上传
- // @Description 文件上传
- // @Tags 工具
- // @Success 200 {object} response.Response{data=string,id=string,msg=string} "{"code": 200, "data": [...]}"
- // @Router /api/apk-upload [get]
- func (e UploadController) ApkUpload(c *gin.Context) {
- err := e.MakeContext(c).Errors
- if err != nil {
- e.Error(500, err, "服务初始化失败!")
- return
- }
- // 处理文件上传逻辑
- file, err := c.FormFile("file")
- uploadFileName := file.Filename // 获取文件名
- fileType := filepath.Ext(uploadFileName) // 获取文件类型(扩展名)
- if err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- if fileType != ".apk" {
- c.JSON(500, gin.H{"code": 200,
- "data": conf.ExtConfig.Apk.Path + "/" + uploadFileName,
- "msg": "success"})
- return
- }
- filePath := conf.ExtConfig.Apk.Path + "/" + uploadFileName
- err = c.SaveUploadedFile(file, filePath)
- if err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- e.Custom(gin.H{
- "code": 200,
- "data": conf.ExtConfig.Apk.Path + "/" + uploadFileName,
- "msg": "success",
- })
- }
|