Up And Down : première version
This commit is contained in:
commit
89c5430308
76 files changed
+20730
No files matched your search
@@ -0,0 +1,91 @@
|
||||
// Lecture de fichiers et d'arborescences à envoyer (glisser-déposer ou sélecteur de dossier).
|
||||
|
||||
export interface PickedFile {
|
||||
/** Chemin relatif au point d'envoi, dossiers compris (« Photos/2024/img.jpg »). */
|
||||
rel: string;
|
||||
file: File;
|
||||
}
|
||||
|
||||
export interface Collected {
|
||||
files: PickedFile[];
|
||||
/** Dossiers à recréer (y compris vides), parents avant enfants. */
|
||||
dirs: string[];
|
||||
/** Éléments ignorés : fichiers et dossiers cachés (.DS_Store, .git...). */
|
||||
ignored: string[];
|
||||
}
|
||||
|
||||
const hidden = (name: string) => name.startsWith(".");
|
||||
|
||||
/** Le glisser contient-il des fichiers venant du système ? */
|
||||
export const hasFiles = (dt: DataTransfer) => dt.types.includes("Files");
|
||||
|
||||
const readAll = (reader: FileSystemDirectoryReader) =>
|
||||
new Promise<FileSystemEntry[]>((resolve, reject) => {
|
||||
const out: FileSystemEntry[] = [];
|
||||
// readEntries rend les entrées par lots (100 sous Chrome) : boucler jusqu'au lot vide.
|
||||
const next = () =>
|
||||
reader.readEntries((batch) => {
|
||||
if (batch.length === 0) return resolve(out);
|
||||
out.push(...batch);
|
||||
next();
|
||||
}, reject);
|
||||
next();
|
||||
});
|
||||
|
||||
const fileOf = (entry: FileSystemFileEntry) => new Promise<File>((resolve, reject) => entry.file(resolve, reject));
|
||||
|
||||
async function walk(entry: FileSystemEntry, prefix: string, acc: Collected) {
|
||||
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
||||
if (hidden(entry.name)) {
|
||||
acc.ignored.push(rel);
|
||||
return;
|
||||
}
|
||||
if (entry.isDirectory) {
|
||||
acc.dirs.push(rel);
|
||||
for (const child of await readAll((entry as FileSystemDirectoryEntry).createReader())) {
|
||||
await walk(child, rel, acc);
|
||||
}
|
||||
} else {
|
||||
acc.files.push({ rel, file: await fileOf(entry as FileSystemFileEntry) });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit un dépôt (fichiers et dossiers). Les entrées doivent être capturées pendant
|
||||
* l'événement drop : le DataTransfer est vidé dès que le gestionnaire rend la main.
|
||||
*/
|
||||
export function collectDrop(dt: DataTransfer): Promise<Collected> {
|
||||
const entries = Array.from(dt.items)
|
||||
.filter((i) => i.kind === "file")
|
||||
.map((i) => i.webkitGetAsEntry?.())
|
||||
.filter((e): e is FileSystemEntry => !!e);
|
||||
const plain = entries.length ? [] : Array.from(dt.files); // navigateur sans webkitGetAsEntry
|
||||
return (async () => {
|
||||
const acc: Collected = { files: [], dirs: [], ignored: [] };
|
||||
for (const e of entries) await walk(e, "", acc);
|
||||
collectInto(plain, acc);
|
||||
return acc;
|
||||
})();
|
||||
}
|
||||
|
||||
/** Fichiers d'un <input type="file"> (chemins dans webkitRelativePath pour un dossier). */
|
||||
export function collectFiles(files: FileList | File[]): Collected {
|
||||
const acc: Collected = { files: [], dirs: [], ignored: [] };
|
||||
collectInto(Array.from(files), acc);
|
||||
return acc;
|
||||
}
|
||||
|
||||
function collectInto(files: File[], acc: Collected) {
|
||||
const dirs = new Set(acc.dirs);
|
||||
for (const file of files) {
|
||||
const rel = file.webkitRelativePath || file.name;
|
||||
const parts = rel.split("/");
|
||||
if (parts.some(hidden)) {
|
||||
acc.ignored.push(rel);
|
||||
continue;
|
||||
}
|
||||
for (let i = 1; i < parts.length; i++) dirs.add(parts.slice(0, i).join("/"));
|
||||
acc.files.push({ rel, file });
|
||||
}
|
||||
acc.dirs = [...dirs].sort((a, b) => a.split("/").length - b.split("/").length);
|
||||
}
|
||||
Reference in new issue
Block a user