Add hierarchical file picker
This commit is contained in:
@@ -1,27 +1,16 @@
|
||||
export function addFileWithDedup(state, file, cfg) {
|
||||
const originalName = file.name;
|
||||
const existing = state.fileMeta.get(originalName);
|
||||
let name = originalName;
|
||||
|
||||
if (existing) {
|
||||
if (existing.size === file.size) return false;
|
||||
const dot = originalName.lastIndexOf('.');
|
||||
const base = dot > 0 ? originalName.slice(0, dot) : originalName;
|
||||
const extension = dot > 0 ? originalName.slice(dot) : '';
|
||||
let suffix = 2;
|
||||
while (state.fileMeta.has(`${base}_${suffix}${extension}`)) suffix += 1;
|
||||
name = `${base}_${suffix}${extension}`;
|
||||
}
|
||||
|
||||
const storedFile = new File([file], name, { lastModified: file.lastModified });
|
||||
state.fileMeta.set(name, { size: storedFile.size });
|
||||
const includedBytes = state.files.reduce(
|
||||
(total, item) => total + (state.overNames.has(item.name) ? 0 : item.size), 0,
|
||||
);
|
||||
if (storedFile.size > cfg.maxFileBytes
|
||||
|| includedBytes + storedFile.size > cfg.maxSessionBytes) {
|
||||
state.overNames.add(name);
|
||||
}
|
||||
state.files.push(storedFile);
|
||||
export function addFileWithDedup(state, fileNode) {
|
||||
const accept = (node) => {
|
||||
if (node.kind === 'file') {
|
||||
const key = `${node.path}\u0000${node.file.size}`;
|
||||
if (state.fileMeta.has(key)) return null;
|
||||
state.fileMeta.set(key, node.id);
|
||||
return node;
|
||||
}
|
||||
node.children = node.children.map(accept).filter(Boolean);
|
||||
return node.children.length ? node : null;
|
||||
};
|
||||
const accepted = accept(fileNode);
|
||||
if (!accepted || (accepted.kind !== 'file' && !accepted.children.length)) return false;
|
||||
state.nodes.push(accepted);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user