| 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
 | package drive
import (
    "fmt"
    "io"
    "os"
)
type DownloadFileArgs struct {
    Id string
    Force bool
    NoProgress bool
    Stdout bool
}
func (self *Drive) Download(args DownloadFileArgs) (err error) {
    getFile := self.service.Files.Get(args.Id)
    f, err := getFile.Do()
    if err != nil {
        return fmt.Errorf("Failed to get file: %s", err)
    }
    res, err := getFile.Download()
    if err != nil {
        return fmt.Errorf("Failed to download file: %s", err)
    }
    // Close body on function exit
    defer res.Body.Close()
    if args.Stdout {
        // Write file content to stdout
        _, err := io.Copy(os.Stdout, res.Body)
        return err
    }
    // Check if file exists
    if !args.Force && fileExists(f.Name) {
        return fmt.Errorf("File '%s' already exists, use --force to overwrite", f.Name)
    }
    // Create new file
    outFile, err := os.Create(f.Name)
    if err != nil {
        return fmt.Errorf("Unable to create new file: %s", err)
    }
    // Close file on function exit
    defer outFile.Close()
    // Save file to disk
    bytes, err := io.Copy(outFile, res.Body)
    if err != nil {
        return fmt.Errorf("Failed saving file: %s", err)
    }
    fmt.Printf("Downloaded '%s' at %s, total %d\n", f.Name, "x/s", bytes)
    //if deleteSourceFile {
    //    self.Delete(args.Id)
    //}
    return
}
 |