refac
This commit is contained in:
1 parent
3841e85abb
commit
c479c22438
2 files changed
+8
-28
No files matched your search
@@ -120,31 +120,12 @@ export const downloadFileBlob = async (
|
|||||||
return { blob, filename };
|
return { blob, filename };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const downloadDirectoryArchive = async (
|
export const archiveFromTerminal = 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 (
|
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
apiKey: string,
|
apiKey: string,
|
||||||
paths: string[]
|
paths: string[]
|
||||||
): Promise<{ blob: Blob; filename: string } | null> => {
|
): Promise<{ blob: Blob; filename: string } | null> => {
|
||||||
const url = `${baseUrl.replace(/\/$/, '')}/files/download`;
|
const url = `${baseUrl.replace(/\/$/, '')}/files/archive`;
|
||||||
const res = await fetch(url, {
|
const res = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -19,8 +19,7 @@
|
|||||||
listFiles,
|
listFiles,
|
||||||
readFile,
|
readFile,
|
||||||
downloadFileBlob,
|
downloadFileBlob,
|
||||||
downloadDirectoryArchive,
|
archiveFromTerminal,
|
||||||
downloadMultipleAsZip,
|
|
||||||
uploadToTerminal,
|
uploadToTerminal,
|
||||||
createDirectory,
|
createDirectory,
|
||||||
deleteEntry,
|
deleteEntry,
|
||||||
@@ -408,7 +407,7 @@
|
|||||||
// Directories end with '/' — download as ZIP archive
|
// Directories end with '/' — download as ZIP archive
|
||||||
const isDir = path.endsWith('/');
|
const isDir = path.endsWith('/');
|
||||||
const result = isDir
|
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);
|
: await downloadFileBlob(terminal.url, terminal.key, path);
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
const url = URL.createObjectURL(result.blob);
|
const url = URL.createObjectURL(result.blob);
|
||||||
@@ -653,14 +652,14 @@
|
|||||||
const paths = [...selectedEntries].map((p) => p.replace(/\/$/, ''));
|
const paths = [...selectedEntries].map((p) => p.replace(/\/$/, ''));
|
||||||
if (paths.length === 0) return;
|
if (paths.length === 0) return;
|
||||||
|
|
||||||
// Single item — use the regular downloadFile path
|
// Single file (not dir) — use the regular downloadFile path
|
||||||
if (paths.length === 1) {
|
if (paths.length === 1 && !([...selectedEntries][0]).endsWith('/')) {
|
||||||
await downloadFile([...selectedEntries][0]);
|
await downloadFile([...selectedEntries][0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiple items — bundle into a single ZIP
|
// Archive everything into a single ZIP
|
||||||
const result = await downloadMultipleAsZip(terminal.url, terminal.key, paths);
|
const result = await archiveFromTerminal(terminal.url, terminal.key, paths);
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
const url = URL.createObjectURL(result.blob);
|
const url = URL.createObjectURL(result.blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
|
|||||||
Reference in new issue
Block a user