FileDownloader now works in Emscripten
This commit is contained in:
parent
dc834f84f8
commit
f972fd5024
5 changed files with 79 additions and 27 deletions
49
include/Helpers/FileDownloaderEmscripten.cpp
Normal file
49
include/Helpers/FileDownloaderEmscripten.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#ifdef __EMSCRIPTEN__
|
||||
#include "FileDownloader.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
std::vector<FileDownloader::DownloadHandler> FileDownloader::currentDownloads;
|
||||
|
||||
void FileDownloader::download_succeeded(emscripten_fetch_t *fetch) {
|
||||
DownloadData* downData = static_cast<DownloadData*>(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<DownloadData*>(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::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.onerror = download_failed;
|
||||
attr.userData = static_cast<void*>(newDownload.data.get());
|
||||
emscripten_fetch(&attr, url.data());
|
||||
|
||||
return newDownload.data;
|
||||
}
|
||||
|
||||
void FileDownloader::cleanup() {
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue