Beispiel #1
0
func getAsset(w http.ResponseWriter, r *http.Request) {
	var (
		request = api.GetAssetRequest{}
	)

	err := readRequest(r, &request)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	accessKey, err := mailbox.FindKeyByName(request.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Access key is invalid")
		return
	}

	if !request.Validate(accessKey.Secret) {
		sendError(w, "Could not validate signature")
		return
	}

	fp := filepath.Join(filesPath(), request.MD5)
	if _, err := os.Stat(fp); os.IsNotExist(err) {
		sendError(w, "Asset not found on server")
		return
	}

	log.Infof("Serving asset to %s", accessKey.Name)
	http.ServeFile(w, r, fp)

}
Beispiel #2
0
func (client *Client) DownloadAsset(md5 string) (string, error) {
	tmpFile, err := ioutil.TempFile("", "conduit.")
	if err != nil {
		return "", err
	}
	defer tmpFile.Close()

	req := api.GetAssetRequest{
		MD5: md5,
	}
	req.Sign(client.AccessKeyName, client.AccessKey)

	requestBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}

	url := fmt.Sprintf("http://%s/%s", client.Host, "asset")
	if client.ShowRequests {
		log.Infof("URL: %s\nRequest: %s", url, string(requestBytes))
	}

	requestReader := bytes.NewReader(requestBytes)
	resp, err := http.Post(url, "application/json", requestReader)
	if resp != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return "", err
	}
	if resp.StatusCode == 200 {
		_, err = io.Copy(tmpFile, resp.Body)
		if err != nil {
			return "", err
		}
		return tmpFile.Name(), nil
	} else {
		return "", errors.New(fmt.Sprintf("Could not download asset (%d)",
			resp.StatusCode))
	}
}