Debian Codegraph @

pkg/api/download.go

raw
1 // Copyright 2023-2024 The Archivista Contributors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package api
16
17 import (
18 "bytes"
19 "context"
20 "encoding/json"
21 "errors"
22 "io"
23 "net/http"
24 "net/url"
25
26 "github.com/in-toto/go-witness/dsse"
27 )
28
29 func DownloadReadCloser(ctx context.Context, baseURL string, gitoid string, requestOptions ...RequestOption) (io.ReadCloser, error) {
30 return DownloadReadCloserWithHTTPClient(ctx, &http.Client{}, baseURL, gitoid, requestOptions...)
31 }
32
33 func DownloadReadCloserWithHTTPClient(ctx context.Context, client *http.Client, baseURL string, gitoid string, requestOptions ...RequestOption) (io.ReadCloser, error) {
34 downloadURL, err := url.JoinPath(baseURL, "download", gitoid)
35 if err != nil {
36 return nil, err
37 }
38
39 req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
40 if err != nil {
41 return nil, err
42 }
43
44 req = applyRequestOptions(req, requestOptions...)
45 req.Header.Set("Content-Type", "application/json")
46 resp, err := client.Do(req)
47 if err != nil {
48 return nil, err
49 }
50
51 if resp.StatusCode != http.StatusOK {
52 // NOTE: attempt to read body on error and
53 // only close if an error occurs
54 defer resp.Body.Close()
55 errMsg, err := io.ReadAll(resp.Body)
56 if err != nil {
57 return nil, err
58 }
59 return nil, errors.New(string(errMsg))
60 }
61 return resp.Body, nil
62 }
63
64 func Download(ctx context.Context, baseURL string, gitoid string, requestOptions ...RequestOption) (dsse.Envelope, error) {
65 buf := &bytes.Buffer{}
66 if err := DownloadWithWriter(ctx, baseURL, gitoid, buf, requestOptions...); err != nil {
67 return dsse.Envelope{}, err
68 }
69
70 env := dsse.Envelope{}
71 dec := json.NewDecoder(buf)
72 if err := dec.Decode(&env); err != nil {
73 return env, err
74 }
75
76 return env, nil
77 }
78
79 func DownloadWithWriter(ctx context.Context, baseURL string, gitoid string, dst io.Writer, requestOptions ...RequestOption) error {
80 return DownloadWithWriterWithHTTPClient(ctx, &http.Client{}, baseURL, gitoid, dst, requestOptions...)
81 }
82
83 func DownloadWithWriterWithHTTPClient(ctx context.Context, client *http.Client, baseURL string, gitoid string, dst io.Writer, requestOptions ...RequestOption) error {
84 downloadUrl, err := url.JoinPath(baseURL, "download", gitoid)
85 if err != nil {
86 return err
87 }
88
89 req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadUrl, nil)
90 if err != nil {
91 return err
92 }
93
94 req = applyRequestOptions(req, requestOptions...)
95 req.Header.Set("Content-Type", "application/json")
96 hc := &http.Client{}
97 resp, err := hc.Do(req)
98 if err != nil {
99 return err
100 }
101
102 defer resp.Body.Close()
103 if resp.StatusCode != http.StatusOK {
104 errMsg, err := io.ReadAll(resp.Body)
105 if err != nil {
106 return err
107 }
108
109 return errors.New(string(errMsg))
110 }
111
112 _, err = io.Copy(dst, resp.Body)
113 return err
114 }