| 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
 | package drive
import (
    "fmt"
    "mime"
    "os"
    "io"
    "path/filepath"
    "google.golang.org/api/googleapi"
    "google.golang.org/api/drive/v3"
    "golang.org/x/net/context"
)
type UploadFileArgs struct {
    Out io.Writer
    Path string
    Name string
    Parents []string
    Mime string
    Recursive bool
    Share bool
    NoProgress bool
}
func (self *Drive) Upload(args UploadFileArgs) (err error) {
    srcFile, err := os.Open(args.Path)
    if err != nil {
        return fmt.Errorf("Failed to open file: %s", err)
    }
    srcFileInfo, err := srcFile.Stat()
    if err != nil {
        return fmt.Errorf("Failed to read file metadata: %s", err)
    }
    // Instantiate empty drive file
    dstFile := &drive.File{}
    // Use provided file name or use filename
    if args.Name == "" {
        dstFile.Name = filepath.Base(srcFileInfo.Name())
    } else {
        dstFile.Name = args.Name
    }
    // Set provided mime type or get type based on file extension
    if args.Mime == "" {
        dstFile.MimeType = mime.TypeByExtension(filepath.Ext(dstFile.Name))
    } else {
        dstFile.MimeType = args.Mime
    }
    // Set parent folders
    dstFile.Parents = args.Parents
    f, err := self.service.Files.Create(dstFile).ResumableMedia(context.Background(), srcFile, srcFileInfo.Size(), dstFile.MimeType).Do()
    if err != nil {
        return fmt.Errorf("Failed to upload file: %s", err)
    }
    fmt.Fprintf(args.Out, "Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size)
    //if args.Share {
    //    self.Share(TODO)
    //}
    return
}
type UploadStreamArgs struct {
    Out io.Writer
    In io.Reader
    Name string
    Parents []string
    Mime string
    Share bool
    ChunkSize int64
}
func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
    if args.ChunkSize > intMax() - 1 {
        return fmt.Errorf("Chunk size is to big, max chunk size for this computer is %d", intMax() - 1)
    }
    // Instantiate empty drive file
    dstFile := &drive.File{Name: args.Name}
    // Set mime type if provided
    if args.Mime != "" {
        dstFile.MimeType = args.Mime
    }
    // Set parent folders
    dstFile.Parents = args.Parents
    // Chunk size option
    chunkSize := googleapi.ChunkSize(int(args.ChunkSize))
    f, err := self.service.Files.Create(dstFile).Media(args.In, chunkSize).Do()
    if err != nil {
        return fmt.Errorf("Failed to upload file: %s", err)
    }
    fmt.Fprintf(args.Out, "Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size)
    //if args.Share {
    //    self.Share(TODO)
    //}
    return
}
 |