Example #1
0
File: cli.go Project: mberk/gdrive
func uploadDirectory(d *gdrive.Drive, input *os.File, inputInfo os.FileInfo, title string, parentId string, share bool, mimeType string, convert bool) error {
	// Create folder
	folder, err := makeFolder(d, filepath.Base(inputInfo.Name()), parentId, share)
	if err != nil {
		return err
	}

	// Read all files in directory
	files, err := input.Readdir(0)
	if err != nil {
		return err
	}

	// Get current dir
	currentDir, err := os.Getwd()
	if err != nil {
		return err
	}

	// Go into directory
	err = input.Chdir()
	if err != nil {
		return err
	}

	// Change back to original directory when done
	defer func() {
		os.Chdir(currentDir)
	}()

	for _, fi := range files {
		f, err := os.Open(fi.Name())
		if err != nil {
			return err
		}

		if fi.IsDir() {
			err = uploadDirectory(d, f, fi, "", folder.Id, share, mimeType, convert)
		} else {
			err = uploadFile(d, f, fi, "", folder.Id, share, mimeType, convert)
		}

		if err != nil {
			return err
		}
	}

	return nil
}