Fix setting of local mtime when remote file change matches local file

This commit is contained in:
Dan Stillman 2016-03-16 02:01:51 -04:00
parent 8df6b4bbd3
commit 28dc7d17e2
4 changed files with 64 additions and 6 deletions

View File

@ -577,13 +577,10 @@ Zotero.Sync.Storage.Local = {
// Set the file mtime to the time from the server
yield OS.File.setDates(path, null, new Date(parseInt(mtime)));
item.attachmentSyncState = this.SYNC_STATE_IN_SYNC;
item.attachmentSyncedModificationTime = mtime;
item.attachmentSyncedHash = md5;
yield item.saveTx({
skipDateModifiedUpdate: true,
skipSelect: true
});
item.attachmentSyncState = "in_sync";
yield item.saveTx({ skipAll: true });
return new Zotero.Sync.Storage.Result({
localChanges: true

View File

@ -156,6 +156,8 @@ Zotero.Sync.Storage.StreamListener.prototype = {
if (!result) {
oldChannel.cancel(Components.results.NS_BINDING_ABORTED);
newChannel.cancel(Components.results.NS_BINDING_ABORTED);
Zotero.debug("Cancelling redirect");
// TODO: Prevent onStateChange error
return false;
}
}

View File

@ -99,7 +99,7 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
var header;
try {
header = "Zotero-File-Modification-Time";
requestData.mtime = oldChannel.getResponseHeader(header);
requestData.mtime = parseInt(oldChannel.getResponseHeader(header));
header = "Zotero-File-MD5";
requestData.md5 = oldChannel.getResponseHeader(header);
header = "Zotero-File-Compressed";
@ -131,6 +131,7 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
}
// Update local metadata and stop request, skipping file download
yield OS.File.setDates(path, null, new Date(requestData.mtime));
item.attachmentSyncedModificationTime = requestData.mtime;
if (updateHash) {
item.attachmentSyncedHash = requestData.md5;
@ -138,6 +139,10 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
item.attachmentSyncState = "in_sync";
yield item.saveTx({ skipAll: true });
deferred.resolve(new Zotero.Sync.Storage.Result({
localChanges: true
}));
return false;
}),
onProgress: function (a, b, c) {

View File

@ -561,6 +561,60 @@ describe("Zotero.Sync.Storage.Mode.ZFS", function () {
assert.equal(item2.version, 15);
})
it("should update local info for remotely updated file that matches local file", function* () {
var { engine, client, caller } = yield setup();
var library = Zotero.Libraries.userLibrary;
library.libraryVersion = 5;
yield library.saveTx();
library.storageDownloadNeeded = true;
var file = getTestDataDirectory();
file.append('test.txt');
var item = yield Zotero.Attachments.importFromFile({ file });
item.version = 5;
item.attachmentSyncState = "to_download";
yield item.saveTx();
var path = yield item.getFilePathAsync();
yield OS.File.setDates(path, null, new Date() - 100000);
var json = item.toJSON();
yield Zotero.Sync.Data.Local.saveCacheObject('item', item.libraryID, json);
var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) + "";
var md5 = Zotero.Utilities.Internal.md5(file)
var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
if (!request.hasHeader('Zotero-API-Key')) {
response.setStatusLine(null, 403, "Forbidden");
return;
}
var key = request.getHeader('Zotero-API-Key');
if (key != apiKey) {
response.setStatusLine(null, 403, "Invalid key");
return;
}
response.setStatusLine(null, 302, "Found");
response.setHeader("Zotero-File-Modification-Time", mtime, false);
response.setHeader("Zotero-File-MD5", md5, false);
response.setHeader("Zotero-File-Compressed", "No", false);
response.setHeader("Location", baseURL + s3Path, false);
}
}
);
var result = yield engine.start();
assert.equal(item.attachmentSyncedModificationTime, mtime);
yield assert.eventually.equal(item.attachmentModificationTime, mtime);
assert.isTrue(result.localChanges);
assert.isFalse(result.remoteChanges);
assert.isFalse(result.syncRequired);
})
it("should update local info for file that already exists on the server", function* () {
var { engine, client, caller } = yield setup();