Update ImageUtilities.plugin.js

This commit is contained in:
Mirco Wittrien 2022-12-08 19:15:15 +01:00
parent 1dbd92f67e
commit e58b48d5eb
1 changed files with 24 additions and 18 deletions

View File

@ -2,7 +2,7 @@
* @name ImageUtilities * @name ImageUtilities
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 5.0.1 * @version 5.0.2
* @description Adds several Utilities for Images/Videos (Gallery, Download, Reverse Search, Zoom, Copy, etc.) * @description Adds several Utilities for Images/Videos (Gallery, Download, Reverse Search, Zoom, Copy, etc.)
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -1446,31 +1446,37 @@ module.exports = (_ => {
downloadFile (url, path, fallbackUrl, alternativeName) { downloadFile (url, path, fallbackUrl, alternativeName) {
url = url.startsWith("/assets") ? (window.location.origin + url) : url; url = url.startsWith("/assets") ? (window.location.origin + url) : url;
BDFDB.LibraryModules.FileRequestUtils.getFileData(url).then(body => { BDFDB.DiscordUtils.requestFileData(url, {timeout: 3000}, (error, buffer) => {
let extension = this.getFileExtension(new Uint8Array(body)); if (error || !buffer) this.downloadFile(fallbackUrl, path, null, alternativeName);
if (!extension) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", BDFDB.LanguageUtils.LanguageStrings.IMAGE).replace("{{var1}}", path), {type: "danger"});
else { else {
let type = fileTypes[extension].video ? BDFDB.LanguageUtils.LanguageStrings.VIDEO : BDFDB.LanguageUtils.LanguageStrings.IMAGE; let extension = this.getFileExtension(new Uint8Array(buffer));
BDFDB.LibraryRequires.fs.writeFile(this.getFileName(path, (alternativeName || url.split("/").pop().split(".").slice(0, -1).join(".") || "unknown").slice(0, 35), extension, 0), Buffer.from(body), error => { if (!extension) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", BDFDB.LanguageUtils.LanguageStrings.IMAGE).replace("{{var1}}", path), {type: "danger"});
if (error) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", type).replace("{{var1}}", path), {type: "danger"}); else {
else BDFDB.NotificationUtils.toast(this.labels.toast_save_success.replace("{{var0}}", type).replace("{{var1}}", path), {type: "success"}); let type = fileTypes[extension].video ? BDFDB.LanguageUtils.LanguageStrings.VIDEO : BDFDB.LanguageUtils.LanguageStrings.IMAGE;
}); BDFDB.LibraryRequires.fs.writeFile(this.getFileName(path, (alternativeName || url.split("/").pop().split(".").slice(0, -1).join(".") || "unknown").slice(0, 35), extension, 0), Buffer.from(buffer), error => {
if (error) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", type).replace("{{var1}}", path), {type: "danger"});
else BDFDB.NotificationUtils.toast(this.labels.toast_save_success.replace("{{var0}}", type).replace("{{var1}}", path), {type: "success"});
});
}
} }
}); });
} }
downloadFileAs (url, fallbackUrl, alternativeName) { downloadFileAs (url, fallbackUrl, alternativeName) {
url = url.startsWith("/assets") ? (window.location.origin + url) : url; url = url.startsWith("/assets") ? (window.location.origin + url) : url;
BDFDB.LibraryModules.FileRequestUtils.getFileData(url).then(body => { BDFDB.DiscordUtils.requestFileData(url, {timeout: 3000}, (error, buffer) => {
let extension = this.getFileExtension(new Uint8Array(body)); if (error || !buffer) this.downloadFileAs(fallbackUrl, null, alternativeName);
if (!extension) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", BDFDB.LanguageUtils.LanguageStrings.IMAGE).replace("{{var1}}", "PC"), {type: "danger"});
else { else {
let hrefURL = window.URL.createObjectURL(new Blob([body], {type: this.getMimeType(extension)})); let extension = this.getFileExtension(new Uint8Array(buffer));
let tempLink = document.createElement("a"); if (!extension) BDFDB.NotificationUtils.toast(this.labels.toast_save_failed.replace("{{var0}}", BDFDB.LanguageUtils.LanguageStrings.IMAGE).replace("{{var1}}", "PC"), {type: "danger"});
tempLink.href = hrefURL; else {
tempLink.download = `${(alternativeName || url.split("/").pop().split(".").slice(0, -1).join(".") || "unknown").slice(0, 35)}.${extension}`; let hrefURL = window.URL.createObjectURL(new Blob([buffer], {type: this.getMimeType(extension)}));
tempLink.click(); let tempLink = document.createElement("a");
window.URL.revokeObjectURL(hrefURL); tempLink.href = hrefURL;
tempLink.download = `${(alternativeName || url.split("/").pop().split(".").slice(0, -1).join(".") || "unknown").slice(0, 35)}.${extension}`;
tempLink.click();
window.URL.revokeObjectURL(hrefURL);
}
} }
}); });
} }