Skip to content

Commit

Permalink
add some tests with credentials outside the file tree of course;
Browse files Browse the repository at this point in the history
cache test results in ./testResults.txt
  • Loading branch information
DrPaulBrewer committed Oct 10, 2017
1 parent 85e1785 commit 0e40953
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ coverage
*.log
*~
test.json
test.keys
test/test.json
test/test.keys
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ Initialize googleapi's Google Drive[tm] client, decorated with some useful 3rd p
// clobber, boolean, must be true to replace an existing file, in which case all files matching folderPath+name will be deleted.
// clobber = false/undefined will reject when the file exists with Error("drive.x.upload2: file exists and clobber not set")
// on success, resolves to metadata of newly uploaded file


## Tests

I'm going to try to stay sane and not post a set of encrypted API keys and tokens to get a green "build passing" badge.

Instead, you can read my test results in [testResults.txt](./testResults.txt), and/or set up your own testing.

Current tests are nowhere near complete, but do demonstrate some basic functionality. From using this over a period of hours, access tokens are being refreshed automatically.

## License: MIT

Expand Down
29 changes: 22 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2017 Paul Brewer - Economic and Financial Technology Consulting LLC <drpaulbrewer@eaftc.com>
// License: MIT

// jshint esversion:6, strict:global, node:true

"use strict";
Expand Down Expand Up @@ -78,13 +81,14 @@ function extensions(drive, request){
return pify(drive.files.delete)({fileId: file.id});
}
return function(files){
if (Array.isArray(files))
if (files && files.id) files = [files];
if ((Array.isArray(files)) && (files.length>0))
return (Promise
.all(files.map(deleteFile))
.then(()=>(returnVal))
);
else
return Promise.resolve();
return Promise.resolve(returnVal);
};
}

Expand Down Expand Up @@ -172,7 +176,7 @@ function extensions(drive, request){

x.reader = driveReader;

function driveDownloader( rootFolderId){
function driveDownloader(rootFolderId){
const spaces = (rootFolderId === 'appDataFolder')? rootFolderId : 'drive';
const reader = driveReader( spaces);
return function(path){
Expand Down Expand Up @@ -255,7 +259,15 @@ function extensions(drive, request){
return new Promise(function(resolve,reject){
const uploadRequest = request(driveupload, (err, httpIncomingMessage, response)=>{
if (err) return reject(err);
resolve(response);
let result;
if (typeof(response)==='string'){
try {
result = JSON.parse(response);
} catch(err){ result = response; }
} else {
result = response;
}
resolve(result);
});
localStream.pipe(uploadRequest);
});
Expand Down Expand Up @@ -288,7 +300,8 @@ function extensions(drive, request){
const findAll = driveFileFinder( null, true);
const getFolder = (createPath)? driveCreatePath( rootFolderId, folderPath): driveFindPath( rootFolderId, folderPath);
function go(parent){
const pUploadUrl = driveUploadDirector( parent);
if (parent===undefined) throw new Error("in upload2: go, parent is undefined");
const pUploadUrl = driveUploadDirector(parent);
return (
pUploadUrl({name, mimeType})
.then(streamToUrl(stream, mimeType))
Expand All @@ -304,9 +317,11 @@ function extensions(drive, request){
);

if (clobber){
const janitor = driveJanitor();
return (common
.then(driveJanitor(c.parent))
.then(go)
.catch((e)=>{ if (e===404) return Promise.resolve([]); return Promise.reject(e); })
.then(janitor)
.then(()=>{return go(c.parent);})
);
}

Expand Down
Empty file added package
Empty file.
Loading

0 comments on commit 0e40953

Please sign in to comment.