diff options
| author | Petter Rasmussen | 2016-01-23 17:22:37 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-01-23 17:22:37 +0100 |
| commit | a4217d488c382bee3e9f7faf33ec3d139ba54cd5 (patch) | |
| tree | 6e359d48ca385b9abfcc300eca5f1b8834f6d249 /drive | |
| parent | e3aa4296e9fc875866d907df341637db4ac8c815 (diff) | |
| download | gdrive-a4217d488c382bee3e9f7faf33ec3d139ba54cd5.tar.bz2 | |
Implement update
Diffstat (limited to 'drive')
| -rw-r--r-- | drive/update.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/drive/update.go b/drive/update.go new file mode 100644 index 0000000..4fa33ed --- /dev/null +++ b/drive/update.go @@ -0,0 +1,71 @@ +package drive + +import ( + "fmt" + "mime" + "os" + "io" + "path/filepath" + "google.golang.org/api/drive/v3" + "golang.org/x/net/context" +) + +type UpdateArgs struct { + Out io.Writer + Id string + Path string + Name string + Parents []string + Mime string + Recursive bool + Stdin bool + Share bool + NoProgress bool +} + +func (self *Drive) Update(args UpdateArgs) (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) + } + + 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.Update(args.Id, 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 +} |
