diff options
| author | Petter Rasmussen | 2014-09-25 13:39:08 +0200 |
|---|---|---|
| committer | Petter Rasmussen | 2014-09-25 13:39:08 +0200 |
| commit | 558db3d6ec68e190b237f1482a34063dbcc08e53 (patch) | |
| tree | 41e0e5b7d0dde8bc94ca25180ae70022ea95bc5a | |
| parent | 63169fef51dab3ca4f55775640bfe5b202fcc042 (diff) | |
| parent | 18b353d561629acb86eeafa6a0a7773ef0ab7d63 (diff) | |
| download | gdrive-558db3d6ec68e190b237f1482a34063dbcc08e53.tar.bz2 | |
Merge pull request #26 from treeder/master
Ability to upload a folder
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | cli/cli.go | 69 | ||||
| -rw-r--r-- | drive.go | 2 |
3 files changed, 65 insertions, 10 deletions
@@ -62,7 +62,7 @@ If you want to compile from source you need the go toolchain: http://golang.org/ unshare: -i, --id File Id (*) upload: - -f, --file File to upload (*) + -f, --file File or directory to upload (*) -s, --stdin Use stdin as file content (*) -t, --title Title to give uploaded file. Defaults to filename -p, --parent Parent Id of the file @@ -82,7 +82,7 @@ If you want to compile from source you need the go toolchain: http://golang.org/ 0B3X9GlR6EmbnU0ZnbGV4dlk1T00 drive-linux-amd64 5 MB 2013-01-01 21:55:06 0B3X9GlR6EmbncTk1TXlMdjd1ODQ drive-darwin-amd64 5 MB 2013-01-01 21:53:34 -###### Upload file +###### Upload file or directory $ drive upload --file drive-linux-amd64 Id: 0B3X9GlR6EmbnU0ZnbGV4dlk1T00 Title: drive-linux-amd64 @@ -136,6 +136,16 @@ func printInfo(d *gdrive.Drive, f *drive.File) { // Create folder in drive func Folder(d *gdrive.Drive, title string, parentId string, share bool) error { + info, err := makeFolder(d, title, parentId, share) + if err != nil { + return err + } + printInfo(d, info) + fmt.Printf("Folder '%s' created\n", info.Title) + return nil +} + +func makeFolder(d *gdrive.Drive, title string, parentId string, share bool) (*drive.File, error) { // File instance f := &drive.File{Title: title, MimeType: "application/vnd.google-apps.folder"} // Set parent (if provided) @@ -146,24 +156,69 @@ func Folder(d *gdrive.Drive, title string, parentId string, share bool) error { // Create folder info, err := d.Files.Insert(f).Do() if err != nil { - return fmt.Errorf("An error occurred creating the folder: %v\n", err) + return nil, fmt.Errorf("An error occurred creating the folder: %v\n", err) } // Share folder if the share flag was provided if share { Share(d, info.Id) } - printInfo(d, info) - fmt.Printf("Folder '%s' created\n", info.Title) - return nil + return info, err } // Upload file to drive func Upload(d *gdrive.Drive, input io.ReadCloser, title string, parentId string, share bool, mimeType string, convert bool) error { // Use filename or 'untitled' as title if no title is specified + f2, ok := input.(*os.File) if title == "" { - if f, ok := input.(*os.File); ok && input != os.Stdin { - title = filepath.Base(f.Name()) + if ok && input != os.Stdin { + // then find title if it's a file or upload directory if it's a directory (directory can't have a title). + fi, err := f2.Stat() + if err != nil { + return err + } + if fi.Mode().IsDir() { + // then upload the entire directory, calling Upload recursively + // make dir first + folder, err := makeFolder(d, filepath.Base(f2.Name()), parentId, share) + if err != nil { + return err + } + currDir, err := os.Getwd() + if err != nil { + return err + } + + files, err := f2.Readdir(0) + if err != nil { + return err + } + // need to change dirs to get the files in the dir + err = f2.Chdir() + if err != nil { + return err + } + for _, el := range files { + if el.IsDir() { + // todo: recursively do this, would need to keep track of parent ids for new directories + } else { + f2, err := os.Open(el.Name()) + if err != nil { + return err + } + Upload(d, f2, filepath.Base(el.Name()), folder.Id, share, mimeType, convert) + } + } + // go back to previous dir + err = os.Chdir(currDir) + if err != nil { + return err + } + return nil + } + // normal file, not a directory + title = filepath.Base(f2.Name()) + } else { title = "untitled" } @@ -185,7 +240,7 @@ func Upload(d *gdrive.Drive, input io.ReadCloser, title string, parentId string, if convert { fmt.Printf("Converting to Google Docs format enabled\n") } - + info, err := d.Files.Insert(f).Convert(convert).Media(input).Do() if err != nil { return fmt.Errorf("An error occurred uploading the document: %v\n", err) @@ -40,7 +40,7 @@ type Options struct { } `goptions:"folder"` Upload struct { - File *os.File `goptions:"-f, --file, mutexgroup='input', obligatory, rdonly, description='File to upload'"` + File *os.File `goptions:"-f, --file, mutexgroup='input', obligatory, rdonly, description='File or directory to upload'"` Stdin bool `goptions:"-s, --stdin, mutexgroup='input', obligatory, description='Use stdin as file content'"` Title string `goptions:"-t, --title, description='Title to give uploaded file. Defaults to filename'"` ParentId string `goptions:"-p, --parent, description='Parent Id of the file'"` |
