Import document from Google Drive

This commit is contained in:
benweet 2013-04-03 19:35:25 +01:00
parent e7800be299
commit 19c6c02786
2 changed files with 27 additions and 27 deletions

View File

@ -295,11 +295,7 @@ define(["jquery", "core", "gdrive", "synchronizer", "async-runner"], function($,
if(fileId.length === 0) { if(fileId.length === 0) {
return; return;
} }
gdrive.downloadMetadata([fileId], function(result) { gdrive.importFiles([fileId]);
if(result === undefined || result.length === 0) {
return;
}
});
} }
function refreshManageSync() { function refreshManageSync() {

View File

@ -254,8 +254,8 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
message: jqXHR.statusText message: jqXHR.statusText
}; };
// Handle error // Handle error
if(error.code === 403) { if(error.code === 403 || error.code === 404) {
error = "File is not available"; error = "File is not available.";
} }
handleError(error, asyncTask, callback); handleError(error, asyncTask, callback);
}); });
@ -365,6 +365,27 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
gdrive.updateFile = function(id, title, content, callback) { gdrive.updateFile = function(id, title, content, callback) {
upload(id, undefined, title, content, callback); upload(id, undefined, title, content, callback);
}; };
gdrive.importFiles = function(ids) {
gdrive.downloadMetadata(ids, function(result) {
if(result === undefined) {
return;
}
gdrive.downloadContent(result, function(result) {
if(result === undefined) {
return;
}
for(var i=0; i<result.length; i++) {
var file = result[i];
fileSyncIndex = SYNC_PROVIDER_GDRIVE + file.id;
localStorage[fileSyncIndex + ".etag"] = file.etag;
var fileIndex = fileManager.createFile(file.title, file.content, [fileSyncIndex]);
fileManager.selectFile(fileIndex);
core.showMessage('"' + file.title + '" imported successfully from Google Drive.');
}
});
});
};
gdrive.init = function(fileManagerModule) { gdrive.init = function(fileManagerModule) {
fileManager = fileManagerModule; fileManager = fileManagerModule;
@ -386,7 +407,7 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
}); });
} }
else if (state.action == "open") { else if (state.action == "open") {
var importIds = []; var ids = [];
for(var i=0; i<state.ids.length; i++) { for(var i=0; i<state.ids.length; i++) {
var id = state.ids[i]; var id = state.ids[i];
var fileSyncIndex = SYNC_PROVIDER_GDRIVE + id; var fileSyncIndex = SYNC_PROVIDER_GDRIVE + id;
@ -394,27 +415,10 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
if(fileIndex !== undefined) { if(fileIndex !== undefined) {
fileManager.selectFile(fileIndex); fileManager.selectFile(fileIndex);
} else { } else {
importIds.push(id); ids.push(id);
} }
} }
gdrive.downloadMetadata(importIds, function(result) { gdrive.importFiles(ids);
if(result === undefined) {
return;
}
gdrive.downloadContent(result, function(result) {
if(result === undefined) {
return;
}
for(var i=0; i<result.length; i++) {
var file = result[i];
fileSyncIndex = SYNC_PROVIDER_GDRIVE + file.id;
localStorage[fileSyncIndex + ".etag"] = file.etag;
var fileIndex = fileManager.createFile(file.title, file.content, [fileSyncIndex]);
fileManager.selectFile(fileIndex);
core.showMessage('"' + file.title + '" imported successfully from Google Drive.');
}
});
});
} }
}; };