Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions google_drive_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function readLinesSync(msgs, callback)
{
msgs[i] = readLineSync(msgs[i]);
}

callback(msgs);
}

Expand All @@ -37,17 +37,17 @@ function askAccessToken(settings, oauth, callback) {
});

console.log('Visit the url: ', url);

var code = readLineSync('Enter the code here:');
// request access token
oauth.getToken(code, callback);
}

function saveAccessToken(callback) {
var oauthSettings = JSON.parse(fs.readFileSync(OAUTH_SETTING_FILE, 'utf8'));

var oauth = createOAuth2Client(oauthSettings);

askAccessToken( oauthSettings,
oauth,
function(err, tokens) {
Expand All @@ -66,25 +66,25 @@ function readToken(callback)
{
var oauthSettings = JSON.parse(fs.readFileSync(OAUTH_SETTING_FILE, 'utf8'));
var token = fs.readFileSync(OAUTH_FILE, 'utf8');

var oauth = createOAuth2Client(oauthSettings);
oauth.setCredentials(JSON.parse(token));

callback(oauth);
}

function readTokenSync()
{
var oauth;

readToken(function(_oauth) {
oauth = _oauth;
});

while (oauth === undefined) {
desync.runLoopOnce();
}

return oauth;
}

Expand All @@ -97,7 +97,7 @@ function getFolderInfo(oauth, folderId, path, callback)
{
var args = { auth: oauth,
folderId: folderId};

drive.children.list( args,
function(err, res) {
if (err) {
Expand All @@ -118,7 +118,7 @@ function getFileInfo(oauth, fileId, path, callback)
{
var args = { auth: oauth,
fileId: fileId};

drive.files.get( args,
function(err, res) {
if (err) {
Expand Down Expand Up @@ -152,6 +152,17 @@ function createFolder(oauth, folderId, name, callback)
});
}

function downloadFile(oauth, fileId, callback) {
var args = {
auth: oauth,
fileId: fileId,
}

drive.files.get(args, function(err, res) {
_defaultCallback(err, res, callback);
});
}

function renameFile(oauth, fileId, name, callback)
{
var args = { auth: oauth,
Expand All @@ -167,10 +178,10 @@ function duplicateFile(oauth, fileId, callback)
{
var args = { auth: oauth,
fileId: fileId};

drive.files.copy( args,
function(err, res) {
_defaultCallback(err, res, callback);
_defaultCallback(err, res, callback);
});
}

Expand All @@ -179,7 +190,7 @@ function moveFile(oauth, fileId, folderId, callback)
var args = { auth: oauth,
fileId: fileId,
resource: { "parents" : [{"id": folderId}]}};

drive.files.update( args,
function(err, res) {
_defaultCallback(err, res, callback);
Expand Down Expand Up @@ -237,7 +248,7 @@ function searchFilesByTitle(oauth, keyword, max_results, callback)
{
args["maxResults"] = max_results;
}

drive.files.list( args,
function(err, res) {
_defaultCallback(err, res, callback);
Expand All @@ -261,3 +272,4 @@ module.exports.duplicateFile = duplicateFile;
module.exports.moveFile = moveFile;
module.exports.copyFile = copyFile;
module.exports.copyFolder = copyFolder;
module.exports.downloadFile = downloadFile;