mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-02 21:52:32 -06:00
WebUI: Display error when download fails
Previously we would still download the file but it would contain the error response, resulting in an invalid file. To test: export a .torrent file for a torrent that hasn't yet downloaded metadata PR #21696. Signed-off-by: Thomas Piccirello <thomas@piccirello.com>
This commit is contained in:
committed by
GitHub
parent
61ff683f11
commit
78a5e4ff3e
@@ -47,6 +47,7 @@ window.qBittorrent.Misc ??= (() => {
|
||||
toFixedPointString: toFixedPointString,
|
||||
containsAllTerms: containsAllTerms,
|
||||
sleep: sleep,
|
||||
downloadFile: downloadFile,
|
||||
// variables
|
||||
FILTER_INPUT_DELAY: 400,
|
||||
MAX_ETA: 8640000
|
||||
@@ -275,6 +276,35 @@ window.qBittorrent.Misc ??= (() => {
|
||||
});
|
||||
};
|
||||
|
||||
const downloadFile = async (url, defaultFileName, errorMessage = "QBT_TR(Unable to download file)QBT_TR[CONTEXT=HttpServer]") => {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
alert(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const fileNamePrefix = "attachment; filename=";
|
||||
const fileNameHeader = response.headers.get("content-disposition");
|
||||
let fileName = defaultFileName;
|
||||
if (fileNameHeader.startsWith(fileNamePrefix)) {
|
||||
fileName = fileNameHeader.substring(fileNamePrefix.length);
|
||||
if (fileName.startsWith("\"") && fileName.endsWith("\""))
|
||||
fileName = fileName.slice(1, -1);
|
||||
}
|
||||
|
||||
const link = document.createElement("a");
|
||||
link.href = window.URL.createObjectURL(blob);
|
||||
link.download = fileName;
|
||||
link.click();
|
||||
link.remove();
|
||||
}
|
||||
catch (error) {
|
||||
alert(errorMessage);
|
||||
}
|
||||
};
|
||||
|
||||
return exports();
|
||||
})();
|
||||
Object.freeze(window.qBittorrent.Misc);
|
||||
|
||||
Reference in New Issue
Block a user