//GetImageJSONV1Handler is getting image json data function. //When docker client push an image, dockyard return http status code '400' or '404' if haven't it. Then the docker client will push the json data and layer file. //If dockyard has the image and return 200, the docker client will ignore it and push another iamge. func GetImageJSONV1Handler(ctx *macaron.Context) (int, []byte) { //TODO: If standalone == true, Dockyard will check HEADER Authorization; if standalone == false, Dockyard will check HEADER TOEKN. imageID := ctx.Params(":image") image := new(models.DockerImageV1) if i, err := image.Get(imageID); err != nil && err == gorm.ErrRecordNotFound { log.WithFields(log.Fields{ "image": i.ImageID, }).Info("Image Not Found.") result, _ := json.Marshal(map[string]string{}) return http.StatusNotFound, result } else if err != nil && err != gorm.ErrRecordNotFound { log.Errorf("[%s] get image error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(map[string]string{"Error": "Get Image Error"}) return http.StatusBadRequest, result } else { ctx.Resp.Header().Set("X-Docker-Checksum-Payload", i.Checksum) ctx.Resp.Header().Set("X-Docker-Size", fmt.Sprint(i.Size)) ctx.Resp.Header().Set("Content-Length", fmt.Sprint(len(i.JSON))) return http.StatusOK, []byte(i.JSON) } }
//GetImageAncestryV1Handler func GetImageAncestryV1Handler(ctx *macaron.Context) (int, []byte) { //TODO: If standalone == true, Dockyard will check HEADER Authorization; if standalone == false, Dockyard will check HEADER TOEKN. imageID := ctx.Params(":image") image := new(models.DockerImageV1) if i, err := image.Get(imageID); err != nil { log.Errorf("[%s] get image ancestry error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(map[string]string{"Error": "Get Image Ancestry Error"}) return http.StatusBadRequest, result } else { ctx.Resp.Header().Set("Content-Length", fmt.Sprint(len(i.Ancestry))) return http.StatusOK, []byte(i.Ancestry) } }
//GetImageLayerV1Handler func GetImageLayerV1Handler(ctx *macaron.Context) { //TODO: If standalone == true, Dockyard will check HEADER Authorization; if standalone == false, Dockyard will check HEADER TOEKN. imageID := ctx.Params(":image") image := new(models.DockerImageV1) if i, err := image.Get(imageID); err != nil { log.Errorf("[%s] get image ancestry error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(map[string]string{"Error": "Get Image Layer Error"}) ctx.Resp.WriteHeader(http.StatusBadRequest) ctx.Resp.Write(result) return } else { if file, err := os.Open(i.Path); err != nil { log.Errorf("[%s] get image layer file status: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(map[string]string{"Error": "Get Image Layer File Status Error"}) ctx.Resp.WriteHeader(http.StatusBadRequest) ctx.Resp.Write(result) return } else { size := strconv.FormatInt(i.Size, 10) ctx.Resp.Header().Set("Content-Description", "File Transfer") ctx.Resp.Header().Set("Content-Type", "application/octet-stream") ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", i.ImageID)) ctx.Resp.Header().Set("Content-Length", size) ctx.Resp.Header().Set("Expires", "0") ctx.Resp.Header().Set("Cache-Control", "must-revalidate") ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") ctx.Resp.Header().Set("Pragma", "public") file.Seek(0, 0) defer file.Close() io.Copy(ctx.Resp, file) ctx.Resp.WriteHeader(http.StatusOK) return } } }