aboutsummaryrefslogtreecommitdiffstats
path: root/drive/upload.go
diff options
context:
space:
mode:
Diffstat (limited to 'drive/upload.go')
-rw-r--r--drive/upload.go47
1 files changed, 42 insertions, 5 deletions
diff --git a/drive/upload.go b/drive/upload.go
index d57e020..5c5e38e 100644
--- a/drive/upload.go
+++ b/drive/upload.go
@@ -6,6 +6,7 @@ import (
"os"
"io"
"path/filepath"
+ "google.golang.org/api/googleapi"
"google.golang.org/api/drive/v3"
"golang.org/x/net/context"
)
@@ -17,16 +18,11 @@ type UploadFileArgs struct {
Parents []string
Mime string
Recursive bool
- Stdin bool
Share bool
NoProgress bool
}
func (self *Drive) Upload(args UploadFileArgs) (err error) {
- //if args.Stdin {
- // self.uploadStdin()
- //}
-
srcFile, err := os.Open(args.Path)
if err != nil {
return fmt.Errorf("Failed to open file: %s", err)
@@ -68,3 +64,44 @@ func (self *Drive) Upload(args UploadFileArgs) (err error) {
//}
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
+}