feat: add download file method for files generated by plugin
This commit is contained in:
committed by
Linus Rath
parent
a08a9e9ed3
commit
a679d82cc3
@@ -60,6 +60,7 @@ const PERM_PER_METHOD: Record<string, Permission | null> = {
|
||||
'ui.prompt': null,
|
||||
'ui.rerenderEmail': null,
|
||||
'ui.openExternalUrl': null,
|
||||
'ui.downloadFile': 'ui:download-file'
|
||||
};
|
||||
|
||||
function hasPermission(plugin: InstalledPlugin, perm: Permission): boolean {
|
||||
@@ -400,6 +401,24 @@ async function saveFile(formerFileID:string, file: File): Promise<string> {
|
||||
return fileId;
|
||||
}
|
||||
|
||||
// ─── Download files generated by the plugin. This is not user's files or attachments ──────────────────────────
|
||||
async function downloadFile(args: { content: string; filename: string; contentType?: string }): Promise<void> {
|
||||
const { content, filename, contentType = 'application/json' } = args;
|
||||
|
||||
try {
|
||||
const url = URL.createObjectURL(new Blob([content], { type: contentType }));
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to download file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── admin config (same as before) ────────────────────────────
|
||||
|
||||
async function adminGetAll(pluginId: string): Promise<Record<string, unknown>> {
|
||||
@@ -547,6 +566,10 @@ export async function dispatchApiCall(
|
||||
window.open(parsed.toString(), '_blank', 'noopener,noreferrer');
|
||||
return undefined;
|
||||
}
|
||||
case 'ui.downloadFile': {
|
||||
const opts = args[0] as { content: string; filename: string; contentType?: string };
|
||||
return downloadFile(opts);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`Unhandled method "${method}"`);
|
||||
|
||||
Reference in New Issue
Block a user