Get filenames of downloaded files

This commit is contained in:
Yousef Khadadeh 2025-10-11 13:02:07 +03:00
parent 1175424c23
commit 48156cb4bf
5 changed files with 77 additions and 6 deletions

View file

@ -16,10 +16,24 @@ void FileDownloader::download_progress(emscripten_fetch_t *fetch) {
void FileDownloader::download_succeeded(emscripten_fetch_t *fetch) {
DownloadData* downData = static_cast<DownloadData*>(fetch->userData);
downData->str = std::string(fetch->data, fetch->numBytes);
size_t headerLength = emscripten_fetch_get_response_headers_length(fetch);
if(headerLength != 0) {
std::string header(headerLength + 1, ' ');
emscripten_fetch_get_response_headers(fetch, header.data(), header.size() + 1);
if(!header.empty())
header.pop_back(); // Remove additional null terminator
std::string newFilename = parse_http_headers_for_filename(header);
if(!newFilename.empty())
downData->fileName = newFilename;
}
downData->status = DownloadData::Status::SUCCESS;
std::erase_if(currentDownloads, [fetch](DownloadHandler& d) {
return (DownloadData*)fetch->userData == d.data.get();
});
emscripten_fetch_close(fetch);
}
@ -35,9 +49,10 @@ void FileDownloader::download_failed(emscripten_fetch_t *fetch) {
void FileDownloader::init() {
}
std::shared_ptr<FileDownloader::DownloadData> FileDownloader::download_data_from_url(std::string_view url) {
std::shared_ptr<FileDownloader::DownloadData> FileDownloader::download_data_from_url(const std::string& url) {
DownloadHandler& newDownload = currentDownloads.emplace_back();
newDownload.data = std::make_shared<DownloadData>();
newDownload.data->fileName = get_potential_filename_from_url(url);
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);