Skip to content

Commit

Permalink
added folderId handling feature to upload2; also found and fixed pote…
Browse files Browse the repository at this point in the history
…ntial clobber:true bug and removed "checkDuplicates" unneeded code
  • Loading branch information
DrPaulBrewer committed Dec 23, 2017
1 parent cb153b2 commit db537fe
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 94 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ Drive retains the corrupted upload.
We haven't tried disrupting the upload and then trying to resume it. It is done in one chunk and seems to deal
with 50 Mb zip files ok.

As of `decorated-google-drive:2.1.0` It is also possible to set `folderId` to a Drive folder.id string instead of setting `folderPath` to a path string.

### getting a URL for resumable upload later

If you want to manage the resumable uploads, this creates a 0 byte file and retrieves a resumable upload URL for later use.
Expand Down
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ const folderMimeType = 'application/vnd.google-apps.folder';
// request = require('request')

function decoratedGoogleDrive(googleapis, request, keys, tokens){
if (!googleapis)
throw new Error("googleapis not defined");
if (!googleapis.auth)
throw new Error("googleapis.auth not defined");
if (!googleapis.auth.OAuth2)
throw new Error("googleapis.auth.OAuth2 not defined");
const OAuth2 = googleapis.auth.OAuth2;
const auth = new OAuth2(keys.key, keys.secret, keys.redirect);
auth.setCredentials(tokens);
const drive = googleapis.drive({version: 'v3', auth});
if (typeof(drive)!=='object')
throw new Error("drive is not an object, got: "+typeof(drive));
return decorate(drive, request);
}

Expand Down Expand Up @@ -307,26 +315,16 @@ function extensions(drive, request, rootFolderId, spaces){

x.streamToUrl = streamToUrl;

function checkDuplicates(files){
if ((!files) || (files.length===0))
throw(404);
if (files.length>1)
throw(new Error("checkDuplicates: failed, multiple files with same name"));
return files[1];
}

x.checkDuplicates = checkDuplicates;

function upload2({folderPath, name, stream, mimeType, createPath, clobber}){
function upload2({folderPath, folderId, name, stream, mimeType, createPath, clobber}){
function requireString(v, l, k){
if ((typeof(v)!=='string') || (v.length<l))
throw new Error("drive.x.upload2, invalid parameter "+k+", requires string of length at least "+l+" chars");
}
requireString(folderPath, 0, 'folderPath');
requireString(name,1,'name');
requireString(mimeType,1,'mimeType');
if (folderPath && folderId) throw new Boom.badRequest("bad request, specify folderPath or folderId, not both");
const findAll = driveSearcher({});
const getFolder = (createPath)? (driveCreatePath(folderPath)) : (driveFindPath(folderPath));
const getFolder = (createPath)? (driveCreatePath(folderPath)) : ( (folderId && Promise.resolve(folderId)) || driveFindPath(folderPath));
function go({parent}){
if (parent===undefined) throw Boom.badImplementation("parent undefined");
const pUploadUrl = driveUploadDirector(parent);
Expand All @@ -353,7 +351,7 @@ function extensions(drive, request, rootFolderId, spaces){
.then(({parent, files})=>{
if (files.length>0)
throw Boom.conflict('file exists');
go({parent});
return go({parent});
})
);
}
Expand Down
131 changes: 67 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"googleapis": "^22.2.0",
"mocha": "^4.0.1",
"request": "^2.83.0",
"should": "^13.1.1",
"should": "^13.1.3",
"string-to-stream": "^1.1.0"
},
"dependencies": {
"boom": "^6.0.0",
"boom": "^7.1.1",
"digest-stream": "^2.0.0",
"p-reduce": "^1.0.0",
"pify": "^3.0.0",
Expand Down
Loading

0 comments on commit db537fe

Please sign in to comment.