-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathresumabledownload.go
329 lines (314 loc) · 8.29 KB
/
resumabledownload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Package main (resumableDownload.go) :
// These methods are for resumable downloading a shared file from Google Drive.
package main
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
drive "google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
// valResumableDownload : Structure for resumable download
type valResumableDownload struct {
para
dlParams
}
// dlParams : Parameters for downloading
type dlParams struct {
CurrentFileSize int64
DownloadFile *drive.File
OutMimeType string
Range string
Start int64
End int64
}
// getFileInfFromP : Retrieve file information from *para.
func (p *para) getFileInfFromP() (*drive.File, error) {
v := &valResumableDownload{
para: *p,
}
v.Client = &http.Client{}
if err := v.getFileInf(); err != nil {
return nil, err
}
return v.DownloadFile, nil
}
// showFileInf : Show file information.
func (p *para) showFileInf() error {
dlfile, err := p.getFileInfFromP()
if err != nil {
return err
}
r, err := json.Marshal(dlfile)
if err != nil {
return err
}
fmt.Printf("%s\n", r)
return nil
}
// getDownloadBytes : Get download size for resumable download.
func getDownloadBytes(size string) (int64, error) {
reg := regexp.MustCompile("^([0-9.]+)$|^([0-9.]+)([bkmgt])")
regRes := reg.FindStringSubmatch(strings.ToLower(size))
switch {
case len(regRes) == 0:
return 0, fmt.Errorf("wrong size: %s", size)
case regRes[1] != "":
s, err := strconv.ParseInt(regRes[1], 10, 64)
if err != nil {
return 0, err
}
return s, nil
case regRes[2] != "" && regRes[3] != "":
f, err := strconv.ParseFloat(regRes[2], 64)
if err != nil {
return 0, err
}
switch regRes[3] {
case "k":
f *= 1000
case "m":
f *= 1000000
case "g":
f *= 1000000000
case "t":
f *= 1000000000000
}
s := int64(f)
if s < 10000000 {
return 10000000, nil
}
return s, nil
default:
return 0, fmt.Errorf("unexpected error '%s'", size)
}
}
// resDownloadFileByAPIKey : Resumable download by API key.
func (v *valResumableDownload) resDownloadFileByAPIKey() (*http.Response, error) {
u, err := url.Parse(driveAPI)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, v.DownloadFile.Id)
q := u.Query()
q.Set("alt", "media")
q.Set("key", v.APIKey)
u.RawQuery = q.Encode()
timeOut := func(size int64) int64 {
if size == 0 {
switch {
case size < 100000000:
return 3600
case size > 100000000:
return 0
}
}
return 0
}(v.DownloadFile.Size)
v.Client.Timeout = time.Duration(timeOut) * time.Second
req, err := http.NewRequest("get", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Range", v.Range)
res, err := v.Client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 206 && res.StatusCode != 200 {
r, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
defer res.Body.Close()
return nil, fmt.Errorf("%s", r)
}
return res, nil
}
// getFileInf : Retrieve file infomation using Drive API.
func (v *valResumableDownload) getFileInf() error {
srv, err := drive.NewService(context.Background(), option.WithAPIKey(v.para.APIKey))
if err != nil {
return err
}
fields := []googleapi.Field{"createdTime,id,md5Checksum,mimeType,modifiedTime,name,owners,parents,shared,size,webContentLink,webViewLink"}
res, err := srv.Files.Get(v.ID).Fields(fields...).SupportsAllDrives(true).Do()
if err != nil {
return err
}
v.DownloadFile = res
return nil
}
// chkResumeFile : Check file and file size of local file.
func (v *valResumableDownload) chkResumeFile() (bool, bool, error) {
if v.Filename == "" {
v.Filename = v.DownloadFile.Name
}
f, err := os.Stat(filepath.Join(v.WorkDir, v.Filename))
if err != nil {
v.CurrentFileSize = 0
v.Start = 0
v.End = func() int64 {
if v.DownloadBytes >= v.DownloadFile.Size {
return v.DownloadFile.Size - 1
}
return v.DownloadBytes - 1
}()
v.Range = fmt.Sprintf("bytes=0-%d", v.End)
v.Size = v.End + 1
return false, false, nil
}
fs := f.Size()
v.CurrentFileSize = fs
if fs == v.DownloadFile.Size {
return false, true, nil
} else if fs > v.DownloadFile.Size {
return false, false, fmt.Errorf("size of download file is larger than that of local file. Please confirm the file and URL. FileName is %s. Download URL is %s", v.Filename, v.URL)
}
v.Start = fs
v.End = func() int64 {
if fs+v.DownloadBytes >= v.DownloadFile.Size {
return v.DownloadFile.Size - 1
}
return fs + v.DownloadBytes - 1
}()
v.Range = fmt.Sprintf("bytes=%d-%d", v.Start, v.End)
v.Size = v.End - v.Start + 1
return true, false, nil
}
// setIndent : Set indent of each element using the maximum length of element.
// st is 2 dimensional array including values.
// k is the index of each element for setting indent.
func setIndent(st [][]string, k int) [][]string {
maxLen := func(max int) int {
for _, e := range st {
if len(e[k]) > max {
max = len(e[k])
}
}
return max
}(0)
for i, e := range st {
spaces := func(l int) string {
temp := make([]string, l)
for i := range temp {
temp[i] = " "
}
return strings.Join(temp[:], "")
}(maxLen - len(e[k]))
st[i][k] = e[k] + spaces
}
return st
}
// getMsg : Convert 2D array to string using delimiter.
func getMsg(st [][]string, delim string) string {
var temp []string
for _, e := range st {
temp = append(temp, strings.Join(e, delim))
}
return strings.Join(temp, "\n")
}
// getMd5Checksum : Get md5checksum
func getMd5Checksum(fileName string) (string, error) {
f, err := os.Open(fileName)
if err != nil {
return "", err
}
defer f.Close()
ha := md5.New()
if _, err := io.Copy(ha, f); err != nil {
return "", err
}
return hex.EncodeToString(ha.Sum(nil)), nil
}
// getStatusMsg : Get status message.
func (v *valResumableDownload) getStatusMsg(fc, end bool) string {
switch {
case !fc && !end:
st := [][]string{
{"Current status", "New download"},
{"Save filename", v.Filename},
{"Filename in Google Drive", v.DownloadFile.Name},
{"Current file size of local [bytes]", strconv.FormatInt(v.CurrentFileSize, 10)},
{"File size of Google Drive [bytes]", strconv.FormatInt(v.DownloadFile.Size, 10)},
{"This download size [bytes]", strconv.FormatInt(v.Size, 10)},
}
return getMsg(setIndent(st, 0), " : ")
case fc && !end:
st := [][]string{
{"Current status", "Resumable download"},
{"Save filename", v.Filename},
{"Filename in Google Drive", v.DownloadFile.Name},
{"Current file size of local [bytes]", strconv.FormatInt(v.CurrentFileSize, 10)},
{"File size of Google Drive [bytes]", strconv.FormatInt(v.DownloadFile.Size, 10)},
{"This download size [bytes]", strconv.FormatInt(v.Size, 10)},
}
return getMsg(setIndent(st, 0), " : ")
case !fc && end:
cs, err := getMd5Checksum(filepath.Join(v.WorkDir, v.Filename))
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
st := [][]string{
{"Current status", "Download has already done."},
{"Save filename", v.Filename},
{"Filename in Google Drive", v.DownloadFile.Name},
{"Current file size of local [bytes]", strconv.FormatInt(v.CurrentFileSize, 10)},
{"File size of Google Drive [bytes]", strconv.FormatInt(v.DownloadFile.Size, 10)},
{"md5checksum at Google Drive", v.DownloadFile.Md5Checksum},
{"md5checksum at Local Drive", cs},
}
return getMsg(setIndent(st, 0), " : ")
default:
return fmt.Sprintln("unknown error")
}
}
// resumableDownload : Main method of resumable download.
func (p *para) resumableDownload() error {
v := &valResumableDownload{
para: *p,
}
if err := v.getFileInf(); err != nil {
return err
}
if strings.Contains(v.DownloadFile.MimeType, "application/vnd.google-apps") {
return fmt.Errorf("a Google Docs file cannot be resumable downloaded")
}
fc, end, err := v.chkResumeFile()
if err != nil {
return err
}
msg := v.getStatusMsg(fc, end)
if (!fc && !end) || (fc && !end) {
fmt.Printf("\n%s\n\n", msg)
var input string
fmt.Printf("Do you start this download? [y or n] ... ")
if _, err := fmt.Scan(&input); err != nil {
return err
}
if input == "y" {
res, err := v.resDownloadFileByAPIKey()
if err != nil {
return err
}
return v.para.saveFile(res)
}
} else {
fmt.Printf("\n%s\n", msg)
}
return nil
}