Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/storage/namedurl.go
2501 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package storage
6
7
import (
8
"context"
9
"net/http"
10
11
"golang.org/x/xerrors"
12
13
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
14
)
15
16
// NamedURLDownloader offers downloads from fixed URLs
17
type NamedURLDownloader struct {
18
URLs map[string]string
19
}
20
21
// Download takes the latest state from the remote storage and downloads it to a local path
22
func (d *NamedURLDownloader) Download(ctx context.Context, destination string, name string, mappings []archive.IDMapping) (found bool, err error) {
23
url, found := d.URLs[name]
24
if !found {
25
return false, nil
26
}
27
28
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
29
if err != nil {
30
return false, err
31
}
32
33
resp, err := http.DefaultClient.Do(req)
34
if err != nil {
35
return false, err
36
}
37
if resp.StatusCode == http.StatusFound {
38
return false, nil
39
}
40
if resp.StatusCode != http.StatusOK {
41
return false, xerrors.Errorf("non-OK status code: %v", resp.StatusCode)
42
}
43
defer resp.Body.Close()
44
45
err = extractTarbal(ctx, destination, resp.Body, mappings)
46
if err != nil {
47
return true, err
48
}
49
50
return true, nil
51
}
52
53
// DownloadSnapshot downloads a snapshot.
54
func (d *NamedURLDownloader) DownloadSnapshot(ctx context.Context, destination string, name string, mappings []archive.IDMapping) (found bool, err error) {
55
return d.Download(ctx, destination, name, mappings)
56
}
57
58