Skip to content

Commit

Permalink
Download missing attachments as needed even in at-sync-time mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dstillman committed Aug 11, 2017
1 parent 3a2f0e6 commit 9202ab8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
3 changes: 1 addition & 2 deletions chrome/content/zotero/zoteroPane.js
Expand Up @@ -4123,8 +4123,7 @@ var ZoteroPane = new function()
}
else {
if (!item.isImportedAttachment()
|| (!Zotero.Sync.Storage.Local.getEnabledForLibrary(item.libraryID)
|| !Zotero.Sync.Storage.Local.downloadAsNeeded(item.libraryID))) {
|| !Zotero.Sync.Storage.Local.getEnabledForLibrary(item.libraryID)) {
this.showAttachmentNotFoundDialog(itemID, noLocateOnMissing);
return;
}
Expand Down
99 changes: 51 additions & 48 deletions test/tests/zoteroPaneTest.js
Expand Up @@ -177,56 +177,59 @@ describe("ZoteroPane", function() {
})

it("should download an attachment on-demand", function* () {
yield setup();
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);

var item = new Zotero.Item("attachment");
item.attachmentLinkMode = 'imported_file';
item.attachmentPath = 'storage:test.txt';
// TODO: Test binary data
var text = Zotero.Utilities.randomString();
item.attachmentSyncState = "to_download";
yield item.saveTx();

var mtime = "1441252524000";
var md5 = Zotero.Utilities.Internal.md5(text)

var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
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);
for (let fn of ['downloadAsNeeded', 'downloadOnSync']) {
yield setup();

Zotero.Sync.Storage.Local[fn](Zotero.Libraries.userLibraryID, true);

var item = new Zotero.Item("attachment");
item.attachmentLinkMode = 'imported_file';
item.attachmentPath = 'storage:test.txt';
// TODO: Test binary data
var text = Zotero.Utilities.randomString();
item.attachmentSyncState = "to_download";
yield item.saveTx();

var mtime = "1441252524000";
var md5 = Zotero.Utilities.Internal.md5(text)

var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
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);
}
}
}
);
this.httpd.registerPathHandler(
"/" + s3Path,
{
handle: function (request, response) {
response.setStatusLine(null, 200, "OK");
response.write(text);
);
this.httpd.registerPathHandler(
"/" + s3Path,
{
handle: function (request, response) {
response.setStatusLine(null, 200, "OK");
response.write(text);
}
}
}
);

// Disable loadURI() so viewAttachment() doesn't trigger translator loading
var stub = sinon.stub(zp, "loadURI");

yield zp.viewAttachment(item.id);

assert.ok(stub.calledOnce);
assert.ok(stub.calledWith(OS.Path.toFileURI(item.getFilePath())));
stub.restore();

assert.equal((yield item.attachmentHash), md5);
assert.equal((yield item.attachmentModificationTime), mtime);
var path = yield item.getFilePathAsync();
assert.equal((yield Zotero.File.getContentsAsync(path)), text);
);
// Disable loadURI() so viewAttachment() doesn't trigger translator loading
var stub = sinon.stub(zp, "loadURI");
yield zp.viewAttachment(item.id);
assert.ok(stub.calledOnce);
assert.ok(stub.calledWith(OS.Path.toFileURI(item.getFilePath())));
stub.restore();
assert.equal((yield item.attachmentHash), md5);
assert.equal((yield item.attachmentModificationTime), mtime);
var path = yield item.getFilePathAsync();
assert.equal((yield Zotero.File.getContentsAsync(path)), text);
}
})
})

Expand Down

0 comments on commit 9202ab8

Please sign in to comment.