From 6c8db2e78117ab66bd221bcb9aa464984c0b1646 Mon Sep 17 00:00:00 2001 From: Jon Date: Sun, 8 Nov 2026 14:44:18 +0000 Subject: [PATCH] Save Discord's downloads to the Downloads folder, without replacing files DiscordWindow installs a download chooser: a download goes to the user's Downloads folder under the name the page suggests, and gets a number when the name is taken. The naming is in Downloads, with unit tests behind DW_BUILD_TESTS. Needs the WebViewWindow change that adds setDownloadChooser. --- CMakeLists.txt | 5 +++ src/DiscordWindow.cpp | 12 +++++++ src/Downloads.cpp | 43 ++++++++++++++++++++++++ src/Downloads.h | 14 ++++++++ tests/Downloads_test.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 src/Downloads.cpp create mode 100644 src/Downloads.h create mode 100644 tests/Downloads_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ba1833e..b65dc86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(discord_webview src/Sha256.cpp src/UpdateCheck.cpp src/main.cpp + src/Downloads.cpp src/PagePermissions.cpp ${DW_HOST} ) @@ -66,4 +67,8 @@ if(DW_BUILD_TESTS) target_include_directories(permission_policy_test PRIVATE src) target_link_libraries(permission_policy_test PRIVATE webview_window) add_test(NAME permission_policy COMMAND permission_policy_test) + + add_executable(downloads_test tests/Downloads_test.cpp src/Downloads.cpp) + target_include_directories(downloads_test PRIVATE src) + add_test(NAME downloads COMMAND downloads_test) endif() diff --git a/src/DiscordWindow.cpp b/src/DiscordWindow.cpp index becca47..5c4791f 100644 --- a/src/DiscordWindow.cpp +++ b/src/DiscordWindow.cpp @@ -8,6 +8,7 @@ #include "Application.h" #include "Host.h" #include "PageScript.h" +#include "Downloads.h" #include "PagePermissions.h" namespace { @@ -47,6 +48,17 @@ DiscordWindow::DiscordWindow(Application& app) : WebViewWindow(WindowOptions{ .hardwareAcceleration = HardwareAcceleration::Always, }), app_(app) { + // Downloads are saved to the Downloads folder, under the name the page suggests, and never over a file already there. + setDownloadChooser([](const std::string& name) -> std::optional { + const std::filesystem::path folder = downloadsFolder(); + try { + std::filesystem::create_directories(folder); + } catch (const std::exception&) { + // Without the folder the platform's default is used. + return std::nullopt; + } + return uniqueDownloadPath(folder, name); + }); auto& methods = handler(); methods.on("moveDrag", [](WebViewWindow& window, const Json&, const Reply& reply) { window.beginMove(); diff --git a/src/Downloads.cpp b/src/Downloads.cpp new file mode 100644 index 0000000..69b4996 --- /dev/null +++ b/src/Downloads.cpp @@ -0,0 +1,43 @@ +#include "Downloads.h" + +#include + +#ifdef _WIN32 +#include +#define DW_TEXT(text) L##text +#else +#define DW_TEXT(text) text +#endif + +std::filesystem::path downloadsFolder() { +#ifdef _WIN32 + const wchar_t* home = _wgetenv(L"USERPROFILE"); +#else + const char* home = std::getenv("HOME"); +#endif + if (!home || !*home) { + return std::filesystem::path("Downloads"); + } + return std::filesystem::path(home) / "Downloads"; +} + +std::filesystem::path uniqueDownloadPath(const std::filesystem::path& folder, const std::string& suggestedName) { + std::filesystem::path name = std::filesystem::path(suggestedName).filename(); + if (name.empty() || name == "." || name == "..") { + name = "download"; + } + std::filesystem::path candidate = folder / name; + // The numbered names keep the original's stem and extension: "archive.tar" becomes "archive.tar (1).gz" for "archive.tar.gz". + const std::filesystem::path::string_type stem = name.stem().native(); + const std::filesystem::path::string_type extension = name.extension().native(); + for (int number = 1; std::filesystem::exists(candidate); ++number) { + std::filesystem::path::string_type numbered = stem; + numbered += DW_TEXT(" ("); + const std::string digits = std::to_string(number); + numbered += std::filesystem::path::string_type(digits.begin(), digits.end()); + numbered += DW_TEXT(")"); + numbered += extension; + candidate = folder / numbered; + } + return candidate; +} diff --git a/src/Downloads.h b/src/Downloads.h new file mode 100644 index 0000000..8cabd99 --- /dev/null +++ b/src/Downloads.h @@ -0,0 +1,14 @@ +// Where Discord's downloads are saved: the user's Downloads folder, under the name the page suggests, without replacing a file that's +// already there. +#pragma once + +#include +#include + +// The user's Downloads folder. +std::filesystem::path downloadsFolder(); + +// A path in folder for a file called suggestedName that isn't already there: the name itself, or "name (1)", "name (2)" and so on +// when it is. The name is reduced to its file name, so a page can't choose a folder, and an empty name is saved as "download". +// Nothing is created. +std::filesystem::path uniqueDownloadPath(const std::filesystem::path& folder, const std::string& suggestedName); diff --git a/tests/Downloads_test.cpp b/tests/Downloads_test.cpp new file mode 100644 index 0000000..e015b2a --- /dev/null +++ b/tests/Downloads_test.cpp @@ -0,0 +1,70 @@ +// Unit tests for where downloads are saved. Exits with 1 when a check fails, and prints each failure. +#include +#include +#include +#include + +#include "Downloads.h" + +namespace fs = std::filesystem; + +namespace { + +int failures = 0; + +void expectPath(const fs::path& got, const fs::path& want, const std::string& label) { + if (got != want) { + std::printf("FAIL: %s gave %s, want %s\n", label.c_str(), got.string().c_str(), want.string().c_str()); + ++failures; + } +} + +void touch(const fs::path& path) { + std::ofstream(path) << "existing"; +} + +} // namespace + +int main() { + const fs::path folder = fs::temp_directory_path() / "discordwebview-downloads-test"; + fs::remove_all(folder); + fs::create_directories(folder); + + // A name that isn't there yet is used as it is, and nothing is created. + expectPath(uniqueDownloadPath(folder, "hello.txt"), folder / "hello.txt", "a free name"); + if (fs::exists(folder / "hello.txt")) { + std::printf("FAIL: uniqueDownloadPath created the file\n"); + ++failures; + } + + // A name already there gets a number, and the numbers go on from there. + touch(folder / "hello.txt"); + expectPath(uniqueDownloadPath(folder, "hello.txt"), folder / "hello (1).txt", "a taken name"); + touch(folder / "hello (1).txt"); + expectPath(uniqueDownloadPath(folder, "hello.txt"), folder / "hello (2).txt", "a second taken name"); + + // The number goes before the last extension, so a double extension stays together. + touch(folder / "archive.tar.gz"); + expectPath(uniqueDownloadPath(folder, "archive.tar.gz"), folder / "archive.tar (1).gz", "a double extension"); + + // The page can't choose a folder: only the file name is used. + expectPath(uniqueDownloadPath(folder, "../../escape.txt"), folder / "escape.txt", "a name with folders"); + + // A name with nothing usable is saved as "download". + expectPath(uniqueDownloadPath(folder, ""), folder / "download", "an empty name"); + expectPath(uniqueDownloadPath(folder, ".."), folder / "download", "a parent folder name"); + + // The Downloads folder is the user's, and is named Downloads. + if (downloadsFolder().filename() != "Downloads") { + std::printf("FAIL: downloadsFolder() is %s\n", downloadsFolder().string().c_str()); + ++failures; + } + + fs::remove_all(folder); + if (failures == 0) { + std::printf("All download checks passed.\n"); + return 0; + } + std::printf("%d download check(s) failed.\n", failures); + return 1; +}