Implement reusable upload platform

This commit is contained in:
“Naeel”
2026-09-05 08:51:32 +03:00
parent 82d9ca9136
commit c0b87880ae
35 changed files with 734 additions and 9 deletions
+32
View File
@@ -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);
}