Path: blob/main/components/content-service/pkg/storage/namedurl.go
2501 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package storage56import (7"context"8"net/http"910"golang.org/x/xerrors"1112"github.com/gitpod-io/gitpod/content-service/pkg/archive"13)1415// NamedURLDownloader offers downloads from fixed URLs16type NamedURLDownloader struct {17URLs map[string]string18}1920// Download takes the latest state from the remote storage and downloads it to a local path21func (d *NamedURLDownloader) Download(ctx context.Context, destination string, name string, mappings []archive.IDMapping) (found bool, err error) {22url, found := d.URLs[name]23if !found {24return false, nil25}2627req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)28if err != nil {29return false, err30}3132resp, err := http.DefaultClient.Do(req)33if err != nil {34return false, err35}36if resp.StatusCode == http.StatusFound {37return false, nil38}39if resp.StatusCode != http.StatusOK {40return false, xerrors.Errorf("non-OK status code: %v", resp.StatusCode)41}42defer resp.Body.Close()4344err = extractTarbal(ctx, destination, resp.Body, mappings)45if err != nil {46return true, err47}4849return true, nil50}5152// DownloadSnapshot downloads a snapshot.53func (d *NamedURLDownloader) DownloadSnapshot(ctx context.Context, destination string, name string, mappings []archive.IDMapping) (found bool, err error) {54return d.Download(ctx, destination, name, mappings)55}565758