2021-07-29 21:36:50 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-07-29 22:01:26 +00:00
|
|
|
export type getIframeFn = () => HTMLIFrameElement; // eslint-disable-line @typescript-eslint/naming-convention
|
2021-07-29 21:36:50 +00:00
|
|
|
|
2022-03-02 17:27:36 +00:00
|
|
|
export const DEFAULT_STYLES = {
|
|
|
|
imgSrc: "",
|
2023-02-13 11:39:16 +00:00
|
|
|
imgStyle: null as string | null, // css props
|
2022-03-02 17:27:36 +00:00
|
|
|
style: "",
|
|
|
|
textContent: "",
|
|
|
|
};
|
|
|
|
|
2021-07-29 21:36:50 +00:00
|
|
|
type DownloadOptions = {
|
2021-07-29 22:01:26 +00:00
|
|
|
blob: Blob;
|
|
|
|
name: string;
|
|
|
|
autoDownload?: boolean;
|
2022-03-02 17:27:36 +00:00
|
|
|
opts?: typeof DEFAULT_STYLES;
|
2021-07-29 22:01:26 +00:00
|
|
|
};
|
2021-07-29 21:36:50 +00:00
|
|
|
|
|
|
|
// set up the iframe as a singleton so we don't have to figure out destruction of it down the line.
|
|
|
|
let managedIframe: HTMLIFrameElement;
|
|
|
|
let onLoadPromise: Promise<void>;
|
|
|
|
function getManagedIframe(): { iframe: HTMLIFrameElement; onLoadPromise: Promise<void> } {
|
|
|
|
if (managedIframe) return { iframe: managedIframe, onLoadPromise };
|
|
|
|
|
|
|
|
managedIframe = document.createElement("iframe");
|
|
|
|
|
|
|
|
// Need to append the iframe in order for the browser to load it.
|
|
|
|
document.body.appendChild(managedIframe);
|
|
|
|
|
|
|
|
// Dev note: the reassignment warnings are entirely incorrect here.
|
|
|
|
|
2021-08-05 17:21:22 +00:00
|
|
|
managedIframe.style.display = "none";
|
|
|
|
|
2021-07-29 21:36:50 +00:00
|
|
|
// @ts-ignore
|
|
|
|
// noinspection JSConstantReassignment
|
|
|
|
managedIframe.sandbox = "allow-scripts allow-downloads allow-downloads-without-user-activation";
|
|
|
|
|
|
|
|
onLoadPromise = new Promise((resolve) => {
|
|
|
|
managedIframe.onload = () => {
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
managedIframe.src = "usercontent/"; // XXX: Should come from the skin
|
|
|
|
});
|
|
|
|
|
|
|
|
return { iframe: managedIframe, onLoadPromise };
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: If we decide to keep the download link behaviour, we should bring the style management into here.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to handle safe file downloads. This operates off an iframe for reasons described
|
|
|
|
* by the blob helpers. By default, this will use a hidden iframe to manage the download
|
|
|
|
* through a user content wrapper, but can be given an iframe reference if the caller needs
|
|
|
|
* additional control over the styling/position of the iframe itself.
|
|
|
|
*/
|
|
|
|
export class FileDownloader {
|
2023-02-13 17:01:43 +00:00
|
|
|
private onLoadPromise?: Promise<void>;
|
2021-07-29 21:36:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new file downloader
|
|
|
|
* @param iframeFn Function to get a pre-configured iframe. Set to null to have the downloader
|
|
|
|
* use a generic, hidden, iframe.
|
|
|
|
*/
|
2023-02-13 17:01:43 +00:00
|
|
|
public constructor(private iframeFn?: getIframeFn) {}
|
2021-07-29 21:36:50 +00:00
|
|
|
|
|
|
|
private get iframe(): HTMLIFrameElement {
|
|
|
|
const iframe = this.iframeFn?.();
|
|
|
|
if (!iframe) {
|
|
|
|
const managed = getManagedIframe();
|
|
|
|
this.onLoadPromise = managed.onLoadPromise;
|
|
|
|
return managed.iframe;
|
|
|
|
}
|
2023-02-13 17:01:43 +00:00
|
|
|
this.onLoadPromise = undefined;
|
2021-07-29 21:36:50 +00:00
|
|
|
return iframe;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public async download({ blob, name, autoDownload = true, opts = DEFAULT_STYLES }: DownloadOptions): Promise<void> {
|
2021-07-29 21:36:50 +00:00
|
|
|
const iframe = this.iframe; // get the iframe first just in case we need to await onload
|
|
|
|
if (this.onLoadPromise) await this.onLoadPromise;
|
2023-02-13 17:01:43 +00:00
|
|
|
iframe.contentWindow?.postMessage(
|
2021-07-29 21:36:50 +00:00
|
|
|
{
|
2022-03-02 17:27:36 +00:00
|
|
|
...opts,
|
2021-07-29 21:36:50 +00:00
|
|
|
blob: blob,
|
|
|
|
download: name,
|
|
|
|
auto: autoDownload,
|
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
"*",
|
2021-07-29 21:36:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|