Synchronize document title with Google Drive real time synchronization

This commit is contained in:
benweet 2013-09-06 00:12:28 +01:00
parent baacf582fa
commit 4616faefe1
3 changed files with 86 additions and 26 deletions

View File

@ -116,11 +116,11 @@ define([
if(parentId) { if(parentId) {
// Specify the directory // Specify the directory
metadata.parents = [ metadata.parents = [
{ {
kind: 'drive#fileLink', kind: 'drive#fileLink',
id: parentId id: parentId
} }
]; ];
} }
var path = '/upload/drive/v2/files'; var path = '/upload/drive/v2/files';
var method = 'POST'; var method = 'POST';
@ -140,18 +140,18 @@ define([
var base64Data = utils.encodeBase64(content); var base64Data = utils.encodeBase64(content);
var multipartRequestBody = [ var multipartRequestBody = [
delimiter, delimiter,
'Content-Type: application/json\r\n\r\n', 'Content-Type: application/json\r\n\r\n',
JSON.stringify(metadata), JSON.stringify(metadata),
delimiter, delimiter,
'Content-Type: ', 'Content-Type: ',
contentType, contentType,
'\r\n', '\r\n',
'Content-Transfer-Encoding: base64\r\n', 'Content-Transfer-Encoding: base64\r\n',
'\r\n', '\r\n',
base64Data, base64Data,
close_delim close_delim
].join(""); ].join("");
var request = gapi.client.request({ var request = gapi.client.request({
'path': path, 'path': path,
@ -194,6 +194,43 @@ define([
task.enqueue(); task.enqueue();
}; };
googleHelper.rename = function(fileId, title, callback) {
var result = undefined;
var task = new AsyncTask();
connect(task);
authenticate(task);
task.onRun(function() {
var body = {'title': title};
var request = gapi.client.drive.files.patch({
'fileId': fileId,
'resource': body
});
request.execute(function(response) {
if(response && response.id) {
// Rename success
result = response;
task.chain();
return;
}
var error = response.error;
// Handle error
if(error !== undefined && fileId !== undefined) {
if(error.code === 404) {
error = 'File ID "' + fileId + '" not found on Google Drive.|removePublish';
}
}
handleError(error, task);
});
});
task.onSuccess(function() {
callback(undefined, result);
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
googleHelper.createRealtimeFile = function(parentId, title, callback) { googleHelper.createRealtimeFile = function(parentId, title, callback) {
var result = undefined; var result = undefined;
var task = new AsyncTask(); var task = new AsyncTask();

View File

@ -114,10 +114,8 @@ define([
}; };
gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) { gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
var syncContentCRC = syncAttributes.contentCRC;
var syncTitleCRC = syncAttributes.titleCRC;
// Skip if CRC has not changed // Skip if CRC has not changed
if(uploadContentCRC == syncContentCRC && uploadTitleCRC == syncTitleCRC) { if(uploadContentCRC == syncAttributes.contentCRC && uploadTitleCRC == syncAttributes.titleCRC) {
callback(undefined, false); callback(undefined, false);
return; return;
} }
@ -133,6 +131,31 @@ define([
}); });
}; };
gdriveProvider.syncUpRealtime = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
var uploadFlag = false;
if(uploadContentCRC != syncAttributes.contentCRC) {
// We don't upload the content since it's a realtime file
syncAttributes.contentCRC = uploadContentCRC;
// But we still inform synchronizer to update syncAttributes
uploadFlag = true;
}
// Skip if title CRC has not changed
if(uploadTitleCRC == syncAttributes.titleCRC) {
callback(undefined, uploadFlag);
return;
}
googleHelper.rename(syncAttributes.id, uploadTitle, function(error, result) {
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
syncAttributes.titleCRC = uploadTitleCRC;
callback(undefined, true);
});
};
gdriveProvider.syncDown = function(callback) { gdriveProvider.syncDown = function(callback) {
var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]); var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]);
googleHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) { googleHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) {

View File

@ -75,14 +75,14 @@ define([
// Dequeue a synchronized location // Dequeue a synchronized location
var syncAttributes = uploadSyncAttributesList.pop(); var syncAttributes = uploadSyncAttributesList.pop();
// Skip real time synchronized location var providerSyncUpFunction = syncAttributes.provider.syncUp;
// Call a special function in case of a real time synchronized location
if(syncAttributes.isRealtime === true) { if(syncAttributes.isRealtime === true) {
locationUp(callback); providerSyncUpFunction = syncAttributes.provider.syncUpRealtime;
return;
} }
// Use the specified provider to perform the upload // Use the specified provider to perform the upload
syncAttributes.provider.syncUp(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, function(error, uploadFlag) { providerSyncUpFunction(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, function(error, uploadFlag) {
if(uploadFlag === true) { if(uploadFlag === true) {
// If uploadFlag is true, request another upload cycle // If uploadFlag is true, request another upload cycle
uploadCycle = true; uploadCycle = true;