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) {
// Specify the directory
metadata.parents = [
{
kind: 'drive#fileLink',
id: parentId
}
];
{
kind: 'drive#fileLink',
id: parentId
}
];
}
var path = '/upload/drive/v2/files';
var method = 'POST';
@ -137,22 +137,22 @@ define([
// if(etag !== undefined) {
// headers["If-Match"] = etag;
// }
var base64Data = utils.encodeBase64(content);
var multipartRequestBody = [
delimiter,
'Content-Type: application/json\r\n\r\n',
JSON.stringify(metadata),
delimiter,
'Content-Type: ',
contentType,
'\r\n',
'Content-Transfer-Encoding: base64\r\n',
'\r\n',
base64Data,
close_delim
].join("");
delimiter,
'Content-Type: application/json\r\n\r\n',
JSON.stringify(metadata),
delimiter,
'Content-Type: ',
contentType,
'\r\n',
'Content-Transfer-Encoding: base64\r\n',
'\r\n',
base64Data,
close_delim
].join("");
var request = gapi.client.request({
'path': path,
'method': method,
@ -193,6 +193,43 @@ define([
});
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) {
var result = undefined;

View File

@ -114,10 +114,8 @@ define([
};
gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
var syncContentCRC = syncAttributes.contentCRC;
var syncTitleCRC = syncAttributes.titleCRC;
// Skip if CRC has not changed
if(uploadContentCRC == syncContentCRC && uploadTitleCRC == syncTitleCRC) {
if(uploadContentCRC == syncAttributes.contentCRC && uploadTitleCRC == syncAttributes.titleCRC) {
callback(undefined, false);
return;
}
@ -132,6 +130,31 @@ define([
callback(undefined, true);
});
};
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) {
var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]);

View File

@ -75,14 +75,14 @@ define([
// Dequeue a synchronized location
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) {
locationUp(callback);
return;
providerSyncUpFunction = syncAttributes.provider.syncUpRealtime;
}
// 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 is true, request another upload cycle
uploadCycle = true;