| @@ -123,8 +123,9 @@ type GetImagesResult 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 { | |||
| @@ -1,9 +1,11 @@ | |||
| package cloudbrain | |||
| import ( | |||
| "code.gitea.io/gitea/modules/log" | |||
| "encoding/json" | |||
| "fmt" | |||
| "strings" | |||
| "code.gitea.io/gitea/modules/log" | |||
| "code.gitea.io/gitea/models" | |||
| "code.gitea.io/gitea/modules/setting" | |||
| @@ -11,13 +13,16 @@ import ( | |||
| ) | |||
| 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 ( | |||
| JobHasBeenStopped = "S410" | |||
| Public = "public" | |||
| Custom = "custom" | |||
| ) | |||
| func getRestyClient() *resty.Client { | |||
| @@ -126,6 +131,16 @@ sendjob: | |||
| } | |||
| 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() | |||
| client := getRestyClient() | |||
| var getImagesResult models.GetImagesResult | |||
| @@ -136,9 +151,9 @@ sendjob: | |||
| res, err := client.R(). | |||
| SetHeader("Content-Type", "application/json"). | |||
| SetAuthToken(TOKEN). | |||
| SetQueryString("pageIndex=1&pageSize=100"). | |||
| SetQueryString(getQueryString(page, size, name)). | |||
| SetResult(&getImagesResult). | |||
| Get(HOST + "/rest-server/api/v1/image/list/") | |||
| Get(HOST + ImagesUrlMap[imageType]) | |||
| if err != nil { | |||
| return nil, fmt.Errorf("resty GetImages: %v", err) | |||
| @@ -157,48 +172,30 @@ 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 { | |||
| return &getImagesResult, fmt.Errorf("getImagesResult err: %s", res.String()) | |||
| } | |||
| getImagesResult.Payload.TotalPages = getTotalPages(getImagesResult, size) | |||
| 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 { | |||
| @@ -32,7 +32,8 @@ const ( | |||
| // tplExploreOrganizations explore organizations page template | |||
| tplExploreOrganizations base.TplName = "explore/organizations" | |||
| // tplExploreCode explore code page template | |||
| tplExploreCode base.TplName = "explore/code" | |||
| tplExploreCode base.TplName = "explore/code" | |||
| tplExploreImages base.TplName = "explore/images" | |||
| ) | |||
| // Home render home page | |||
| @@ -475,6 +476,10 @@ func ExploreCode(ctx *context.Context) { | |||
| ctx.HTML(200, tplExploreCode) | |||
| } | |||
| func ExploreImages(ctx *context.Context) { | |||
| ctx.HTML(200, tplExploreImages) | |||
| } | |||
| // NotFound render 404 page | |||
| func NotFound(ctx *context.Context) { | |||
| ctx.Data["Title"] = "Page Not Found" | |||
| @@ -1,8 +1,6 @@ | |||
| package repo | |||
| import ( | |||
| "code.gitea.io/gitea/modules/git" | |||
| "code.gitea.io/gitea/modules/storage" | |||
| "encoding/json" | |||
| "errors" | |||
| "net/http" | |||
| @@ -13,6 +11,9 @@ import ( | |||
| "strings" | |||
| "time" | |||
| "code.gitea.io/gitea/modules/git" | |||
| "code.gitea.io/gitea/modules/storage" | |||
| "code.gitea.io/gitea/models" | |||
| "code.gitea.io/gitea/modules/auth" | |||
| "code.gitea.io/gitea/modules/base" | |||
| @@ -23,10 +24,10 @@ import ( | |||
| ) | |||
| 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 ( | |||
| @@ -397,6 +398,38 @@ func CloudBrainShowModels(ctx *context.Context) { | |||
| 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) { | |||
| var req string | |||
| modelActualPath := setting.JobPath + jobName + "/model/" | |||
| @@ -413,7 +446,7 @@ func CloudBrainDownloadModel(ctx *context.Context) { | |||
| parentDir := ctx.Query("parentDir") | |||
| fileName := ctx.Query("fileName") | |||
| jobName := ctx.Query("jobName") | |||
| filePath := "jobs/" +jobName + "/model/" + parentDir | |||
| filePath := "jobs/" + jobName + "/model/" + parentDir | |||
| url, err := storage.Attachments.PresignedGetURL(filePath, fileName) | |||
| if err != nil { | |||
| log.Error("PresignedGetURL failed: %v", err.Error(), ctx.Data["msgID"]) | |||
| @@ -6,13 +6,14 @@ package routes | |||
| import ( | |||
| "bytes" | |||
| "code.gitea.io/gitea/routers/secure" | |||
| "encoding/gob" | |||
| "net/http" | |||
| "path" | |||
| "text/template" | |||
| "time" | |||
| "code.gitea.io/gitea/routers/secure" | |||
| "code.gitea.io/gitea/models" | |||
| "code.gitea.io/gitea/modules/auth" | |||
| "code.gitea.io/gitea/modules/context" | |||
| @@ -313,11 +314,14 @@ func RegisterRoutes(m *macaron.Macaron) { | |||
| m.Get("", func(ctx *context.Context) { | |||
| 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("/datasets", routers.ExploreDatasets) | |||
| m.Get("/users", routers.ExploreUsers) | |||
| m.Get("/organizations", routers.ExploreOrganizations) | |||
| m.Get("/code", routers.ExploreCode) | |||
| m.Get("/images", routers.ExploreImages) | |||
| }, ignSignIn) | |||
| m.Combo("/install", routers.InstallInit).Get(routers.Install). | |||
| Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost) | |||