-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (65 loc) · 1.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"archive/zip"
"cloud.google.com/go/storage"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
)
type Asset struct {
Filename string
Url string
GcsUrl string
}
var client = &http.Client{}
func main() {
http.HandleFunc("/download", func(w http.ResponseWriter, req *http.Request) {
assetManifest := make([]Asset, 0)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
json.Unmarshal(body, &assetManifest)
w.Header()["Content-Type"] = []string{"application/zip"}
w.Header()["Content-Disposition"] = []string{"attachment; filename=\"brandfolder_assets.zip\""}
w.WriteHeader(200) // Status Code pushes headers so we can stream data
writer := zip.NewWriter(w)
for _, asset := range assetManifest {
func() (err error) {
zipWriter, err := writer.CreateHeader(
&zip.FileHeader{
Name: asset.Filename,
},
)
var fileReader io.Reader
if asset.GcsUrl != "" {
url, err := url.Parse(asset.GcsUrl)
client, err := storage.NewClient(context.Background())
fileReader, err = client.Bucket(url.Host).Object(url.Path).NewReader(context.Background())
if err != nil {
return err
}
} else {
req, err := http.NewRequest("GET", asset.Url, nil)
assetResp, err := client.Do(req)
if err != nil {
return err
}
defer assetResp.Body.Close()
fileReader = assetResp.Body
}
io.Copy(zipWriter, fileReader)
return
}()
}
writer.Close()
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil))
}