Implement reusable upload platform
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// Рекурсивно получить из ZIP только файлы с разрешёнными расширениями.
|
||||
|
||||
function extensionAllowed(name, allowedExt) {
|
||||
const lowerName = name.toLowerCase();
|
||||
return allowedExt.some((extension) => lowerName.endsWith(extension.toLowerCase()));
|
||||
}
|
||||
|
||||
function makeFile(data, name) {
|
||||
return new File([data], name);
|
||||
}
|
||||
|
||||
async function listEntries(data, prefix, allowedExt, depth) {
|
||||
if (depth > 20) throw new Error('Слишком глубокая вложенность ZIP');
|
||||
const entries = fflate.unzipSync(data);
|
||||
const files = [];
|
||||
|
||||
for (const [entryName, entryData] of Object.entries(entries)) {
|
||||
if (entryName.endsWith('/')) continue;
|
||||
const path = prefix ? `${prefix}/${entryName}` : entryName;
|
||||
if (entryName.toLowerCase().endsWith('.zip')) {
|
||||
files.push(...await listEntries(entryData, path, allowedExt, depth + 1));
|
||||
} else if (extensionAllowed(entryName, allowedExt)) {
|
||||
files.push(makeFile(entryData, path));
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function listZipFiles(file, allowedExt) {
|
||||
const data = new Uint8Array(await file.arrayBuffer());
|
||||
return listEntries(data, '', allowedExt, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user