Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions WebAPI_Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# WebAPI Changelog

## 2.15.2

* [#23357](https://github.com/qbittorrent/qBittorrent/pull/23856)
* Add `app/getFreeSpaceAtPathAction` endpoint with `path` as parameter returning the free space at the given path

## 2.15.1
* [#23357](https://github.com/qbittorrent/qBittorrent/pull/23357)
* Add `app/processInfo` endpoint returning `launch_time` (process launch time as UTC epoch seconds)
Expand Down
17 changes: 17 additions & 0 deletions src/webui/api/appcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,23 @@ void AppController::getDirectoryContentAction()
setResult(ret);
}

void AppController::getFreeSpaceAtPathAction()
{
requireParams({u"path"_s});
Path current {params().value(u"path"_s)};
const Path root = current.rootItem();
qint64 freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);

// for non-existent directories (which will be created on demand) `Utils::Fs::freeDiskSpaceOnPath`
// will return invalid value so instead query its parent/ancestor paths
while ((freeSpace < 0) && (current != root))
{
current = current.parentPath();
freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
}
setResult(QString::number(freeSpace));
}

void AppController::cookiesAction()
{
const QList<QNetworkCookie> cookies = Net::DownloadManager::instance()->allCookies();
Expand Down
1 change: 1 addition & 0 deletions src/webui/api/appcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private slots:
void defaultSavePathAction();
void sendTestEmailAction();
void getDirectoryContentAction();
void getFreeSpaceAtPathAction();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to increase WebAPI version and this endpoint should be mentioned in WebAPI Changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've increased the WebAPI version and added a mention in the changelog

void cookiesAction();
void setCookiesAction();
void rotateAPIKeyAction();
Expand Down
2 changes: 1 addition & 1 deletion src/webui/webapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

using namespace std::chrono_literals;

inline const Utils::Version<3, 2> API_VERSION {2, 15, 1};
inline const Utils::Version<3, 2> API_VERSION {2, 15, 2};

class APIController;
class AuthController;
Expand Down
33 changes: 30 additions & 3 deletions src/webui/www/private/scripts/addtorrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ window.qBittorrent.AddTorrent ??= (() => {
};

let table = null;
let size = "";
let defaultSavePath = "";
let defaultTempPath = "";
let defaultTempPathEnabled = false;
Expand Down Expand Up @@ -146,6 +147,7 @@ window.qBittorrent.AddTorrent ??= (() => {

if (isAutoTMMEnabled()) {
document.getElementById("savepath").value = defaultSavePath;
document.getElementById("size").textContent = showFreeSpace(defaultSavePath);

const downloadPathEnabled = categoryDownloadPathEnabled(categoryName);
document.getElementById("useDownloadPath").checked = downloadPathEnabled;
Expand All @@ -158,7 +160,9 @@ window.qBittorrent.AddTorrent ??= (() => {
item.nextElementSibling.value = text;

if (isAutoTMMEnabled()) {
document.getElementById("savepath").value = categorySavePath(categoryName);
const tempCategoryPath = categorySavePath(categoryName);
document.getElementById("savepath").value = tempCategoryPath;
document.getElementById("size").textContent = showFreeSpace(tempCategoryPath);

const downloadPathEnabled = categoryDownloadPathEnabled(categoryName);
document.getElementById("useDownloadPath").checked = downloadPathEnabled;
Expand Down Expand Up @@ -192,6 +196,7 @@ window.qBittorrent.AddTorrent ??= (() => {
useDownloadPath.checked = defaultTempPathEnabled;
}

document.getElementById("size").textContent = showFreeSpace(savepath.value);
savepath.disabled = autoTMMEnabled;
useDownloadPath.disabled = autoTMMEnabled;

Expand All @@ -215,6 +220,25 @@ window.qBittorrent.AddTorrent ??= (() => {
}
};

const showFreeSpace = (element) => {
if (element === "")
return;

fetch(`api/v2/app/getFreeSpaceAtPath?path=${element}`, {
method: "GET",
cache: "no-store"
})
.then(response => response.text())
.then(freeSpace => { filloutFreeSpace(freeSpace); })
.catch(error => {});
};

const filloutFreeSpace = (freeSpace) => {
document.getElementById("size").textContent = `${" QBT_TR(%1 (Free space on disk: %2))QBT_TR[CONTEXT=AddNewTorrentDialog]"
.replace("%1", size)
.replace("%2", window.qBittorrent.Misc.friendlyUnit(freeSpace, false))}`;
};

let loadMetadataTimer = -1;
const loadMetadata = (sourceUrl = undefined, downloaderName = undefined) => {
if (sourceUrl !== undefined)
Expand Down Expand Up @@ -280,8 +304,10 @@ window.qBittorrent.AddTorrent ??= (() => {
document.getElementById("infoHashV1").textContent = (metadata.infohash_v1 === undefined) ? notAvailable : (metadata.infohash_v1 || notApplicable);
document.getElementById("infoHashV2").textContent = (metadata.infohash_v2 === undefined) ? notAvailable : (metadata.infohash_v2 || notApplicable);

if (metadata.info?.length !== undefined)
document.getElementById("size").textContent = window.qBittorrent.Misc.friendlyUnit(metadata.info.length, false);
if (metadata.info?.length !== undefined) {
size = window.qBittorrent.Misc.friendlyUnit(metadata.info.length, false);
document.getElementById("size").textContent = showFreeSpace(document.getElementById("savepath").value);
}
if ((metadata.creation_date !== undefined) && (metadata.creation_date > 1))
document.getElementById("createdDate").textContent = window.qBittorrent.Misc.formatDate(new Date(metadata.creation_date * 1000));
if (metadata.comment !== undefined)
Expand Down Expand Up @@ -326,6 +352,7 @@ window.qBittorrent.AddTorrent ??= (() => {

const init = (source, downloader, fetchMetadata) => {
table = window.qBittorrent.TorrentContent.init("addTorrentFilesTableDiv", window.qBittorrent.DynamicTable.AddTorrentFilesTable);
document.getElementById("savepath").addEventListener("input", function(event) { showFreeSpace(this.value); });
if (fetchMetadata)
loadMetadata(source, downloader);
};
Expand Down
Loading