forked from qiniu/qshell
/
qdownload.go
500 lines (442 loc) · 13.8 KB
/
qdownload.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package qshell
import (
"bufio"
"errors"
"fmt"
"github.com/astaxie/beego/logs"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"qiniu/api.v6/auth/digest"
"qiniu/api.v6/conf"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
/*
{
"dest_dir" : "/Users/jemy/Backup",
"bucket" : "test-bucket",
"prefix" : "demo/",
"suffixes" : ".png,.jpg",
}
*/
const (
MIN_DOWNLOAD_THREAD_COUNT = 1
MAX_DOWNLOAD_THREAD_COUNT = 2000
)
type DownloadConfig struct {
DestDir string `json:"dest_dir"`
Bucket string `json:"bucket"`
Prefix string `json:"prefix,omitempty"`
Suffixes string `json:"suffixes,omitempty"`
//down from cdn
Referer string `json:"referer,omitempty"`
CdnDomain string `json:"cdn_domain,omitempty"`
//log settings
LogLevel string `json:"log_level,omitempty"`
LogFile string `json:"log_file,omitempty"`
LogRotate int `json:"log_rotate,omitempty"`
LogStdout bool `json:"log_stdout,omitempty"`
IsHostFileSpecified bool `json:"-"`
}
var downloadTasks chan func()
var initDownOnce sync.Once
func doDownload(tasks chan func()) {
for {
task := <-tasks
task()
}
}
func QiniuDownload(threadCount int, downConfig *DownloadConfig) {
timeStart := time.Now()
//create job id
jobId := Md5Hex(fmt.Sprintf("%s:%s", downConfig.DestDir, downConfig.Bucket))
//local storage path
storePath := filepath.Join(QShellRootPath, ".qshell", QAccountName, "qdownload", jobId)
if mkdirErr := os.MkdirAll(storePath, 0775); mkdirErr != nil {
logs.Error("Failed to mkdir `%s` due to `%s`", storePath, mkdirErr)
os.Exit(STATUS_ERROR)
}
//init log settings
defaultLogFile := filepath.Join(storePath, fmt.Sprintf("%s.log", jobId))
//init log level
logLevel := logs.LevelInfo
logRotate := 1
if downConfig.LogRotate > 0 {
logRotate = downConfig.LogRotate
}
switch downConfig.LogLevel {
case "debug":
logLevel = logs.LevelDebug
case "info":
logLevel = logs.LevelInfo
case "warn":
logLevel = logs.LevelWarning
case "error":
logLevel = logs.LevelError
default:
logLevel = logs.LevelInfo
}
//init log writer
if downConfig.LogFile == "" {
//set default log file
downConfig.LogFile = defaultLogFile
}
if !downConfig.LogStdout {
logs.GetBeeLogger().DelLogger(logs.AdapterConsole)
}
//open log file
fmt.Println("Writing download log to file", downConfig.LogFile)
//daily rotate
logCfg := BeeLogConfig{
Filename: downConfig.LogFile,
Level: logLevel,
Daily: true,
MaxDays: logRotate,
}
logs.SetLogger(logs.AdapterFile, logCfg.ToJson())
fmt.Println()
account, gErr := GetAccount()
if gErr != nil {
fmt.Println("Get account error,", gErr)
os.Exit(STATUS_ERROR)
}
mac := digest.Mac{account.AccessKey, []byte(account.SecretKey)}
var domainOfBucket string
//get bucket zone info
bucketInfo, gErr := GetBucketInfo(&mac, downConfig.Bucket)
if gErr != nil {
logs.Error("Get bucket region info error,", gErr)
os.Exit(STATUS_ERROR)
}
//get domains of bucket
domainsOfBucket, gErr := GetDomainsOfBucket(&mac, downConfig.Bucket)
if gErr != nil {
logs.Error("Get domains of bucket error,", gErr)
os.Exit(STATUS_ERROR)
}
if len(domainsOfBucket) == 0 {
logs.Error("No domains found for bucket", downConfig.Bucket)
os.Exit(STATUS_ERROR)
}
for _, d := range domainsOfBucket {
if !strings.HasPrefix(d.Domain, ".") {
domainOfBucket = d.Domain
break
}
}
if !downConfig.IsHostFileSpecified {
//set up host
SetZone(bucketInfo.Region)
}
//set proxy
ioProxyAddress := conf.IO_HOST
//check whether cdn domain is set
if downConfig.CdnDomain != "" {
ioProxyAddress = downConfig.CdnDomain
}
//trim http and https prefix
ioProxyAddress = strings.TrimPrefix(ioProxyAddress, "http://")
ioProxyAddress = strings.TrimPrefix(ioProxyAddress, "https://")
if downConfig.CdnDomain != "" {
domainOfBucket = ioProxyAddress
}
if domainOfBucket == "" {
logs.Error("No domains found to download files")
os.Exit(STATUS_ERROR)
}
jobListFileName := filepath.Join(storePath, fmt.Sprintf("%s.list", jobId))
resumeFile := filepath.Join(storePath, fmt.Sprintf("%s.ldb", jobId))
resumeLevelDb, openErr := leveldb.OpenFile(resumeFile, nil)
if openErr != nil {
logs.Error("Open resume record leveldb error", openErr)
os.Exit(STATUS_ERROR)
}
defer resumeLevelDb.Close()
//sync underlying writes from the OS buffer cache
//through to actual disk
ldbWOpt := opt.WriteOptions{
Sync: true,
}
//list bucket, prepare file list to download
logs.Info("Listing bucket `%s` by prefix `%s`", downConfig.Bucket, downConfig.Prefix)
listErr := ListBucket(&mac, downConfig.Bucket, downConfig.Prefix, "", jobListFileName)
if listErr != nil {
logs.Error("List bucket error", listErr)
os.Exit(STATUS_ERROR)
}
//init wait group
downWaitGroup := sync.WaitGroup{}
initDownOnce.Do(func() {
downloadTasks = make(chan func(), threadCount)
for i := 0; i < threadCount; i++ {
go doDownload(downloadTasks)
}
})
//init counters
var totalFileCount int64
var currentFileCount int64
var existsFileCount int64
var updateFileCount int64
var successFileCount int64
var failureFileCount int64
var skipBySuffixes int64
totalFileCount = GetFileLineCount(jobListFileName)
//open prepared file list to download files
listFp, openErr := os.Open(jobListFileName)
if openErr != nil {
logs.Error("Open list file error", openErr)
os.Exit(STATUS_ERROR)
}
defer listFp.Close()
listScanner := bufio.NewScanner(listFp)
listScanner.Split(bufio.ScanLines)
//key, fsize, etag, lmd, mime, enduser
downSuffixes := strings.Split(downConfig.Suffixes, ",")
filterSuffixes := make([]string, 0, len(downSuffixes))
for _, suffix := range downSuffixes {
if strings.TrimSpace(suffix) != "" {
filterSuffixes = append(filterSuffixes, suffix)
}
}
for listScanner.Scan() {
currentFileCount += 1
line := strings.TrimSpace(listScanner.Text())
items := strings.Split(line, "\t")
if len(items) >= 4 {
fileKey := items[0]
if len(filterSuffixes) > 0 {
//filter files by suffixes
var goAhead bool
for _, suffix := range filterSuffixes {
if strings.HasSuffix(fileKey, suffix) {
goAhead = true
break
}
}
if !goAhead {
skipBySuffixes += 1
logs.Info("Skip download `%s`, suffix filter not match", fileKey)
continue
}
}
fileSize, pErr := strconv.ParseInt(items[1], 10, 64)
if pErr != nil {
logs.Error("Invalid list line", line)
continue
}
fileMtime, pErr := strconv.ParseInt(items[3], 10, 64)
if pErr != nil {
logs.Error("Invalid list line", line)
continue
}
fileUrl := makePrivateDownloadLink(&mac, domainOfBucket, ioProxyAddress, fileKey)
//progress
if totalFileCount != 0 {
fmt.Printf("Downloading %s [%d/%d, %.1f%%] ...\n", fileKey, currentFileCount, totalFileCount,
float32(currentFileCount)*100/float32(totalFileCount))
} else {
fmt.Printf("Downloading %s ...\n", fileKey)
}
//check whether log file exists
localFilePath := filepath.Join(downConfig.DestDir, fileKey)
localAbsFilePath, _ := filepath.Abs(localFilePath)
localFilePathTmp := fmt.Sprintf("%s.tmp", localFilePath)
localFileInfo, statErr := os.Stat(localFilePath)
var downNewFile bool
var fromBytes int64
if statErr == nil {
//log file exists, check whether have updates
oldFileInfo, notFoundErr := resumeLevelDb.Get([]byte(localFilePath), nil)
if notFoundErr == nil {
//if exists
oldFileInfoItems := strings.Split(string(oldFileInfo), "|")
oldFileLmd, _ := strconv.ParseInt(oldFileInfoItems[0], 10, 64)
//oldFileSize, _ := strconv.ParseInt(oldFileInfoItems[1], 10, 64)
if oldFileLmd == fileMtime && localFileInfo.Size() == fileSize {
//nothing change, ignore
logs.Info("Local file `%s` exists, same as in bucket, download skip", localAbsFilePath)
existsFileCount += 1
continue
} else {
//somthing changed, must download a new file
logs.Info("Local file `%s` exists, but remote file changed, go to download", localAbsFilePath)
downNewFile = true
}
} else {
if localFileInfo.Size() != fileSize {
logs.Info("Local file `%s` exists, size not the same as in bucket, go to download", localAbsFilePath)
downNewFile = true
} else {
//treat the local file not changed, write to leveldb, though may not accurate
//nothing to do
logs.Warning("Local file `%s` exists with same size as `%s`, treat it not changed", localAbsFilePath, fileKey)
atomic.AddInt64(&existsFileCount, 1)
continue
}
}
} else {
//check whether tmp file exists
localTmpFileInfo, statErr := os.Stat(localFilePathTmp)
if statErr == nil {
//if tmp file exists, check whether last modify changed
oldFileInfo, notFoundErr := resumeLevelDb.Get([]byte(localFilePath), nil)
if notFoundErr == nil {
//if exists
oldFileInfoItems := strings.Split(string(oldFileInfo), "|")
oldFileLmd, _ := strconv.ParseInt(oldFileInfoItems[0], 10, 64)
//oldFileSize, _ := strconv.ParseInt(oldFileInfoItems[1], 10, 64)
if oldFileLmd == fileMtime {
//tmp file exists, file not changed, use range to download
if localTmpFileInfo.Size() < fileSize {
fromBytes = localTmpFileInfo.Size()
} else {
//rename it
renameErr := os.Rename(localFilePathTmp, localFilePath)
if renameErr != nil {
logs.Error("Rename temp file `%s` to final file `%s` error", localFilePathTmp, localFilePath, renameErr)
}
continue
}
} else {
logs.Info("Local tmp file `%s` exists, but remote file changed, go to download", localFilePathTmp)
downNewFile = true
}
} else {
//log tmp file exists, but no record in leveldb, download a new log file
logs.Info("Local tmp file `%s` exists, but no record in leveldb ,go to download", localFilePathTmp)
downNewFile = true
}
} else {
//no log file exists, donwload a new log file
downNewFile = true
}
}
//set file info in leveldb
rKey := localAbsFilePath
rVal := fmt.Sprintf("%d|%d", fileMtime, fileSize)
resumeLevelDb.Put([]byte(rKey), []byte(rVal), &ldbWOpt)
//download new
downWaitGroup.Add(1)
downloadTasks <- func() {
defer downWaitGroup.Done()
downErr := downloadFile(downConfig, fileKey, fileUrl, domainOfBucket, fileSize, fromBytes)
if downErr != nil {
atomic.AddInt64(&failureFileCount, 1)
} else {
atomic.AddInt64(&successFileCount, 1)
if !downNewFile {
atomic.AddInt64(&updateFileCount, 1)
}
}
}
}
}
//wait for all tasks done
downWaitGroup.Wait()
logs.Info("-------Download Result-------")
logs.Info("%10s%10d", "Total:", totalFileCount)
logs.Info("%10s%10d", "Skipped:", skipBySuffixes)
logs.Info("%10s%10d", "Exists:", existsFileCount)
logs.Info("%10s%10d", "Success:", successFileCount)
logs.Info("%10s%10d", "Update:", updateFileCount)
logs.Info("%10s%10d", "Failure:", failureFileCount)
logs.Info("%10s%15s", "Duration:", time.Since(timeStart))
logs.Info("-----------------------------")
fmt.Println("\nSee download log at path", downConfig.LogFile)
if failureFileCount > 0 {
os.Exit(STATUS_ERROR)
}
}
/*
@param ioProxyHost - like http://iovip.qbox.me
*/
func makePrivateDownloadLink(mac *digest.Mac, domainOfBucket, ioProxyAddress, fileKey string) (fileUrl string) {
publicUrl := fmt.Sprintf("http://%s/%s", domainOfBucket, url.PathEscape(fileKey))
deadline := time.Now().Add(time.Hour * 24 * 30).Unix()
privateUrl, _ := PrivateUrl(mac, publicUrl, deadline)
//replace the io proxy host
fileUrl = strings.Replace(privateUrl, domainOfBucket, ioProxyAddress, -1)
return
}
//file key -> mtime
func downloadFile(downConfig *DownloadConfig, fileName, fileUrl, domainOfBucket string, fileSize int64, fromBytes int64) (err error) {
startDown := time.Now().Unix()
destDir := downConfig.DestDir
localFilePath := filepath.Join(destDir, fileName)
localFileDir := filepath.Dir(localFilePath)
localFilePathTmp := fmt.Sprintf("%s.tmp", localFilePath)
mkdirErr := os.MkdirAll(localFileDir, 0775)
if mkdirErr != nil {
err = mkdirErr
logs.Error("MkdirAll failed for", localFileDir, mkdirErr)
return
}
logs.Info("Downloading", fileName, "=>", localFilePath)
//new request
req, reqErr := http.NewRequest("GET", fileUrl, nil)
if reqErr != nil {
err = reqErr
logs.Info("New request", fileName, "failed by url", fileUrl, reqErr)
return
}
//set host
req.Host = domainOfBucket
if downConfig.Referer != "" {
req.Header.Add("Referer", downConfig.Referer)
}
if fromBytes != 0 {
req.Header.Add("Range", fmt.Sprintf("bytes=%d-", fromBytes))
}
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
err = respErr
logs.Info("Download", fileName, "failed by url", fileUrl, respErr)
return
}
defer resp.Body.Close()
if resp.StatusCode/100 == 2 {
var localFp *os.File
var openErr error
if fromBytes != 0 {
localFp, openErr = os.OpenFile(localFilePathTmp, os.O_APPEND|os.O_WRONLY, 0655)
} else {
localFp, openErr = os.Create(localFilePathTmp)
}
if openErr != nil {
err = openErr
logs.Error("Open local file", localFilePathTmp, "failed", openErr)
return
}
cpCnt, cpErr := io.Copy(localFp, resp.Body)
if cpErr != nil {
err = cpErr
localFp.Close()
logs.Error("Download", fileName, "failed", cpErr)
return
}
localFp.Close()
endDown := time.Now().Unix()
avgSpeed := fmt.Sprintf("%.2fKB/s", float64(cpCnt)/float64(endDown-startDown)/1024)
//move temp file to log file
renameErr := os.Rename(localFilePathTmp, localFilePath)
if renameErr != nil {
err = renameErr
logs.Error("Rename temp file to final log file error", renameErr)
return
}
logs.Info("Download", fileName, "=>", localFilePath, "success", avgSpeed)
} else {
err = errors.New("download failed")
logs.Info("Download", fileName, "failed by url", fileUrl, resp.Status)
return
}
return
}