#ifdef __EMSCRIPTEN__ #include "FileDownloader.hpp" #include std::vector FileDownloader::currentDownloads; void FileDownloader::download_succeeded(emscripten_fetch_t *fetch) { DownloadData* downData = static_cast(fetch->userData); downData->str = std::string(fetch->data, fetch->numBytes); downData->status = DownloadData::Status::SUCCESS; std::erase_if(currentDownloads, [fetch](DownloadHandler& d) { return (DownloadData*)fetch->userData == d.data.get(); }); emscripten_fetch_close(fetch); } void FileDownloader::download_failed(emscripten_fetch_t *fetch) { DownloadData* downData = static_cast(fetch->userData); downData->status = DownloadData::Status::FAILURE; std::erase_if(currentDownloads, [fetch](DownloadHandler& d) { return (DownloadData*)fetch->userData == d.data.get(); }); emscripten_fetch_close(fetch); } void FileDownloader::init() { } std::shared_ptr FileDownloader::download_data_from_url(std::string_view url) { DownloadHandler& newDownload = currentDownloads.emplace_back(); newDownload.data = std::make_shared(); 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.onerror = download_failed; attr.userData = static_cast(newDownload.data.get()); emscripten_fetch(&attr, url.data()); return newDownload.data; } void FileDownloader::cleanup() { } #endif