|
| 1 | +package shock |
| 2 | + |
| 3 | +import ( |
| 4 | + "compress/gzip" |
| 5 | + "context" |
| 6 | + "crypto/md5" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "strconv" |
| 14 | +) |
| 15 | + |
| 16 | +type downloadConfig struct { |
| 17 | + uncompress string |
| 18 | + computeMD5 bool |
| 19 | +} |
| 20 | + |
| 21 | +// DownloadOption configures a DownloadToFile call. |
| 22 | +type DownloadOption func(*downloadConfig) |
| 23 | + |
| 24 | +// WithUncompress sets the decompression method for downloads (e.g. "gzip"). |
| 25 | +func WithUncompress(method string) DownloadOption { |
| 26 | + return func(cfg *downloadConfig) { |
| 27 | + cfg.uncompress = method |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// WithComputeMD5 enables MD5 checksum computation during download. |
| 32 | +func WithComputeMD5() DownloadOption { |
| 33 | + return func(cfg *downloadConfig) { |
| 34 | + cfg.computeMD5 = true |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Download streams a node's file data. The caller must close the returned ReadCloser. |
| 39 | +func (c *Client) Download(ctx context.Context, nodeID string) (io.ReadCloser, error) { |
| 40 | + query := url.Values{"download": {""}} |
| 41 | + u := c.buildURL("/node/"+nodeID, query) |
| 42 | + req, err := http.NewRequestWithContext(ctx, "GET", u, nil) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + c.setAuth(req) |
| 47 | + c.debugf("GET %s (download)", u) |
| 48 | + |
| 49 | + res, err := c.httpClient.Do(req) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + if res.StatusCode == http.StatusOK { |
| 55 | + return res.Body, nil |
| 56 | + } |
| 57 | + |
| 58 | + // Error response — parse JSON envelope |
| 59 | + defer res.Body.Close() |
| 60 | + body, _ := io.ReadAll(res.Body) |
| 61 | + var envelope struct { |
| 62 | + Error []string `json:"error"` |
| 63 | + } |
| 64 | + if json.Unmarshal(body, &envelope) == nil && len(envelope.Error) > 0 { |
| 65 | + return nil, &APIError{StatusCode: res.StatusCode, Errors: envelope.Error} |
| 66 | + } |
| 67 | + return nil, &APIError{StatusCode: res.StatusCode, Errors: []string{string(body)}} |
| 68 | +} |
| 69 | + |
| 70 | +// DownloadRange streams a byte range from a node's file. |
| 71 | +func (c *Client) DownloadRange(ctx context.Context, nodeID string, seek, length int64) (io.ReadCloser, error) { |
| 72 | + query := url.Values{ |
| 73 | + "download": {""}, |
| 74 | + "seek": {strconv.FormatInt(seek, 10)}, |
| 75 | + "length": {strconv.FormatInt(length, 10)}, |
| 76 | + } |
| 77 | + u := c.buildURL("/node/"+nodeID, query) |
| 78 | + req, err := http.NewRequestWithContext(ctx, "GET", u, nil) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + c.setAuth(req) |
| 83 | + |
| 84 | + res, err := c.httpClient.Do(req) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + if res.StatusCode == http.StatusOK { |
| 90 | + return res.Body, nil |
| 91 | + } |
| 92 | + |
| 93 | + defer res.Body.Close() |
| 94 | + body, _ := io.ReadAll(res.Body) |
| 95 | + var envelope struct { |
| 96 | + Error []string `json:"error"` |
| 97 | + } |
| 98 | + if json.Unmarshal(body, &envelope) == nil && len(envelope.Error) > 0 { |
| 99 | + return nil, &APIError{StatusCode: res.StatusCode, Errors: envelope.Error} |
| 100 | + } |
| 101 | + return nil, &APIError{StatusCode: res.StatusCode, Errors: []string{string(body)}} |
| 102 | +} |
| 103 | + |
| 104 | +// DownloadToFile downloads a node's file to a local path, optionally decompressing and computing MD5. |
| 105 | +func (c *Client) DownloadToFile(ctx context.Context, nodeID, filepath string, opts ...DownloadOption) (int64, string, error) { |
| 106 | + cfg := &downloadConfig{} |
| 107 | + for _, opt := range opts { |
| 108 | + opt(cfg) |
| 109 | + } |
| 110 | + |
| 111 | + body, err := c.Download(ctx, nodeID) |
| 112 | + if err != nil { |
| 113 | + return 0, "", err |
| 114 | + } |
| 115 | + defer body.Close() |
| 116 | + |
| 117 | + localFile, err := os.Create(filepath) |
| 118 | + if err != nil { |
| 119 | + return 0, "", err |
| 120 | + } |
| 121 | + defer localFile.Close() |
| 122 | + |
| 123 | + md5h := md5.New() |
| 124 | + var size int64 |
| 125 | + |
| 126 | + if cfg.uncompress == "gzip" { |
| 127 | + var src io.Reader = body |
| 128 | + if cfg.computeMD5 { |
| 129 | + // MD5 on compressed stream, then decompress |
| 130 | + pReader, pWriter := io.Pipe() |
| 131 | + defer pReader.Close() |
| 132 | + dst := io.MultiWriter(pWriter, md5h) |
| 133 | + go func() { |
| 134 | + io.Copy(dst, body) |
| 135 | + pWriter.Close() |
| 136 | + }() |
| 137 | + src = pReader |
| 138 | + } |
| 139 | + gr, gerr := gzip.NewReader(src) |
| 140 | + if gerr != nil { |
| 141 | + return 0, "", gerr |
| 142 | + } |
| 143 | + defer gr.Close() |
| 144 | + size, err = io.Copy(localFile, gr) |
| 145 | + if err != nil { |
| 146 | + return 0, "", err |
| 147 | + } |
| 148 | + } else { |
| 149 | + var dst io.Writer |
| 150 | + if cfg.computeMD5 { |
| 151 | + dst = io.MultiWriter(localFile, md5h) |
| 152 | + } else { |
| 153 | + dst = localFile |
| 154 | + } |
| 155 | + size, err = io.Copy(dst, body) |
| 156 | + if err != nil { |
| 157 | + return 0, "", err |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + var md5sum string |
| 162 | + if cfg.computeMD5 { |
| 163 | + md5sum = fmt.Sprintf("%x", md5h.Sum(nil)) |
| 164 | + } |
| 165 | + return size, md5sum, nil |
| 166 | +} |
| 167 | + |
| 168 | +// GetDownloadURL gets a pre-authenticated download URL for a node. |
| 169 | +func (c *Client) GetDownloadURL(ctx context.Context, nodeID string) (*PreAuthResponse, error) { |
| 170 | + query := url.Values{"download_url": {""}} |
| 171 | + var resp PreAuthResponse |
| 172 | + if err := c.doRequest(ctx, "GET", "/node/"+nodeID, query, &resp); err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + return &resp, nil |
| 176 | +} |
| 177 | + |
| 178 | +// DownloadURL returns the direct download URL for a node (no pre-auth). |
| 179 | +func (c *Client) DownloadURL(nodeID string) string { |
| 180 | + query := url.Values{"download": {""}} |
| 181 | + return c.buildURL("/node/"+nodeID, query) |
| 182 | +} |
0 commit comments