GetLength of given url
(ctx context.Context, url string)
| 82 | |
| 83 | // GetLength of given url |
| 84 | func (downloader *downloaderImpl) GetLength(ctx context.Context, url string) (int64, error) { |
| 85 | req, err := downloader.newRequest(ctx, "HEAD", url) |
| 86 | if err != nil { |
| 87 | return -1, err |
| 88 | } |
| 89 | |
| 90 | var resp *http.Response |
| 91 | |
| 92 | maxTries := downloader.maxTries |
| 93 | for maxTries > 0 { |
| 94 | resp, err = downloader.client.Do(req) |
| 95 | if err != nil && retryableError(err) { |
| 96 | maxTries-- |
| 97 | } else { |
| 98 | // stop retrying |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if err != nil { |
| 104 | return -1, errors.Wrap(err, url) |
| 105 | } |
| 106 | |
| 107 | if resp.StatusCode < 200 || resp.StatusCode > 299 { |
| 108 | return -1, &Error{Code: resp.StatusCode, URL: url} |
| 109 | } |
| 110 | |
| 111 | if resp.ContentLength < 0 { |
| 112 | // an existing, but zero-length file can be reported with ContentLength -1 |
| 113 | if resp.StatusCode == 200 && resp.ContentLength == -1 { |
| 114 | return 0, nil |
| 115 | } |
| 116 | return -1, fmt.Errorf("could not determine length of %s", url) |
| 117 | } |
| 118 | |
| 119 | return resp.ContentLength, nil |
| 120 | } |
| 121 | |
| 122 | // Download starts new download task |
| 123 | func (downloader *downloaderImpl) Download(ctx context.Context, url string, destination string) error { |
nothing calls this directly
no test coverage detected