Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New file or folder #246

Merged
merged 2 commits into from Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,7 @@
- [Listing Files](#listing-files)
- [Stating Files](#stating-files)
- [Retrieving md5 checksums](#retrieving-md5-checksums)
- [New File](#new-file)
- [Quota](#quota)
- [Features](#features)
- [About](#about)
Expand Down Expand Up @@ -560,6 +561,19 @@ Compare across two different Drive accounts, including subfolders

_Note: Running the 'drive md5sum' command retrieves pre-computed md5 sums from Drive; its speed is proportional to the number of files on Drive. Running the shell 'md5sum' command on local files requires reading through the files; its speed is proportional to the size of the files._


### New File

drive allows you to create an empty file or folder remotely
Sample usage:

```shell
$ drive new --folder flux
$ drive new --mime-key doc bofx
$ drive new --mime-key folder content
$ drive new flux.txt oxen.pdf # Allow auto type resolution from the extension
```

### Quota

The `quota` command prints information about your drive, such as the account type, bytes used/free, and the total amount of storage available.
Expand Down
32 changes: 32 additions & 0 deletions cmd/drive/main.go
Expand Up @@ -76,6 +76,7 @@ func main() {
bindCommandWithAliases(drive.DeleteKey, drive.DescDelete, &deleteCmd{}, []string{})
bindCommandWithAliases(drive.UnpubKey, drive.DescUnpublish, &unpublishCmd{}, []string{})
bindCommandWithAliases(drive.VersionKey, drive.Version, &versionCmd{}, []string{})
bindCommandWithAliases(drive.NewKey, drive.DescNew, &newCmd{}, []string{})

command.DefineHelp(&helpCmd{})
command.ParseAndRun()
Expand Down Expand Up @@ -781,6 +782,37 @@ func (cmd *trashCmd) Run(args []string) {
}
}

type newCmd struct {
folder *bool
mimeKey *string
}

func (cmd *newCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.folder = fs.Bool("folder", false, "create a folder if set otherwise create a regular file")
cmd.mimeKey = fs.String(drive.MimeKey, "", "coerce the file to this mimeType")
return fs
}

func (cmd *newCmd) Run(args []string) {
sources, context, path := preprocessArgs(args)
opts := drive.Options{
Path: path,
Sources: sources,
}

meta := map[string][]string{
drive.MimeKey: drive.NonEmptyTrimmedStrings(strings.Split(*cmd.mimeKey, ",")...),
}

opts.Meta = &meta

if *cmd.folder {
exitWithError(drive.New(context, &opts).NewFolder())
} else {
exitWithError(drive.New(context, &opts).NewFile())
}
}

type copyCmd struct {
quiet *bool
recursive *bool
Expand Down
4 changes: 4 additions & 0 deletions src/help.go
Expand Up @@ -46,6 +46,7 @@ const (
UnpubKey = "unpub"
VersionKey = "version"
Md5sumKey = "md5sum"
NewKey = "new"

CoercedMimeKeyKey = "coerced-mime"
DepthKey = "depth"
Expand Down Expand Up @@ -75,6 +76,8 @@ const (
ExactOwnerKey = "exact-owner"
NotOwnerKey = "skip-owner"
SortKey = "sort"
FolderKey = "folder"
MimeKey = "mime-key"
DriveRepoRelPath = "github.com/odeke-em/drive"
)

Expand Down Expand Up @@ -121,6 +124,7 @@ const (
DescMatchOwner = "elements with matching owners"
DescExactOwner = "elements with the exact owner"
DescNotOwner = "ignore elements owned by these users"
DescNew = "create a new file/folder"
)

const (
Expand Down
3 changes: 3 additions & 0 deletions src/misc.go
Expand Up @@ -442,6 +442,9 @@ func cacher(regMap map[*regexp.Regexp]string) func(string) string {
var mimeTypeFromQuery = cacher(regMapper(regExtStrMap, map[string]string{
"folder": DriveFolderMimeType,
"mp4": "video/mp4",
"docs": "application/vnd.google-apps.document",
"sheet": "application/vnd.google-apps.sheet",
"form": "application/vnd.google-apps.form",
}))

var mimeTypeFromExt = cacher(regMapper(regExtStrMap))
Expand Down
79 changes: 79 additions & 0 deletions src/new.go
@@ -0,0 +1,79 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package drive

import (
"path/filepath"
"time"
)

func (g *Commands) NewFolder() (err error) {
return _newFile(g, true)
}

func (g *Commands) NewFile() (err error) {
return _newFile(g, false)
}

func _newFile(g *Commands, folder bool) (err error) {
var coercedMimeKey string
if g.opts.Meta != nil {
meta := *(g.opts.Meta)
mimeKeys, ok := meta[MimeKey]
if ok {
coercedMimeKey = sepJoin("", mimeKeys...)
}
}

for _, relToRootPath := range g.opts.Sources {
parentPath, basename := g.pathSplitter(relToRootPath)

parent, parErr := g.remoteMkdirAll(parentPath)
if parErr != nil {
g.log.LogErrf("newFile: %s %v\n", relToRootPath, parErr)
continue
}

f := &File{
ModTime: time.Now(),
Name: urlToPath(basename, false),
}

if folder {
f.IsDir = true
} else {
mimeKey := coercedMimeKey
if coercedMimeKey == "" {
mimeKey = filepath.Ext(relToRootPath)
}
f.MimeType = mimeTypeFromQuery(mimeKey)
}

upArg := upsertOpt{
parentId: parent.Id,
src: f,
}

freshFile, _, fErr := g.rem.upsertByComparison(nil, &upArg)
if fErr != nil {
g.log.LogErrf("newFile: %s creation failed %v\n", relToRootPath, fErr)
continue
}

g.log.Logf("%s %s\n", relToRootPath, freshFile.Id)
}

return err
}
4 changes: 4 additions & 0 deletions src/remote.go
Expand Up @@ -445,6 +445,10 @@ func (r *Remote) upsertByComparison(body io.Reader, args *upsertOpt) (f *File, m
uploaded.MimeType = DriveFolderMimeType
}

if args.src.MimeType != "" {
uploaded.MimeType = args.src.MimeType
}

if args.mimeKey != "" {
uploaded.MimeType = guessMimeType(args.mimeKey)
}
Expand Down