示例#1
0
//AppcPutACIV1Handler is
func AppcPutACIV1Handler(ctx *macaron.Context) (int, []byte) {
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	version := ctx.Params(":version")
	aci := ctx.Params(":aci")

	r := new(models.AppcV1)
	if err := r.Get(namespace, repository); err != nil {
		log.Errorf("[%s] get AppcV1 repository error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Get Appc Repository Error."})
		return http.StatusBadRequest, result
	}

	basePath := setting.AppcStorage
	imagePath := fmt.Sprintf("%s/%s/%s/%s", basePath, namespace, repository, version)
	filePath := fmt.Sprintf("%s/%s", imagePath, aci)

	if !utils.IsDirExist(imagePath) {
		os.MkdirAll(imagePath, os.ModePerm)
	}

	if _, err := os.Stat(filePath); err == nil {
		os.Remove(filePath)
	}

	if file, err := os.Create(filePath); err != nil {
		log.Errorf("[%s] Create aci file error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Create .aci File Error."})
		return http.StatusBadRequest, result
	} else {
		io.Copy(file, ctx.Req.Request.Body)
	}

	size, _ := utils.GetFileSize(filePath)

	i := new(models.ACIv1)
	if err := i.PutACI(r.ID, size, version, aci, filePath); err != nil {
		log.Errorf("[%s] write aci data error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Write .aci Data Error."})
		return http.StatusBadRequest, result
	}

	result, _ := json.Marshal(map[string]string{})
	return http.StatusOK, result
}