Support pre-SingleFile snapshots

This commit is contained in:
Abe Jellinek 2023-07-24 16:02:03 -04:00 committed by Dan Stillman
parent 5d8dd7cdf4
commit b408b43394
2 changed files with 30 additions and 4 deletions

View file

@ -146,6 +146,7 @@ class ReaderInstance {
buf = new Uint8Array(buf).buffer;
let annotationItems = this._item.getAnnotations();
let annotations = (await Promise.all(annotationItems.map(x => this._getAnnotation(x)))).filter(x => x);
let resourceBaseURI = `zotero://attachment/${Zotero.API.getLibraryPrefix(this._item.libraryID)}/items/${this._item.key}/`;
// TODO: Remove after some time
// Migrate Mendeley colors to Zotero PDF reader colors
@ -189,6 +190,7 @@ class ReaderInstance {
type: this._type,
buf: new Uint8Array(buf),
annotations,
resourceBaseURI,
primaryViewState: state,
secondaryViewState: secondViewState,
location,

View file

@ -57,14 +57,18 @@ function ZoteroProtocolHandler() {
/**
* zotero://attachment/library/[itemKey]
* zotero://attachment/groups/[groupID]/[itemKey]
* zotero://attachment/library/items/[itemKey]
* zotero://attachment/groups/[groupID]/items/[itemKey]
*
* And for snapshot attachments only:
* zotero://attachment/library/items/[itemKey]/[resourcePath]
* zotero://attachment/groups/[groupID]/items/[itemKey]/[resourcePath]
*/
var AttachmentExtension = {
loadAsChrome: false,
newChannel: function (uri) {
return new AsyncChannel(uri, function* () {
newChannel: function (uri, loadInfo) {
return new AsyncChannel(uri, loadInfo, function* () {
try {
var uriPath = uri.pathQueryRef;
if (!uriPath) {
@ -100,6 +104,26 @@ function ZoteroProtocolHandler() {
return this._errorChannel(`${path} not found`);
}
var resourcePathParts = uriPath.split('/')
.slice(params.groupID !== undefined ? 4 : 3)
.filter(Boolean);
if (resourcePathParts.length) {
if (!item.isSnapshotAttachment()) {
return this._errorChannel(`Item for ${uriPath} is not a snapshot attachment -- cannot access resources`);
}
try {
path = PathUtils.join(PathUtils.parent(path), ...resourcePathParts);
}
catch (e) {
Zotero.logError(e);
return this._errorChannel(`Resource ${resourcePathParts.join('/')} not found`);
}
if (!(yield IOUtils.exists(path))) {
return this._errorChannel(`Resource ${resourcePathParts.join('/')} not found`);
}
}
// Set originalURI so that it seems like we're serving from zotero:// protocol.
// This is necessary to allow url() links to work from within CSS files.
// Otherwise they try to link to files on the file:// protocol, which isn't allowed.