Synchronize document title with Google Drive real time synchronization
This commit is contained in:
parent
baacf582fa
commit
4616faefe1
@ -194,6 +194,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;
|
||||
var task = new AsyncTask();
|
||||
|
@ -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;
|
||||
}
|
||||
@ -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) {
|
||||
var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]);
|
||||
googleHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) {
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user