Skip to content

Commit eddaf65

Browse files
committed
Directly download individual files without buffering in memory
1 parent 56917f6 commit eddaf65

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

web/src/lib/utils/asset-utils.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ export const downloadBlob = (data: Blob, filename: string) => {
162162
URL.revokeObjectURL(url);
163163
};
164164

165+
export const downloadUrl = (url: string, filename: string) => {
166+
const anchor = document.createElement('a');
167+
anchor.href = url;
168+
anchor.download = filename;
169+
170+
document.body.append(anchor);
171+
anchor.click();
172+
anchor.remove();
173+
174+
URL.revokeObjectURL(url);
175+
};
176+
165177
export const downloadArchive = async (fileName: string, options: Omit<DownloadInfoDto, 'archiveSize'>) => {
166178
const $preferences = get<UserPreferencesResponseDto | undefined>(preferences);
167179
const dto = { ...options, archiveSize: $preferences?.download.archiveSize };
@@ -238,33 +250,18 @@ export const downloadFile = async (asset: AssetResponseDto) => {
238250
}
239251
}
240252

241-
for (const { filename, id, size } of assets) {
242-
const downloadKey = filename;
243-
253+
for (const { filename, id } of assets) {
244254
try {
245-
const abort = new AbortController();
246-
downloadManager.add(downloadKey, size, abort);
247255
const key = getKey();
248256

249257
notificationController.show({
250258
type: NotificationType.Info,
251259
message: $t('downloading_asset_filename', { values: { filename: asset.originalFileName } }),
252260
});
253261

254-
// TODO use sdk once it supports progress events
255-
const { data } = await downloadRequest({
256-
method: 'GET',
257-
url: getBaseUrl() + `/assets/${id}/original` + (key ? `?key=${key}` : ''),
258-
signal: abort.signal,
259-
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded, event.total),
260-
});
261-
262-
downloadBlob(data, filename);
262+
downloadUrl(getBaseUrl() + `/assets/${id}/original` + (key ? `?key=${key}` : ''), filename);
263263
} catch (error) {
264264
handleError(error, $t('errors.error_downloading', { values: { filename } }));
265-
downloadManager.clear(downloadKey);
266-
} finally {
267-
setTimeout(() => downloadManager.clear(downloadKey), 5000);
268265
}
269266
}
270267
};

0 commit comments

Comments
 (0)