Skip to content
Merged
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand All @@ -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()
12 changes: 12 additions & 0 deletions src/DiscordWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Application.h"
#include "Host.h"
#include "PageScript.h"
#include "Downloads.h"
#include "PagePermissions.h"

namespace {
Expand Down Expand Up @@ -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<std::filesystem::path> {
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();
Expand Down
43 changes: 43 additions & 0 deletions src/Downloads.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Downloads.h"

#include <cstdlib>

#ifdef _WIN32
#include <stdlib.h>
#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;
}
14 changes: 14 additions & 0 deletions src/Downloads.h
Original file line number Diff line number Diff line change
@@ -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 <filesystem>
#include <string>

// 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);
70 changes: 70 additions & 0 deletions tests/Downloads_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Unit tests for where downloads are saved. Exits with 1 when a check fails, and prints each failure.
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>

#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;
}