Skip to content

Commit

Permalink
Display error in WebUI when download fails
Browse files Browse the repository at this point in the history
Previously we would still download the file but it would contain the error response.

Signed-off-by: Thomas Piccirello <[email protected]>
  • Loading branch information
Piccirello committed Nov 3, 2024
1 parent 1a7ebfc commit 91084b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/webui/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void WebApplication::configure()
const QString contentSecurityPolicy =
(m_isAltUIUsed
? 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())
+ (m_isHttpsEnabled ? u" upgrade-insecure-requests;"_s : QString());
if (!contentSecurityPolicy.isEmpty())
Expand Down
32 changes: 32 additions & 0 deletions src/webui/www/private/scripts/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ window.qBittorrent.Misc ??= (() => {
toFixedPointString: toFixedPointString,
containsAllTerms: containsAllTerms,
sleep: sleep,
downloadFile: downloadFile,
// variables
FILTER_INPUT_DELAY: 400,
MAX_ETA: 8640000
Expand Down Expand Up @@ -275,6 +276,37 @@ window.qBittorrent.Misc ??= (() => {
});
};

const downloadFile = async (url, defaultFileName, errorMessage = undefined) => {
errorMessage ??= "QBT_TR(Unable to download file)QBT_TR[CONTEXT=HttpServer]";
try {
const response = await fetch(url);
if ((response.status >= 200) && (response.status < 300)) {
const blob = await response.blob();
const fileNamePrefix = "filename=";
let fileName = defaultFileName;
for (const part of (response.headers.get("content-disposition") ?? "").split(";").map(s => s.trim())) {
if (part.startsWith(fileNamePrefix)) {
fileName = part.substring(fileNamePrefix.length);
if (fileName.startsWith("\"") && fileName.endsWith("\""))
fileName = fileName.slice(1, -1);
break;
}
}

const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
}
else {
alert(errorMessage);
}
}
catch (error) {
alert(errorMessage);
}
};

return exports();
})();
Object.freeze(window.qBittorrent.Misc);
10 changes: 2 additions & 8 deletions src/webui/www/private/scripts/mocha-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,16 +1115,10 @@ const initializeWindows = () => {
continue;

const name = row.full_data.name;
const url = new URI("api/v2/torrents/export");
url.setData("hash", hash);
const url = new URI("api/v2/torrents/export").setData("hash", hash).toString();

// download response to file
const element = document.createElement("a");
element.href = url;
element.download = (name + ".torrent");
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
await window.qBittorrent.Misc.downloadFile(url, `${name}.torrent`, "QBT_TR(Unable to export torrent file)QBT_TR[CONTEXT=MainWindow]");

// https://stackoverflow.com/questions/53560991/automatic-file-downloads-limited-to-10-files-on-chrome-browser
await window.qBittorrent.Misc.sleep(200);
Expand Down

0 comments on commit 91084b6

Please sign in to comment.