From 212b3c217c1701c55d14aed20fe0a25b60628233 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Mon, 18 Apr 2022 09:24:21 +0800 Subject: [PATCH] =?UTF-8?q?fix-1913=20=E6=8F=90=E4=BA=A4=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/cloudbrain.go | 2 ++ models/cloudbrain_image.go | 4 +-- modules/auth/cloudbrain.go | 10 ++++++++ modules/cloudbrain/resty.go | 41 +++++++++++++++++++++++++++++- routers/admin/cloudbrains.go | 7 ++++++ routers/repo/cloudbrain.go | 49 ++++++++++++++++++++++++++++++++++++ routers/routes/routes.go | 2 ++ 7 files changed, 112 insertions(+), 3 deletions(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 0465faf9a..44b177471 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -580,6 +580,8 @@ type CommitImageParams struct { Topics []string CloudBrainType int UID int64 + Place string + Type int } type CommitImageResult struct { diff --git a/models/cloudbrain_image.go b/models/cloudbrain_image.go index c88db0f67..eb21e0d87 100644 --- a/models/cloudbrain_image.go +++ b/models/cloudbrain_image.go @@ -567,12 +567,12 @@ func isImageStaring(e Engine, userID, imageID int64) bool { } func RecommendImage(imageId int64, recommond bool) error { - image := Image{Type: getRecommondType(recommond)} + image := Image{Type: GetRecommondType(recommond)} _, err := x.ID(imageId).Cols("type").Update(image) return err } -func getRecommondType(recommond bool) int { +func GetRecommondType(recommond bool) int { if recommond { return RECOMMOND_TYPE diff --git a/modules/auth/cloudbrain.go b/modules/auth/cloudbrain.go index 85f3a2127..e5be38084 100755 --- a/modules/auth/cloudbrain.go +++ b/modules/auth/cloudbrain.go @@ -33,6 +33,16 @@ type CommitImageCloudBrainForm struct { Topics string `form:"topics"` } +type CommitAdminImageCloudBrainForm struct { + Description string `form:"description" binding:"Required"` + Type int `form:"type" binding:"Required"` + Tag string `form:"tag" binding:"Required;MaxSize(100)" ` + IsPrivate bool `form:"isPrivate" binding:"Required"` + Topics string `form:"topics"` + Place string `form:"place" binding:"Required"` + IsRecommend bool `form:"isRecommend" binding:"Required"` +} + type EditImageCloudBrainForm struct { ID int64 `form:"id" binding:"Required"` Description string `form:"description" binding:"Required"` diff --git a/modules/cloudbrain/resty.go b/modules/cloudbrain/resty.go index f1f213bea..05f110856 100755 --- a/modules/cloudbrain/resty.go +++ b/modules/cloudbrain/resty.go @@ -312,12 +312,51 @@ sendjob: return nil }) if err == nil { - go updateImageStatus(image, isSetCreatedUnix, createTime) } return err } +func CommitAdminImage(jobID string, params models.CommitImageParams) error { + + exist, err := models.IsImageExist(params.ImageTag) + + if err != nil { + return fmt.Errorf("resty CommitImage: %v", err) + } + if exist { + return models.ErrorImageTagExist{ + Tag: params.ImageTag, + } + } + + image := models.Image{ + CloudbrainType: params.CloudBrainType, + UID: params.UID, + IsPrivate: params.IsPrivate, + Tag: params.ImageTag, + Description: params.ImageDescription, + Place: params.Place, + Status: models.IMAGE_STATUS_SUCCESS, + Type: params.Type, + } + + err = models.WithTx(func(ctx models.DBContext) error { + + if err := models.CreateLocalImage(&image); err != nil { + log.Error("Failed to insert image record.", err) + return fmt.Errorf("resty CommitImage: %v", err) + } + + if err := models.SaveImageTopics(image.ID, params.Topics...); err != nil { + log.Error("Failed to insert image record.", err) + return fmt.Errorf("resty CommitImage: %v", err) + } + return nil + }) + return err +} + func updateImageStatus(image models.Image, isSetCreatedUnix bool, createTime time.Time) { attemps := 5 commitSuccess := false diff --git a/routers/admin/cloudbrains.go b/routers/admin/cloudbrains.go index 91d866093..5ad1246ff 100755 --- a/routers/admin/cloudbrains.go +++ b/routers/admin/cloudbrains.go @@ -21,6 +21,7 @@ import ( const ( tplCloudBrains base.TplName = "admin/cloudbrain/list" tplImages base.TplName = "admin/cloudbrain/images" + tplCommitImages base.TplName = "admin/cloudbrain/imagecommit" EXCEL_DATE_FORMAT = "20060102150405" CREATE_TIME_FORMAT = "2006/01/02 15:04:05" ) @@ -114,6 +115,12 @@ func Images(ctx *context.Context) { } +func CloudBrainCommitImageShow(ctx *context.Context) { + ctx.Data["PageIsAdminImages"] = true + ctx.HTML(200, tplImages) + +} + func DownloadCloudBrains(ctx *context.Context) { page := 1 diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 5daf96e78..c2eaf1468 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -702,6 +702,55 @@ func CloudBrainCommitImageCheck(ctx *context.Context, form auth.CommitImageCloud } +func CloudBrainAdminCommitImage(ctx *context.Context, form auth.CommitAdminImageCloudBrainForm) { + + if !NamePattern.MatchString(form.Tag) { + ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err"))) + return + } + + if utf8.RuneCountInString(form.Description) > 255 { + ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err", 255))) + return + } + + validTopics, errMessage := checkTopics(form.Topics) + if errMessage != "" { + ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr(errMessage))) + return + } + + err := cloudbrain.CommitAdminImage(ctx.Cloudbrain.JobID, models.CommitImageParams{ + CommitImageCloudBrainParams: models.CommitImageCloudBrainParams{ + Ip: ctx.Cloudbrain.ContainerIp, + TaskContainerId: ctx.Cloudbrain.ContainerID, + ImageDescription: form.Description, + ImageTag: form.Tag, + }, + IsPrivate: form.IsPrivate, + CloudBrainType: form.Type, + Topics: validTopics, + UID: ctx.User.ID, + Type: models.GetRecommondType(form.IsRecommend), + Place: form.Place, + }) + if err != nil { + log.Error("CommitImage(%s) failed:%v", ctx.Cloudbrain.JobName, err.Error(), ctx.Data["msgID"]) + if models.IsErrImageTagExist(err) { + ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_exist"))) + + } else if models.IsErrorImageCommitting(err) { + ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_committing"))) + } else { + ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_commit_fail"))) + } + + return + } + + ctx.JSON(200, models.BaseOKMessage) +} + func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { if !NamePattern.MatchString(form.Tag) { diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 9df429e8b..cf1a18b31 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -534,6 +534,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/images", func() { m.Get("", admin.Images) m.Get("/data", repo.GetAllImages) + m.Get("/commit_image", admin.CloudBrainCommitImageShow) + m.Post("/commit_image", bindIgnErr(auth.CommitAdminImageCloudBrainForm{}), repo.CloudBrainAdminCommitImage) }) m.Put("/image/:id/action/:action", image.Action)