| @@ -123,8 +123,9 @@ type GetImagesResult struct { | |||||
| } | } | ||||
| type GetImagesPayload struct { | type GetImagesPayload struct { | ||||
| Count int `json:"count"` | |||||
| ImageInfo []*ImageInfo `json:"rows"` | |||||
| Count int `json:"count"` | |||||
| TotalPages int `json:"totalPages,omitempty"` | |||||
| ImageInfo []*ImageInfo `json:"rows"` | |||||
| } | } | ||||
| type CloudbrainsOptions struct { | type CloudbrainsOptions struct { | ||||
| @@ -1,9 +1,11 @@ | |||||
| package cloudbrain | package cloudbrain | ||||
| import ( | import ( | ||||
| "code.gitea.io/gitea/modules/log" | |||||
| "encoding/json" | "encoding/json" | ||||
| "fmt" | "fmt" | ||||
| "strings" | |||||
| "code.gitea.io/gitea/modules/log" | |||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| "code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
| @@ -11,13 +13,16 @@ import ( | |||||
| ) | ) | ||||
| var ( | var ( | ||||
| restyClient *resty.Client | |||||
| HOST string | |||||
| TOKEN string | |||||
| restyClient *resty.Client | |||||
| HOST string | |||||
| TOKEN string | |||||
| ImagesUrlMap = map[string]string{Public: "/rest-server/api/v1/image/public/list/", Custom: "/rest-server/api/v1/image/list/"} | |||||
| ) | ) | ||||
| const ( | const ( | ||||
| JobHasBeenStopped = "S410" | JobHasBeenStopped = "S410" | ||||
| Public = "public" | |||||
| Custom = "custom" | |||||
| ) | ) | ||||
| func getRestyClient() *resty.Client { | func getRestyClient() *resty.Client { | ||||
| @@ -126,6 +131,16 @@ sendjob: | |||||
| } | } | ||||
| func GetImages() (*models.GetImagesResult, error) { | func GetImages() (*models.GetImagesResult, error) { | ||||
| return GetImagesPageable(1, 100, Custom, "") | |||||
| } | |||||
| func GetPublicImages() (*models.GetImagesResult, error) { | |||||
| return GetImagesPageable(1, 100, Public, "") | |||||
| } | |||||
| func GetImagesPageable(page int, size int, imageType string, name string) (*models.GetImagesResult, error) { | |||||
| checkSetting() | checkSetting() | ||||
| client := getRestyClient() | client := getRestyClient() | ||||
| var getImagesResult models.GetImagesResult | var getImagesResult models.GetImagesResult | ||||
| @@ -136,9 +151,9 @@ sendjob: | |||||
| res, err := client.R(). | res, err := client.R(). | ||||
| SetHeader("Content-Type", "application/json"). | SetHeader("Content-Type", "application/json"). | ||||
| SetAuthToken(TOKEN). | SetAuthToken(TOKEN). | ||||
| SetQueryString("pageIndex=1&pageSize=100"). | |||||
| SetQueryString(getQueryString(page, size, name)). | |||||
| SetResult(&getImagesResult). | SetResult(&getImagesResult). | ||||
| Get(HOST + "/rest-server/api/v1/image/list/") | |||||
| Get(HOST + ImagesUrlMap[imageType]) | |||||
| if err != nil { | if err != nil { | ||||
| return nil, fmt.Errorf("resty GetImages: %v", err) | return nil, fmt.Errorf("resty GetImages: %v", err) | ||||
| @@ -157,48 +172,30 @@ sendjob: | |||||
| goto sendjob | goto sendjob | ||||
| } | } | ||||
| if len(response.Code) != 0 { | |||||
| log.Error("getImagesResult failed(%s): %s", response.Code, response.Msg) | |||||
| return &getImagesResult, fmt.Errorf("getImagesResult failed(%s): %s", response.Code, response.Msg) | |||||
| } | |||||
| if getImagesResult.Code != Success { | if getImagesResult.Code != Success { | ||||
| return &getImagesResult, fmt.Errorf("getImagesResult err: %s", res.String()) | return &getImagesResult, fmt.Errorf("getImagesResult err: %s", res.String()) | ||||
| } | } | ||||
| getImagesResult.Payload.TotalPages = getTotalPages(getImagesResult, size) | |||||
| return &getImagesResult, nil | return &getImagesResult, nil | ||||
| } | } | ||||
| func GetPublicImages() (*models.GetImagesResult, error) { | |||||
| checkSetting() | |||||
| client := getRestyClient() | |||||
| var getImagesResult models.GetImagesResult | |||||
| retry := 0 | |||||
| sendjob: | |||||
| res, err := client.R(). | |||||
| SetHeader("Content-Type", "application/json"). | |||||
| SetAuthToken(TOKEN). | |||||
| SetQueryString("pageIndex=1&pageSize=100"). | |||||
| SetResult(&getImagesResult). | |||||
| Get(HOST + "/rest-server/api/v1/image/public/list/") | |||||
| if err != nil { | |||||
| return nil, fmt.Errorf("resty GetPublicImages: %v", err) | |||||
| } | |||||
| if getImagesResult.Code == "S401" && retry < 1 { | |||||
| retry++ | |||||
| _ = loginCloudbrain() | |||||
| goto sendjob | |||||
| func getTotalPages(getImagesResult models.GetImagesResult, size int) int { | |||||
| totalCount := getImagesResult.Payload.Count | |||||
| var totalPages int | |||||
| if totalCount%size != 0 { | |||||
| totalPages = totalCount/size + 1 | |||||
| } else { | |||||
| totalPages = totalCount / size | |||||
| } | } | ||||
| return totalPages | |||||
| } | |||||
| if getImagesResult.Code != Success { | |||||
| return &getImagesResult, fmt.Errorf("getImgesResult err: %s", res.String()) | |||||
| func getQueryString(page int, size int, name string) string { | |||||
| if strings.TrimSpace(name) == "" { | |||||
| return fmt.Sprintf("pageIndex=%d&pageSize=%d", page, size) | |||||
| } | } | ||||
| return &getImagesResult, nil | |||||
| return fmt.Sprintf("pageIndex=%d&pageSize=%d&name=%s", page, size, name) | |||||
| } | } | ||||
| func CommitImage(jobID string, params models.CommitImageParams) error { | func CommitImage(jobID string, params models.CommitImageParams) error { | ||||
| @@ -32,7 +32,8 @@ const ( | |||||
| // tplExploreOrganizations explore organizations page template | // tplExploreOrganizations explore organizations page template | ||||
| tplExploreOrganizations base.TplName = "explore/organizations" | tplExploreOrganizations base.TplName = "explore/organizations" | ||||
| // tplExploreCode explore code page template | // tplExploreCode explore code page template | ||||
| tplExploreCode base.TplName = "explore/code" | |||||
| tplExploreCode base.TplName = "explore/code" | |||||
| tplExploreImages base.TplName = "explore/images" | |||||
| ) | ) | ||||
| // Home render home page | // Home render home page | ||||
| @@ -475,6 +476,10 @@ func ExploreCode(ctx *context.Context) { | |||||
| ctx.HTML(200, tplExploreCode) | ctx.HTML(200, tplExploreCode) | ||||
| } | } | ||||
| func ExploreImages(ctx *context.Context) { | |||||
| ctx.HTML(200, tplExploreImages) | |||||
| } | |||||
| // NotFound render 404 page | // NotFound render 404 page | ||||
| func NotFound(ctx *context.Context) { | func NotFound(ctx *context.Context) { | ||||
| ctx.Data["Title"] = "Page Not Found" | ctx.Data["Title"] = "Page Not Found" | ||||
| @@ -1,8 +1,6 @@ | |||||
| package repo | package repo | ||||
| import ( | import ( | ||||
| "code.gitea.io/gitea/modules/git" | |||||
| "code.gitea.io/gitea/modules/storage" | |||||
| "encoding/json" | "encoding/json" | ||||
| "errors" | "errors" | ||||
| "net/http" | "net/http" | ||||
| @@ -13,6 +11,9 @@ import ( | |||||
| "strings" | "strings" | ||||
| "time" | "time" | ||||
| "code.gitea.io/gitea/modules/git" | |||||
| "code.gitea.io/gitea/modules/storage" | |||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| "code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
| "code.gitea.io/gitea/modules/base" | "code.gitea.io/gitea/modules/base" | ||||
| @@ -23,10 +24,10 @@ import ( | |||||
| ) | ) | ||||
| const ( | const ( | ||||
| tplCloudBrainIndex base.TplName = "repo/cloudbrain/index" | |||||
| tplCloudBrainNew base.TplName = "repo/cloudbrain/new" | |||||
| tplCloudBrainShow base.TplName = "repo/cloudbrain/show" | |||||
| tplCloudBrainShowModels base.TplName = "repo/cloudbrain/models/index" | |||||
| tplCloudBrainIndex base.TplName = "repo/cloudbrain/index" | |||||
| tplCloudBrainNew base.TplName = "repo/cloudbrain/new" | |||||
| tplCloudBrainShow base.TplName = "repo/cloudbrain/show" | |||||
| tplCloudBrainShowModels base.TplName = "repo/cloudbrain/models/index" | |||||
| ) | ) | ||||
| var ( | var ( | ||||
| @@ -397,6 +398,38 @@ func CloudBrainShowModels(ctx *context.Context) { | |||||
| ctx.HTML(200, tplCloudBrainShowModels) | ctx.HTML(200, tplCloudBrainShowModels) | ||||
| } | } | ||||
| func GetPublicImages(ctx *context.Context) { | |||||
| getImages(ctx, cloudbrain.Public) | |||||
| } | |||||
| func GetCustomImages(ctx *context.Context) { | |||||
| getImages(ctx, cloudbrain.Custom) | |||||
| } | |||||
| func getImages(ctx *context.Context, imageType string) { | |||||
| log.Info("Get images begin") | |||||
| page := ctx.QueryInt("page") | |||||
| size := ctx.QueryInt("size") | |||||
| name := ctx.Query("name") | |||||
| getImagesResult, err := cloudbrain.GetImagesPageable(page, size, imageType, name) | |||||
| if err != nil { | |||||
| log.Error("Can not get images:%v", err) | |||||
| ctx.JSON(http.StatusOK, models.GetImagesPayload{ | |||||
| Count: 0, | |||||
| TotalPages: 0, | |||||
| ImageInfo: []*models.ImageInfo{}, | |||||
| }) | |||||
| } else { | |||||
| ctx.JSON(http.StatusOK, getImagesResult.Payload) | |||||
| } | |||||
| log.Info("Get images end") | |||||
| } | |||||
| func getModelDirs(jobName string, parentDir string) (string, error) { | func getModelDirs(jobName string, parentDir string) (string, error) { | ||||
| var req string | var req string | ||||
| modelActualPath := setting.JobPath + jobName + "/model/" | modelActualPath := setting.JobPath + jobName + "/model/" | ||||
| @@ -413,7 +446,7 @@ func CloudBrainDownloadModel(ctx *context.Context) { | |||||
| parentDir := ctx.Query("parentDir") | parentDir := ctx.Query("parentDir") | ||||
| fileName := ctx.Query("fileName") | fileName := ctx.Query("fileName") | ||||
| jobName := ctx.Query("jobName") | jobName := ctx.Query("jobName") | ||||
| filePath := "jobs/" +jobName + "/model/" + parentDir | |||||
| filePath := "jobs/" + jobName + "/model/" + parentDir | |||||
| url, err := storage.Attachments.PresignedGetURL(filePath, fileName) | url, err := storage.Attachments.PresignedGetURL(filePath, fileName) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error("PresignedGetURL failed: %v", err.Error(), ctx.Data["msgID"]) | log.Error("PresignedGetURL failed: %v", err.Error(), ctx.Data["msgID"]) | ||||
| @@ -6,13 +6,14 @@ package routes | |||||
| import ( | import ( | ||||
| "bytes" | "bytes" | ||||
| "code.gitea.io/gitea/routers/secure" | |||||
| "encoding/gob" | "encoding/gob" | ||||
| "net/http" | "net/http" | ||||
| "path" | "path" | ||||
| "text/template" | "text/template" | ||||
| "time" | "time" | ||||
| "code.gitea.io/gitea/routers/secure" | |||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| "code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
| "code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
| @@ -313,11 +314,14 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Get("", func(ctx *context.Context) { | m.Get("", func(ctx *context.Context) { | ||||
| ctx.Redirect(setting.AppSubURL + "/explore/repos") | ctx.Redirect(setting.AppSubURL + "/explore/repos") | ||||
| }) | }) | ||||
| m.Get("/images/public", repo.GetPublicImages) | |||||
| m.Get("/images/custom", repo.GetCustomImages) | |||||
| m.Get("/repos", routers.ExploreRepos) | m.Get("/repos", routers.ExploreRepos) | ||||
| m.Get("/datasets", routers.ExploreDatasets) | m.Get("/datasets", routers.ExploreDatasets) | ||||
| m.Get("/users", routers.ExploreUsers) | m.Get("/users", routers.ExploreUsers) | ||||
| m.Get("/organizations", routers.ExploreOrganizations) | m.Get("/organizations", routers.ExploreOrganizations) | ||||
| m.Get("/code", routers.ExploreCode) | m.Get("/code", routers.ExploreCode) | ||||
| m.Get("/images", routers.ExploreImages) | |||||
| }, ignSignIn) | }, ignSignIn) | ||||
| m.Combo("/install", routers.InstallInit).Get(routers.Install). | m.Combo("/install", routers.InstallInit).Get(routers.Install). | ||||
| Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost) | Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost) | ||||