57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
|
||
|
"git.makerforce.io/photos/photos/internal/httphelpers"
|
||
|
lib "git.makerforce.io/photos/photos/pkg/bucket"
|
||
|
"git.makerforce.io/photos/photos/pkg/credentials"
|
||
|
)
|
||
|
|
||
|
// TODO: handle errors nicer
|
||
|
|
||
|
func getPhoto(bucket lib.Bucket, cred credentials.Credential, photo lib.Photo) ([]byte, error) {
|
||
|
unsignedReq, err := http.NewRequest(http.MethodGET, bucket.URL(photo).String(), nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
signedReq := sig.Sign(unsignedReq, cred)
|
||
|
|
||
|
resp, err := http.DefaultClient.Do(signedReq)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
return nil, httphelpers.ErrorFromStatus(resp.StatusCode)
|
||
|
}
|
||
|
|
||
|
buf, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return buf, nil
|
||
|
}
|
||
|
|
||
|
func putPhotoMetadataSize(bucket lib.Bucket, cred credentials.Credential, photo lib.Photo, s lib.PhotoMetadataSize) error {
|
||
|
buf, err := lib.PhotoMetadataSizeMarshal(s)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
unsignedReq, err := http.NewRequest(http.MethodPUT, bucket.URL(photo.MetadataSize()).String(), bytes.NewBuffer(buf))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
signedReq := sig.Sign(unsignedReq, cred)
|
||
|
|
||
|
resp, err := http.DefaultClient.Do(signedReq)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
return httphelpers.ErrorFromStatus(resp.StatusCode)
|
||
|
}
|