//AppcGetACIV1Handler is func AppcGetACIV1Handler(ctx *macaron.Context) { namespace := ctx.Params(":namespace") repository := ctx.Params(":repository") filename := ctx.Params(":file") aci := strings.Trim(filename, ".asc") //TODO: Decode aci file name with template. version := strings.Split(aci, "-")[1] r := new(models.AppcV1) i := new(models.ACIv1) 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."}) ctx.Resp.WriteHeader(http.StatusBadRequest) ctx.Resp.Write(result) return } if err := i.Get(r.ID, version, aci); err != nil { log.Errorf("[%s] get ACIV1 data error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(map[string]string{"message": "Get ACI Data Error."}) ctx.Resp.WriteHeader(http.StatusBadRequest) ctx.Resp.Write(result) return } var path string if asc := strings.Contains(filename, ".asc"); asc == true { path = i.Sign } else { path = i.Path } if file, err := os.Open(path); err != nil { log.Errorf("[%s] get File(%v) error: %s", ctx.Req.RequestURI, file, err.Error()) result, _ := json.Marshal(map[string]string{"message": "Get ACI or ASC file Error."}) ctx.Resp.WriteHeader(http.StatusBadRequest) ctx.Resp.Write(result) return } else { stat, _ := file.Stat() size := strconv.FormatInt(stat.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", filename)) 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 } }
//AppcPostCompleteV1Handler is func AppcPostCompleteV1Handler(ctx *macaron.Context) (int, []byte) { namespace := ctx.Params(":namespace") repository := ctx.Params(":repository") version := ctx.Params(":version") aci := ctx.Params(":aci") complete := new(CompleteMsg) i := new(models.ACIv1) 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(CompleteMsg{ Success: false, Reason: "", ServerReason: "Get Appc Repository Error.", }) return http.StatusBadRequest, result } if err := i.Get(r.ID, version, aci); err != nil { log.Errorf("[%s] get ACIV1 data error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(CompleteMsg{ Success: false, Reason: "", ServerReason: "Get ACI Data Error.", }) return http.StatusBadRequest, result } data, _ := ctx.Req.Body().Bytes() if err := json.Unmarshal(data, &complete); err != nil { log.Errorf("[%s] decode complete json dataerror: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(CompleteMsg{ Success: false, Reason: "", ServerReason: "Decode Complete JSON Data Error.", }) return http.StatusBadRequest, result } if complete.Success == true { root, _ := os.Getwd() sign := fmt.Sprintf("%s/%s", root, "external/signs") if err := signature.VerifyACISignature(i.Path, i.Sign, sign); err != nil { log.Errorf("[%s] Verify ACI signature error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(CompleteMsg{ Success: false, Reason: "", ServerReason: "Verify ACI Signature Error.", }) return http.StatusBadRequest, result } if err := i.Unlocked(r.ID, version, aci); err != nil { log.Errorf("[%s] Unlocked ACI file error: %s", ctx.Req.RequestURI, err.Error()) result, _ := json.Marshal(CompleteMsg{ Success: false, Reason: "", ServerReason: "Unlocked ACI File Error.", }) return http.StatusBadRequest, result } result, _ := json.Marshal(CompleteMsg{ Success: true, Reason: "", ServerReason: "", }) return http.StatusOK, result } result, _ := json.Marshal(CompleteMsg{ Success: false, Reason: complete.Reason, ServerReason: "", }) return http.StatusBadRequest, result }