Download progress is tracked by FileDownloader

This commit is contained in:
Yousef Khadadeh 2025-10-09 17:58:12 +03:00
parent 27d334e234
commit 5c9fc917ac
3 changed files with 26 additions and 3 deletions

View file

@ -1,9 +1,18 @@
#ifdef __EMSCRIPTEN__
#include "FileDownloader.hpp"
#include <cstdio>
#include <iostream>
std::vector<FileDownloader::DownloadHandler> FileDownloader::currentDownloads;
void FileDownloader::download_progress(emscripten_fetch_t *fetch) {
DownloadData* downData = static_cast<DownloadData*>(fetch->userData);
if(fetch->totalBytes == 0)
downData->progress = 0.0f;
else
downData->progress = static_cast<float>(fetch->dataOffset) / static_cast<float>(fetch->totalBytes);
}
void FileDownloader::download_succeeded(emscripten_fetch_t *fetch) {
DownloadData* downData = static_cast<DownloadData*>(fetch->userData);
downData->str = std::string(fetch->data, fetch->numBytes);
@ -29,13 +38,13 @@ void FileDownloader::init() {
std::shared_ptr<FileDownloader::DownloadData> FileDownloader::download_data_from_url(std::string_view url) {
DownloadHandler& newDownload = currentDownloads.emplace_back();
newDownload.data = std::make_shared<DownloadData>();
newDownload.data->status = DownloadData::Status::IN_PROGRESS;
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
std::strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = download_succeeded;
attr.onprogress = download_progress;
attr.onerror = download_failed;
attr.userData = static_cast<void*>(newDownload.data.get());
emscripten_fetch(&attr, url.data());