407 lines
10 KiB
Go
407 lines
10 KiB
Go
package apis
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/gin-gonic/gin"
|
||
json "github.com/json-iterator/go"
|
||
"gitlink.org.cn/JointCloud/pcm-openi/common"
|
||
"gitlink.org.cn/JointCloud/pcm-openi/model"
|
||
"gitlink.org.cn/JointCloud/pcm-openi/service"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
type modelApi struct {
|
||
*Api
|
||
*service.ModelService
|
||
}
|
||
|
||
var ModelApi = modelApi{
|
||
Api: BaseApi,
|
||
ModelService: service.NewModelService(),
|
||
}
|
||
|
||
func CreateModel(ctx *gin.Context) {
|
||
var param model.CreateModelParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELCREATE
|
||
|
||
var resp model.CreateModel
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
_, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetFormData(map[string]string{
|
||
"jobId": param.JobId,
|
||
"name": param.Name,
|
||
"version": param.Version,
|
||
"modelSelectedFile": param.ModelSelectedFile,
|
||
}).
|
||
SetResult(&resp).
|
||
Post(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
if (model.CreateModel{} == resp) {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
if resp.Code != "0" {
|
||
model.Response(ctx, 500, common.INVOKEERROR, resp.Msg)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func GetModelById(ctx *gin.Context) {
|
||
var param model.GetByIdModelParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELGETBYID
|
||
|
||
var resp model.Model
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
_, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetQueryParam("id", param.Id).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetResult(&resp).
|
||
Get(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
if (model.Model{} == resp) {
|
||
model.Response(ctx, http.StatusOK, common.NOTFOUND, nil)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func DownloadById(ctx *gin.Context) {
|
||
var param model.DownloadByIdParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELDOWNLOADBYID
|
||
|
||
reqUrl = strings.Replace(reqUrl, "{username}", param.UserName, -1)
|
||
reqUrl = strings.Replace(reqUrl, "{reponame}", param.RepoName, -1)
|
||
|
||
var client http.Client
|
||
params := url.Values{}
|
||
params.Add("id", param.Id)
|
||
params.Add("access_token", token)
|
||
|
||
resp, err := client.Get(reqUrl + common.QUESTION_MARK + params.Encode())
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
if resp.StatusCode != http.StatusOK {
|
||
model.Response(ctx, resp.StatusCode, resp.Status, nil)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
ctx.Writer.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
|
||
ctx.Writer.Header().Set("Content-Disposition", resp.Header.Get("Content-Disposition"))
|
||
|
||
io.Copy(ctx.Writer, resp.Body)
|
||
}
|
||
|
||
func CreateLocalModel(ctx *gin.Context) {
|
||
var param model.CreateLocalModelParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELLOCALCREATE
|
||
|
||
var resp model.CreateLocalModel
|
||
|
||
var isPrivate string
|
||
if param.IsPrivate {
|
||
isPrivate = "true"
|
||
} else {
|
||
isPrivate = "false"
|
||
}
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
res, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetFormData(map[string]string{
|
||
"formdata": param.FormData,
|
||
"name": param.Name,
|
||
"version": param.Version,
|
||
"engine": strconv.Itoa(param.Engine),
|
||
"label": param.Label,
|
||
"isPrivate": isPrivate,
|
||
"description": param.Description,
|
||
"type": strconv.Itoa(param.Type),
|
||
}).
|
||
SetResult(&resp).
|
||
Post(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
if res != nil {
|
||
if res.StatusCode() != 200 {
|
||
errStr := json.Get(res.Body(), "message").ToString()
|
||
model.Response(ctx, 500, errStr, nil)
|
||
return
|
||
}
|
||
}
|
||
|
||
if resp.Code != "0" {
|
||
model.Response(ctx, 500, resp.Msg, nil)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func GetUploadedChunks(ctx *gin.Context) {
|
||
var param model.GetUploadedChunksParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELLOCALGETUPLOADEDCHUNKS
|
||
|
||
var resp model.GetUploadedChunks
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
_, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetQueryParam("md5", param.Md5).
|
||
SetQueryParam("file_name", param.FileName).
|
||
SetQueryParam("type", strconv.Itoa(param.Type)).
|
||
SetQueryParam("modeluuid", param.Modeluuid).
|
||
SetResult(&resp).
|
||
Get(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func NewMultipart(ctx *gin.Context) {
|
||
var param model.NewMultipartParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELLOCALNEWMULTIPART
|
||
|
||
var resp model.NewMultipart
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
_, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetQueryParam("totalChunkCounts", strconv.Itoa(param.TotalChunkCounts)).
|
||
SetQueryParam("type", strconv.Itoa(param.Type)).
|
||
SetQueryParam("size", strconv.Itoa(param.Size)).
|
||
SetQueryParam("file_name", param.File_name).
|
||
SetQueryParam("modeluuid", param.Modeluuid).
|
||
SetResult(&resp).
|
||
Get(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
if resp.Code != "0" {
|
||
model.Response(ctx, 500, resp.Msg, nil)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func GetMultipartUrl(ctx *gin.Context) {
|
||
var param model.GetMultipartUrlParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELLOCALGETMULTIPARTURL
|
||
|
||
var resp model.GetMultipartUrl
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
rp, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetQueryParam("uuid", param.Uuid).
|
||
SetQueryParam("uploadID", param.UploadID).
|
||
SetQueryParam("type", strconv.Itoa(param.Type)).
|
||
SetQueryParam("size", strconv.Itoa(param.Size)).
|
||
SetQueryParam("chunkNumber", strconv.Itoa(param.ChunkNumber)).
|
||
SetQueryParam("file_name", param.FileName).
|
||
SetQueryParam("modeluuid", param.Modeluuid).
|
||
SetResult(&resp).
|
||
Get(reqUrl)
|
||
|
||
fmt.Println(rp)
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func CompleteMultipart(ctx *gin.Context) {
|
||
var param model.CompleteMultipartParam
|
||
if err := ctx.BindJSON(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
||
return
|
||
}
|
||
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
reqUrl := common.OPENIPREFIX + common.MODELLOCALCOMPLETEMULTIPART
|
||
|
||
var resp model.CompleteMultipart
|
||
|
||
req := common.GetRestyRequest(common.TIMEOUT)
|
||
rp, err := req.
|
||
SetQueryParam(common.ACCESSTOKEN, token).
|
||
SetPathParam("username", param.UserName).
|
||
SetPathParam("reponame", param.RepoName).
|
||
SetFormData(map[string]string{
|
||
"uuid": param.Uuid,
|
||
"uploadID": param.UploadID,
|
||
"modeluuid": param.Modeluuid,
|
||
"type": strconv.Itoa(param.Type),
|
||
}).
|
||
SetResult(&resp).
|
||
Post(reqUrl)
|
||
|
||
if err != nil {
|
||
model.Response(ctx, 500, common.INVOKEERROR, err)
|
||
return
|
||
}
|
||
|
||
if rp.StatusCode() != 200 {
|
||
model.Response(ctx, rp.StatusCode(), rp.Status(), string(rp.Body()))
|
||
return
|
||
}
|
||
|
||
if resp.Code != "0" {
|
||
model.Response(ctx, rp.StatusCode(), rp.Status(), string(rp.Body()))
|
||
return
|
||
}
|
||
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
|
||
func (r modelApi) UploadFile2Model(ctx *gin.Context) {
|
||
// 设置最大请求体大小(100 个文件 * 512 MiB = 51.2 GiB)
|
||
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 100<<20*512)
|
||
|
||
// 解析 multipart form
|
||
form, err := ctx.MultipartForm()
|
||
if err != nil {
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "文件上传失败: " + err.Error()})
|
||
return
|
||
}
|
||
|
||
// 获取所有文件
|
||
files := form.File["files"]
|
||
|
||
// 检查文件数量
|
||
if len(files) > 100 {
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "单次最多上传 100 个文件"})
|
||
return
|
||
}
|
||
|
||
// 检查文件名长度和文件大小
|
||
for _, file := range files {
|
||
// 检查文件名长度
|
||
if len(file.Filename) > 128 {
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "文件名不能超过 128 个字符"})
|
||
return
|
||
}
|
||
|
||
// 检查单个文件大小(512 MiB)
|
||
if file.Size > 512<<20 {
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "单个文件不能超过 512 MiB"})
|
||
return
|
||
}
|
||
}
|
||
|
||
// 绑定表单参数到结构体
|
||
var param model.ModelUploadFileParam
|
||
if err := ctx.ShouldBind(¶m); err != nil {
|
||
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
||
return
|
||
}
|
||
|
||
// 获取 token
|
||
token := ctx.Query(common.ACCESSTOKEN)
|
||
|
||
// 调用服务层处理文件上传
|
||
if resp, err := r.ModelService.UploadFile(param, token, files); err != nil {
|
||
model.Response(ctx, http.StatusInternalServerError, err.Error(), resp)
|
||
} else {
|
||
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
||
}
|
||
}
|