Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(file-sharing) Keep original filename on file download (#16183)

Horatiu Muresan0b095f36eb00
diff --git a/react/features/file-sharing/middleware.web.ts b/react/features/file-sharing/middleware.web.ts
index 617ac0a8c1..9732479bdb 100644
--- a/react/features/file-sharing/middleware.web.ts
+++ b/react/features/file-sharing/middleware.web.ts
@@ -14,6 +14,7 @@ import { addFile, removeFile, updateFileProgress } from './actions';
import { getFileExtension } from './functions.any';
import logger from './logger';
import { IFileMetadata } from './types';
+import { downloadFile } from './utils';
/**
@@ -122,14 +123,14 @@ MiddlewareRegistry.register(store => next => action => {
}
}))
.then((response: any) => response.json())
- .then((data: { presignedUrl: any; }) => {
- const url = data.presignedUrl;
+ .then((data: { fileName: string; presignedUrl: string; }) => {
+ const { presignedUrl, fileName } = data;
- if (!url) {
+ if (!presignedUrl) {
throw new Error('No presigned URL found in the response.');
}
- window.open(url, '_blank', 'noreferrer,noopener');
+ return downloadFile(presignedUrl, fileName);
})
.catch((error: any) => {
logger.warn('Could not download file:', error);
diff --git a/react/features/file-sharing/utils.ts b/react/features/file-sharing/utils.ts
new file mode 100644
index 0000000000..adb0c63580
--- /dev/null
+++ b/react/features/file-sharing/utils.ts
@@ -0,0 +1,27 @@
+const generateDownloadUrl = async (url: string) => {
+ const resp = await fetch(url);
+ const respBlob = await resp.blob();
+
+ const blob = new Blob([ respBlob ]);
+
+ return URL.createObjectURL(blob);
+};
+
+export const downloadFile = async (url: string, fileName: string) => {
+ const dowloadUrl = await generateDownloadUrl(url);
+ const link = document.createElement('a');
+
+ if (fileName) {
+ link.download = fileName;
+ }
+ link.href = dowloadUrl;
+
+ document.body.appendChild(link);
+ link.click();
+ link.remove();
+
+ // fix for certain browsers
+ setTimeout(() => {
+ URL.revokeObjectURL(dowloadUrl);
+ }, 0);
+};