Skip to content

Commit cc5b041

Browse files
authored
feat: download pdfs during bulk import
1 parent 2759b7e commit cc5b041

File tree

4 files changed

+79
-58
lines changed

4 files changed

+79
-58
lines changed

js/import/import.ui.js

+69-50
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
/* global CodeMirror, showdown, html_beautify, ExcelJS */
12+
/* global CodeMirror, showdown, html_beautify, ExcelJS, WebImporter */
1313
import { initOptionFields, attachOptionFieldsListeners } from '../shared/fields.js';
1414
import { getDirectoryHandle, saveFile } from '../shared/filesystem.js';
1515
import { asyncForEach } from '../shared/utils.js';
@@ -41,6 +41,8 @@ const BULK_URLS_LIST = document.querySelector('#import-result ul');
4141

4242
const IMPORT_FILE_PICKER_CONTAINER = document.getElementById('import-file-picker-container');
4343

44+
const DOWNLOAD_BINARY_TYPES = ['pdf'];
45+
4446
const ui = {};
4547
const config = {};
4648
const importStatus = {
@@ -276,57 +278,74 @@ const attachListeners = () => {
276278
});
277279
processNext();
278280
} else {
279-
const frame = document.createElement('iframe');
280-
frame.id = 'import-content-frame';
281-
282-
if (config.fields['import-enable-js']) {
283-
frame.removeAttribute('sandbox');
284-
} else {
285-
frame.setAttribute('sandbox', 'allow-same-origin');
286-
}
287-
288-
const onLoad = async () => {
289-
const includeDocx = !!dirHandle;
290-
291-
window.setTimeout(async () => {
292-
const { originalURL, replacedURL } = frame.dataset;
293-
if (frame.contentDocument) {
294-
try {
295-
config.importer.setTransformationInput({
296-
url: replacedURL,
297-
document: frame.contentDocument,
298-
includeDocx,
299-
params: { originalURL },
300-
});
301-
await config.importer.transform();
302-
} catch (e) {
303-
// eslint-disable-next-line no-console
304-
console.error(`Cannot transform ${originalURL} - transformation error ?`, e);
305-
// fallback, probably transformation error
306-
importStatus.rows.push({
307-
url: originalURL,
308-
status: `Error: ${e.message}`,
309-
});
281+
const contentType = res.headers.get('content-type');
282+
if (contentType.includes('html')) {
283+
const frame = document.createElement('iframe');
284+
frame.id = 'import-content-frame';
285+
286+
if (config.fields['import-enable-js']) {
287+
frame.removeAttribute('sandbox');
288+
} else {
289+
frame.setAttribute('sandbox', 'allow-same-origin');
290+
}
291+
292+
const onLoad = async () => {
293+
const includeDocx = !!dirHandle;
294+
295+
window.setTimeout(async () => {
296+
const { originalURL, replacedURL } = frame.dataset;
297+
if (frame.contentDocument) {
298+
try {
299+
config.importer.setTransformationInput({
300+
url: replacedURL,
301+
document: frame.contentDocument,
302+
includeDocx,
303+
params: { originalURL },
304+
});
305+
await config.importer.transform();
306+
} catch (e) {
307+
// eslint-disable-next-line no-console
308+
console.error(`Cannot transform ${originalURL} - transformation error ?`, e);
309+
// fallback, probably transformation error
310+
importStatus.rows.push({
311+
url: originalURL,
312+
status: `Error: ${e.message}`,
313+
});
314+
}
310315
}
311-
}
312-
313-
const event = new Event('transformation-complete');
314-
frame.dispatchEvent(event);
315-
}, config.fields['import-pageload-timeout'] || 100);
316-
};
317316

318-
frame.addEventListener('load', onLoad);
319-
frame.addEventListener('transformation-complete', processNext);
320-
321-
frame.dataset.originalURL = url;
322-
frame.dataset.replacedURL = src;
323-
frame.src = src;
324-
325-
const current = getContentFrame();
326-
current.removeEventListener('load', onLoad);
327-
current.removeEventListener('transformation-complete', processNext);
328-
329-
current.replaceWith(frame);
317+
const event = new Event('transformation-complete');
318+
frame.dispatchEvent(event);
319+
}, config.fields['import-pageload-timeout'] || 100);
320+
};
321+
322+
frame.addEventListener('load', onLoad);
323+
frame.addEventListener('transformation-complete', processNext);
324+
325+
frame.dataset.originalURL = url;
326+
frame.dataset.replacedURL = src;
327+
frame.src = src;
328+
329+
const current = getContentFrame();
330+
current.removeEventListener('load', onLoad);
331+
current.removeEventListener('transformation-complete', processNext);
332+
333+
current.replaceWith(frame);
334+
} else if (IS_BULK
335+
&& DOWNLOAD_BINARY_TYPES.filter((t) => contentType.includes(t)).length > 0) {
336+
const blob = await res.blob();
337+
const u = new URL(src);
338+
const path = WebImporter.FileUtils.sanitizePath(u.pathname);
339+
340+
await saveFile(dirHandle, path, blob);
341+
importStatus.rows.push({
342+
url,
343+
status: 'Success',
344+
path,
345+
});
346+
updateImporterUI(null, url);
347+
processNext();
348+
}
330349
}
331350
} else {
332351
// eslint-disable-next-line no-console

modules/importer.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import {
1515
DOMUtils,
16+
FileUtils,
1617
Blocks,
1718
html2docx,
1819
html2md,
@@ -74,6 +75,7 @@ async function html2docxWrapper(url, document, transformCfg, params) {
7475
export {
7576
Blocks,
7677
DOMUtils,
78+
FileUtils,
7779
html2mdWrapper as html2md,
7880
html2docxWrapper as html2docx,
7981
};

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"semantic-release": "semantic-release"
1414
},
1515
"dependencies": {
16-
"@adobe/helix-importer": "1.14.0",
16+
"@adobe/helix-importer": "1.15.0",
1717
"@spectrum-web-components/bundle": "0.26.0",
1818
"@spectrum-web-components/icons-workflow": "0.8.10",
1919
"parcel": "2.5.0",

0 commit comments

Comments
 (0)