|
| 1 | +import { getZotero } from 'web-common/zotero'; |
| 2 | +import { fetchAllChildItems, fetchChildItems, getAttachmentUrl } from '.'; |
| 3 | +import { cleanURL, get, getDOIURL, getItemFromApiUrl, openDelayedURL } from '../utils'; |
| 4 | +import { makePath } from '../common/navigation'; |
| 5 | +import { PDFWorker } from '../common/pdf-worker.js'; |
| 6 | +import { REQUEST_EXPORT_PDF, RECEIVE_EXPORT_PDF, ERROR_EXPORT_PDF } from '../constants/actions'; |
| 7 | +import { saveAs } from 'file-saver'; |
| 8 | +import { READER_CONTENT_TYPES } from '../constants/reader'; |
| 9 | + |
| 10 | +const tryGetFirstLink = itemKey => { |
| 11 | + return async (dispatch, getState) => { |
| 12 | + const state = getState(); |
| 13 | + let itemsByParent = get(state, ['libraries', state.current.libraryKey, 'itemsByParent', itemKey], null); |
| 14 | + if(!itemsByParent) { |
| 15 | + await dispatch(fetchChildItems(itemKey, { start: 0, limit: 100 })); |
| 16 | + itemsByParent = get(getState(), ['libraries', state.current.libraryKey, 'itemsByParent', itemKey], null); |
| 17 | + } |
| 18 | + if(itemsByParent && itemsByParent.keys.length > 0) { |
| 19 | + const firstAttachmentKey = itemsByParent.keys[0]; |
| 20 | + const item = get(getState(), ['libraries', state.current.libraryKey, 'items', firstAttachmentKey], null); |
| 21 | + if(item && item.url) { |
| 22 | + return item.url; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return false; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// On-prem overlay: always prefer PDF proxy when configured, even if Zotero Storage links exist. |
| 31 | +const tryGetAttachmentURL = attachmentItemKey => { |
| 32 | + return async (dispatch, getState) => { |
| 33 | + const state = getState(); |
| 34 | + const item = get(state, ['libraries', state.current.libraryKey, 'items', attachmentItemKey], null); |
| 35 | + |
| 36 | + // If a proxy base URL is configured, bypass Storage/enclosure links and use the proxy. |
| 37 | + if (state.config?.pdfProxyBaseUrl) { |
| 38 | + return dispatch(getAttachmentUrl(attachmentItemKey)); |
| 39 | + } |
| 40 | + |
| 41 | + const isFile = item && item.linkMode && item.linkMode.startsWith('imported') && item[Symbol.for('links')].enclosure; |
| 42 | + const isLink = item && item.linkMode && item.linkMode === 'linked_url'; |
| 43 | + const hasLink = isFile || isLink; |
| 44 | + |
| 45 | + if(hasLink) { |
| 46 | + return dispatch(getAttachmentUrl(attachmentItemKey)); |
| 47 | + } |
| 48 | + |
| 49 | + return false; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const tryGetBestAttachmentURLFallback = itemKey => { |
| 54 | + return async (dispatch, getState) => { |
| 55 | + const state = getState(); |
| 56 | + const item = get(state, ['libraries', state.current.libraryKey, 'items', itemKey], null); |
| 57 | + |
| 58 | + if(item.url) { |
| 59 | + const url = cleanURL(item.url, true); |
| 60 | + if(url) { |
| 61 | + return url; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if(item.DOI) { |
| 66 | + const doi = getZotero().Utilities.cleanDOI(item.DOI); |
| 67 | + if(doi) { |
| 68 | + return getDOIURL(doi); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return dispatch(tryGetFirstLink(itemKey)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +const pickBestItemAction = itemKey => { |
| 77 | + return async (dispatch, getState) => { |
| 78 | + const state = getState(); |
| 79 | + const item = get(state, ['libraries', state.current.libraryKey, 'items', itemKey], null); |
| 80 | + const current = state.current; |
| 81 | + const attachment = get(item, [Symbol.for('links'), 'attachment'], null); |
| 82 | + if(attachment) { |
| 83 | + const attachmentItemKey = getItemFromApiUrl(attachment.href); |
| 84 | + if (Object.keys(READER_CONTENT_TYPES).includes(attachment.attachmentType)) { |
| 85 | + // "best" attachment is PDF, open in reader |
| 86 | + const readerPath = makePath(state.config, { |
| 87 | + attachmentKey: attachmentItemKey, |
| 88 | + collection: current.collectionKey, |
| 89 | + items: [itemKey], |
| 90 | + library: current.libraryKey, |
| 91 | + noteKey: null, |
| 92 | + publications: current.isMyPublications, |
| 93 | + qmode: current.qmode, |
| 94 | + search: current.search, |
| 95 | + tags: current.tags, |
| 96 | + trash: current.isTrash, |
| 97 | + view: 'reader', |
| 98 | + }); |
| 99 | + return window.open(readerPath); |
| 100 | + } else if(attachment) { |
| 101 | + // "best" attachment exists, but is not PDF, open file |
| 102 | + return openDelayedURL(dispatch(getAttachmentUrl(attachmentItemKey))); |
| 103 | + } |
| 104 | + } else { |
| 105 | + // no "best" attachment, pick most appropriate fallback |
| 106 | + return openDelayedURL(dispatch(tryGetBestAttachmentURLFallback(itemKey))); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const pickBestAttachmentItemAction = attachmentItemKey => { |
| 112 | + return async (dispatch, getState) => { |
| 113 | + const state = getState(); |
| 114 | + const current = state.current; |
| 115 | + const item = get(state, ['libraries', state.current.libraryKey, 'items', attachmentItemKey], null); |
| 116 | + |
| 117 | + const isFile = item && item.linkMode && item.linkMode.startsWith('imported') && item[Symbol.for('links')].enclosure; |
| 118 | + const isLink = item && item.linkMode && item.linkMode === 'linked_url'; |
| 119 | + |
| 120 | + if (isFile) { |
| 121 | + if (Object.keys(READER_CONTENT_TYPES).includes(item.contentType)) { |
| 122 | + const readerPath = makePath(state.config, { |
| 123 | + attachmentKey: null, |
| 124 | + collection: current.collectionKey, |
| 125 | + items: [attachmentItemKey], |
| 126 | + library: current.libraryKey, |
| 127 | + noteKey: null, |
| 128 | + publications: current.isMyPublications, |
| 129 | + qmode: current.qmode, |
| 130 | + search: current.search, |
| 131 | + tags: current.tags, |
| 132 | + trash: current.isTrash, |
| 133 | + view: 'reader', |
| 134 | + }); |
| 135 | + return window.open(readerPath); |
| 136 | + } else { |
| 137 | + return openDelayedURL(dispatch(getAttachmentUrl(attachmentItemKey))); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if(isLink) { |
| 142 | + const url = cleanURL(item.url, true); |
| 143 | + if (url) { |
| 144 | + window.open(url); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return false; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +const exportAttachmentWithAnnotations = itemKey => { |
| 153 | + return async (dispatch, getState) => { |
| 154 | + const libraryKey = getState().current.libraryKey; |
| 155 | + dispatch({ |
| 156 | + type: REQUEST_EXPORT_PDF, |
| 157 | + itemKey, |
| 158 | + libraryKey |
| 159 | + }); |
| 160 | + |
| 161 | + try { |
| 162 | + const attachmentURL = await dispatch(getAttachmentUrl(itemKey)); |
| 163 | + await dispatch(fetchAllChildItems(itemKey)); |
| 164 | + const state = getState(); |
| 165 | + const { pdfWorkerURL, pdfReaderCMapsURL, pdfReaderStandardFontsURL } = state.config; |
| 166 | + const childItems = state.libraries[state.current.libraryKey]?.itemsByParent[itemKey]?.keys ?? []; |
| 167 | + const allItems = state.libraries[state.current.libraryKey]?.items; |
| 168 | + const attachmentItem = allItems[itemKey]; |
| 169 | + if (attachmentItem.contentType !== 'application/pdf') { |
| 170 | + throw new Error("Attachment is not a PDF"); |
| 171 | + } |
| 172 | + const annotations = childItems |
| 173 | + .map(childItemKey => allItems[childItemKey]) |
| 174 | + .filter(item => !item.deleted && item.itemType === 'annotation'); |
| 175 | + |
| 176 | + const pdfWorker = new PDFWorker({ pdfWorkerURL, pdfReaderCMapsURL, pdfReaderStandardFontsURL }); |
| 177 | + const data = await (await fetch(attachmentURL)).arrayBuffer(); |
| 178 | + const buf = await pdfWorker.export(data, annotations); |
| 179 | + const blob = new Blob([buf], { type: 'application/pdf' }); |
| 180 | + const fileName = attachmentItem?.filename || 'file.pdf'; |
| 181 | + saveAs(blob, fileName); |
| 182 | + |
| 183 | + dispatch({ |
| 184 | + type: RECEIVE_EXPORT_PDF, |
| 185 | + libraryKey, |
| 186 | + itemKey, |
| 187 | + fileName, |
| 188 | + blob, |
| 189 | + annotations |
| 190 | + }); |
| 191 | + } catch(error) { |
| 192 | + dispatch({ |
| 193 | + type: ERROR_EXPORT_PDF, |
| 194 | + libraryKey, |
| 195 | + itemKey, |
| 196 | + error |
| 197 | + }); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +export { pickBestAttachmentItemAction, pickBestItemAction, exportAttachmentWithAnnotations, tryGetAttachmentURL } |
0 commit comments