mirror of
https://github.com/Qortal/qortal-ui.git
synced 2025-02-14 11:15:50 +00:00
added download action type
This commit is contained in:
parent
6333e4f3a0
commit
a1240569b8
@ -0,0 +1,56 @@
|
||||
export const mimeToExtensionMap = {
|
||||
// Documents
|
||||
"application/pdf": ".pdf",
|
||||
"application/msword": ".doc",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
||||
"application/vnd.ms-excel": ".xls",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
||||
"application/vnd.ms-powerpoint": ".ppt",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation": ".pptx",
|
||||
"application/vnd.oasis.opendocument.text": ".odt",
|
||||
"application/vnd.oasis.opendocument.spreadsheet": ".ods",
|
||||
"application/vnd.oasis.opendocument.presentation": ".odp",
|
||||
"text/plain": ".txt",
|
||||
"text/csv": ".csv",
|
||||
"text/html": ".html",
|
||||
"application/xhtml+xml": ".xhtml",
|
||||
"application/xml": ".xml",
|
||||
"application/json": ".json",
|
||||
|
||||
// Images
|
||||
"image/jpeg": ".jpg",
|
||||
"image/png": ".png",
|
||||
"image/gif": ".gif",
|
||||
"image/webp": ".webp",
|
||||
"image/svg+xml": ".svg",
|
||||
"image/tiff": ".tif",
|
||||
"image/bmp": ".bmp",
|
||||
|
||||
// Audio
|
||||
"audio/mpeg": ".mp3",
|
||||
"audio/ogg": ".ogg",
|
||||
"audio/wav": ".wav",
|
||||
"audio/webm": ".weba",
|
||||
"audio/aac": ".aac",
|
||||
|
||||
// Video
|
||||
"video/mp4": ".mp4",
|
||||
"video/webm": ".webm",
|
||||
"video/ogg": ".ogv",
|
||||
"video/x-msvideo": ".avi",
|
||||
"video/quicktime": ".mov",
|
||||
"video/x-ms-wmv": ".wmv",
|
||||
"video/mpeg": ".mpeg",
|
||||
"video/3gpp": ".3gp",
|
||||
"video/3gpp2": ".3g2",
|
||||
"video/x-matroska": ".mkv",
|
||||
"video/x-flv": ".flv",
|
||||
|
||||
// Archives
|
||||
"application/zip": ".zip",
|
||||
"application/x-rar-compressed": ".rar",
|
||||
"application/x-tar": ".tar",
|
||||
"application/x-7z-compressed": ".7z",
|
||||
"application/x-gzip": ".gz",
|
||||
"application/x-bzip2": ".bz2",
|
||||
};
|
@ -23,6 +23,7 @@ import { Loader } from '../../../utils/loader.js';
|
||||
import { QORT_DECIMALS } from 'qortal-ui-crypto/api/constants';
|
||||
import nacl from '../../../../../qortal-ui-crypto/api/deps/nacl-fast.js'
|
||||
import ed2curve from '../../../../../qortal-ui-crypto/api/deps/ed2curve.js'
|
||||
import { mimeToExtensionMap } from '../../components/qdn-action-constants';
|
||||
const parentEpml = new Epml({ type: 'WINDOW', source: window.parent });
|
||||
|
||||
class WebBrowser extends LitElement {
|
||||
@ -150,8 +151,7 @@ class WebBrowser extends LitElement {
|
||||
if (
|
||||
this.identifier && this.identifier != 'null' &&
|
||||
this.identifier != 'default'
|
||||
)
|
||||
{
|
||||
) {
|
||||
displayUrl = displayUrl.concat('/' + this.identifier);
|
||||
}
|
||||
if (this.path != null && this.path != '/')
|
||||
@ -1392,6 +1392,83 @@ class WebBrowser extends LitElement {
|
||||
// If they decline, send back JSON that includes an `error` key, such as `{"error": "User declined request"}`
|
||||
break;
|
||||
}
|
||||
case 'DOWNLOAD': {
|
||||
try {
|
||||
const requiredFields = ['filename', 'blob'];
|
||||
const missingFields = [];
|
||||
|
||||
requiredFields.forEach((field) => {
|
||||
if (!data[field]) {
|
||||
missingFields.push(field);
|
||||
}
|
||||
});
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
const missingFieldsString = missingFields.join(', ');
|
||||
const errorMsg = `Missing fields: ${missingFieldsString}`
|
||||
let data = {};
|
||||
data['error'] = errorMsg;
|
||||
response = JSON.stringify(data);
|
||||
break
|
||||
}
|
||||
|
||||
const filename = data.filename
|
||||
const blob = data.blob
|
||||
|
||||
const mimeType = blob.type || data.mimeType
|
||||
let backupExention = filename.split('.').pop()
|
||||
if (backupExention) {
|
||||
backupExention = '.' + backupExention
|
||||
}
|
||||
const fileExtension = mimeToExtensionMap[mimeType] || backupExention
|
||||
let fileHandleOptions = {}
|
||||
if (!mimeType) {
|
||||
const obj = {};
|
||||
const errorMsg = 'A mimeType could not be derived';
|
||||
obj['error'] = errorMsg;
|
||||
response = JSON.stringify(obj);
|
||||
break
|
||||
}
|
||||
if (!fileExtension) {
|
||||
const obj = {};
|
||||
const errorMsg = 'A file extension could not be derived';
|
||||
obj['error'] = errorMsg;
|
||||
response = JSON.stringify(obj);
|
||||
break
|
||||
}
|
||||
if (fileExtension && mimeType) {
|
||||
fileHandleOptions = {
|
||||
accept: {
|
||||
[mimeType]: [fileExtension]
|
||||
}
|
||||
}
|
||||
}
|
||||
const fileHandle = await self.showSaveFilePicker({
|
||||
suggestedName: filename,
|
||||
types: [
|
||||
{
|
||||
description: mimeType,
|
||||
...fileHandleOptions
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
})
|
||||
const writeFile = async (fileHandle, contents) => {
|
||||
const writable = await fileHandle.createWritable()
|
||||
await writable.write(contents)
|
||||
await writable.close()
|
||||
}
|
||||
writeFile(fileHandle, blob).then(() => console.log("FILE SAVED"))
|
||||
response = JSON.stringify(true);
|
||||
} catch (error) {
|
||||
const obj = {};
|
||||
const errorMsg = error.message || 'Failed to initiate download';
|
||||
obj['error'] = errorMsg;
|
||||
response = JSON.stringify(obj);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// case 'DEPLOY_AT': {
|
||||
// const requiredFields = ['name', 'description', 'tags', 'creationBytes', 'amount', 'assetId', 'type'];
|
||||
|
Loading…
x
Reference in New Issue
Block a user