mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-19 21:03:30 -07: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:
parent
61ff683f11
commit
78a5e4ff3e
3 changed files with 33 additions and 9 deletions
|
@ -467,7 +467,7 @@ void WebApplication::configure()
|
||||||
const QString contentSecurityPolicy =
|
const QString contentSecurityPolicy =
|
||||||
(m_isAltUIUsed
|
(m_isAltUIUsed
|
||||||
? QString()
|
? QString()
|
||||||
: u"default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline'; object-src 'none'; form-action 'self';"_s)
|
: u"default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline'; object-src 'none'; form-action 'self'; frame-src 'self' blob:;"_s)
|
||||||
+ (isClickjackingProtectionEnabled ? u" frame-ancestors 'self';"_s : QString())
|
+ (isClickjackingProtectionEnabled ? u" frame-ancestors 'self';"_s : QString())
|
||||||
+ (m_isHttpsEnabled ? u" upgrade-insecure-requests;"_s : QString());
|
+ (m_isHttpsEnabled ? u" upgrade-insecure-requests;"_s : QString());
|
||||||
if (!contentSecurityPolicy.isEmpty())
|
if (!contentSecurityPolicy.isEmpty())
|
||||||
|
|
|
@ -47,6 +47,7 @@ window.qBittorrent.Misc ??= (() => {
|
||||||
toFixedPointString: toFixedPointString,
|
toFixedPointString: toFixedPointString,
|
||||||
containsAllTerms: containsAllTerms,
|
containsAllTerms: containsAllTerms,
|
||||||
sleep: sleep,
|
sleep: sleep,
|
||||||
|
downloadFile: downloadFile,
|
||||||
// variables
|
// variables
|
||||||
FILTER_INPUT_DELAY: 400,
|
FILTER_INPUT_DELAY: 400,
|
||||||
MAX_ETA: 8640000
|
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();
|
return exports();
|
||||||
})();
|
})();
|
||||||
Object.freeze(window.qBittorrent.Misc);
|
Object.freeze(window.qBittorrent.Misc);
|
||||||
|
|
|
@ -1115,16 +1115,10 @@ const initializeWindows = () => {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const name = row.full_data.name;
|
const name = row.full_data.name;
|
||||||
const url = new URI("api/v2/torrents/export");
|
const url = new URI("api/v2/torrents/export").setData("hash", hash).toString();
|
||||||
url.setData("hash", hash);
|
|
||||||
|
|
||||||
// download response to file
|
// download response to file
|
||||||
const element = document.createElement("a");
|
await window.qBittorrent.Misc.downloadFile(url, `${name}.torrent`, "QBT_TR(Unable to export torrent file)QBT_TR[CONTEXT=MainWindow]");
|
||||||
element.href = url;
|
|
||||||
element.download = (name + ".torrent");
|
|
||||||
document.body.appendChild(element);
|
|
||||||
element.click();
|
|
||||||
document.body.removeChild(element);
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/53560991/automatic-file-downloads-limited-to-10-files-on-chrome-browser
|
// https://stackoverflow.com/questions/53560991/automatic-file-downloads-limited-to-10-files-on-chrome-browser
|
||||||
await window.qBittorrent.Misc.sleep(200);
|
await window.qBittorrent.Misc.sleep(200);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue