67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"SciencesServer/config"
|
|
"SciencesServer/lib"
|
|
"SciencesServer/utils"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Upload struct{}
|
|
|
|
/**
|
|
* @apiDefine Upload 上传管理
|
|
*/
|
|
|
|
/**
|
|
* @api {post} /api/upload 上传接口
|
|
* @apiVersion 1.0.0
|
|
* @apiName Upload
|
|
* @apiGroup Upload
|
|
*
|
|
* @apiHeader {string} x-token token
|
|
*
|
|
* @apiParam {File} file 文件信息
|
|
*
|
|
* @apiSuccess (200) {Number} code 成功响应状态码!
|
|
* @apiSuccess (200) {String} msg 成功提示
|
|
* @apiSuccess (200) {Object} data 具体信息
|
|
* @apiSuccess (200) {Number} data.url 文件访问地址
|
|
* @apiSuccess (200) {String} data.filepath 文件地址
|
|
* @apiSuccess (200) {String} data.filename 文件名称
|
|
*
|
|
* @apiSuccessExample {json} Success response:
|
|
* HTTPS 200 OK
|
|
* {
|
|
* "code": 200
|
|
* "msg": "ok"
|
|
* "data":{
|
|
* "url": "http://192.168.99.185:8010/upload/20210401/ad228811386cb8cd089a9d668d2885cd.png",
|
|
* "filepath": "/upload/20210401/ad228811386cb8cd089a9d668d2885cd.png",
|
|
* "filename": "8251863448d7ed13393bf0aae2211272.jpg"
|
|
* }
|
|
* }
|
|
*/
|
|
func (a *Upload) Upload(c *gin.Context) {
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
APIFailure(err)(c)
|
|
return
|
|
}
|
|
resp := new(lib.UploadHandle)
|
|
|
|
if resp, err = lib.Upload(config.SystemConfig[config.UploadPath], strings.Split(config.SystemConfig[config.UploadExt], ","),
|
|
utils.StringToInt64(config.SystemConfig[config.UploadSize]), true).Handle()(file, config.SystemConfig[config.SysImageDomain]); err != nil {
|
|
APIFailure(err)(c)
|
|
return
|
|
}
|
|
if err = c.SaveUploadedFile(file, resp.RelativePath); err != nil {
|
|
APIFailure(err)(c)
|
|
return
|
|
}
|
|
APISuccess(resp)(c)
|
|
}
|