refac
This commit is contained in:
@@ -120,31 +120,12 @@ export const downloadFileBlob = async (
|
||||
return { blob, filename };
|
||||
};
|
||||
|
||||
export const downloadDirectoryArchive = async (
|
||||
baseUrl: string,
|
||||
apiKey: string,
|
||||
path: string
|
||||
): Promise<{ blob: Blob; filename: string } | null> => {
|
||||
const url = `${baseUrl.replace(/\/$/, '')}/files/download?path=${encodeURIComponent(path)}`;
|
||||
const res = await fetch(url, {
|
||||
headers: { Authorization: `Bearer ${apiKey}` }
|
||||
}).catch(() => null);
|
||||
|
||||
if (!res || !res.ok) return null;
|
||||
|
||||
const disposition = res.headers.get('content-disposition') ?? '';
|
||||
const match = disposition.match(/filename="?([^"]+)"?/);
|
||||
const filename = match?.[1] ?? `${path.split('/').filter(Boolean).pop() ?? 'download'}.zip`;
|
||||
const blob = await res.blob();
|
||||
return { blob, filename };
|
||||
};
|
||||
|
||||
export const downloadMultipleAsZip = async (
|
||||
export const archiveFromTerminal = async (
|
||||
baseUrl: string,
|
||||
apiKey: string,
|
||||
paths: string[]
|
||||
): Promise<{ blob: Blob; filename: string } | null> => {
|
||||
const url = `${baseUrl.replace(/\/$/, '')}/files/download`;
|
||||
const url = `${baseUrl.replace(/\/$/, '')}/files/archive`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
listFiles,
|
||||
readFile,
|
||||
downloadFileBlob,
|
||||
downloadDirectoryArchive,
|
||||
downloadMultipleAsZip,
|
||||
archiveFromTerminal,
|
||||
uploadToTerminal,
|
||||
createDirectory,
|
||||
deleteEntry,
|
||||
@@ -408,7 +407,7 @@
|
||||
// Directories end with '/' — download as ZIP archive
|
||||
const isDir = path.endsWith('/');
|
||||
const result = isDir
|
||||
? await downloadDirectoryArchive(terminal.url, terminal.key, path.replace(/\/$/, ''))
|
||||
? await archiveFromTerminal(terminal.url, terminal.key, [path.replace(/\/$/, '')])
|
||||
: await downloadFileBlob(terminal.url, terminal.key, path);
|
||||
if (!result) return;
|
||||
const url = URL.createObjectURL(result.blob);
|
||||
@@ -653,14 +652,14 @@
|
||||
const paths = [...selectedEntries].map((p) => p.replace(/\/$/, ''));
|
||||
if (paths.length === 0) return;
|
||||
|
||||
// Single item — use the regular downloadFile path
|
||||
if (paths.length === 1) {
|
||||
// Single file (not dir) — use the regular downloadFile path
|
||||
if (paths.length === 1 && !([...selectedEntries][0]).endsWith('/')) {
|
||||
await downloadFile([...selectedEntries][0]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Multiple items — bundle into a single ZIP
|
||||
const result = await downloadMultipleAsZip(terminal.url, terminal.key, paths);
|
||||
// Archive everything into a single ZIP
|
||||
const result = await archiveFromTerminal(terminal.url, terminal.key, paths);
|
||||
if (!result) return;
|
||||
const url = URL.createObjectURL(result.blob);
|
||||
const a = document.createElement('a');
|
||||
|
||||
Reference in New Issue
Block a user