37 lines
1.6 KiB
JavaScript
37 lines
1.6 KiB
JavaScript
import { listZipFiles } from '../zip/list_zip_files.js';
|
|
import { addFileWithDedup } from './add_file_with_dedup.js';
|
|
import { render } from './render.js';
|
|
|
|
export function onFolderChange(state, cfg, elements) {
|
|
return async () => {
|
|
if (state.busy) return;
|
|
for (const file of Array.from(elements.folderInputEl.files)) {
|
|
const parts = (file.webkitRelativePath || file.name).split('/');
|
|
const relativePath = parts.slice(1).join('/') || file.name;
|
|
const lowerPath = relativePath.toLowerCase();
|
|
const directory = relativePath.includes('/')
|
|
? relativePath.slice(0, relativePath.lastIndexOf('/')) : '';
|
|
if (lowerPath.endsWith('.zip')) {
|
|
try {
|
|
const extracted = await listZipFiles(file, cfg.allowedExt);
|
|
if (extracted.length) {
|
|
extracted.forEach((item) => addFileWithDedup(state, new File([item],
|
|
directory ? `${directory}/${item.name}` : item.name,
|
|
{ lastModified: item.lastModified }), cfg));
|
|
} else {
|
|
addFileWithDedup(state, new File([file], relativePath,
|
|
{ lastModified: file.lastModified }), cfg);
|
|
}
|
|
} catch (error) {
|
|
addFileWithDedup(state, new File([file], relativePath,
|
|
{ lastModified: file.lastModified }), cfg);
|
|
}
|
|
} else if (cfg.allowedExt.some((extension) => lowerPath.endsWith(extension))) {
|
|
addFileWithDedup(state, new File([file], relativePath,
|
|
{ lastModified: file.lastModified }), cfg);
|
|
}
|
|
}
|
|
elements.folderInputEl.value = '';
|
|
render(state, elements);
|
|
};
|
|
} |