Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Dec 3, 2024
1 parent 99aa22b commit 57b2dbf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/httpfetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ You should have received a copy of the GNU General Public License
along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
*/

#if defined(__EMSCRIPTEN__)
#include <emscripten/fetch.h>
#endif

#include "httpfetch.h"
#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include <iostream>
Expand Down Expand Up @@ -810,6 +814,19 @@ static void httpfetch_request_clear(u64 caller)
void httpfetch_sync(const HTTPFetchRequest &fetch_request,
HTTPFetchResult &fetch_result)
{
// https://github.com/emscripten-core/emscripten/issues/4906
#if defined(__EMSCRIPTEN__)
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
auto *fetch = emscripten_fetch(&attr, fetch_request.url.c_str());
fetch_result.response_code = fetch->status;
fetch_result.succeeded = fetch_result.response_code == 200;
fetch_result.data = std::string((const char *)fetch->data, fetch->numBytes);
// errorstream << "emsfetch=" << fetch_result.response_code << " " << fetch_result.succeeded << " : " << readBuffer.size() << "\n";
emscripten_fetch_close(fetch);
#else
// Create ongoing fetch data and make a cURL handle
// Set cURL options based on HTTPFetchRequest
CurlHandlePool pool;
Expand All @@ -818,6 +835,7 @@ void httpfetch_sync(const HTTPFetchRequest &fetch_request,
CURLcode res = ongoing.start(NULL);
// Update fetch result
fetch_result = *ongoing.complete(res);
#endif
}

#else // USE_CURL
Expand Down
20 changes: 15 additions & 5 deletions src/mapgen/earth/hgt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,18 @@ const auto http_to_file = [](const std::string &url, const std::string &zipfull)

actionstream << req.url << " " << res.succeeded << " " << res.response_code << " "
<< res.data.size() << "\n";
if (!res.succeeded || res.response_code >= 300)
if (!res.succeeded || res.response_code >= 300) {
return uintmax_t{0};
}

if (!res.data.size())
if (!res.data.size()) {
return uintmax_t{0};
}

std::ofstream(zipfull) << res.data;
if (!std::filesystem::exists(zipfull))
if (!std::filesystem::exists(zipfull)) {
return uintmax_t{0};
}
return std::filesystem::file_size(zipfull);
};

Expand Down Expand Up @@ -394,7 +397,13 @@ bool height_hgt::load(ll_t lat, ll_t lon)
fs::CreateAllDirs(ffolder);
multi_http_to_file(zstfile,
{
"http://cdn.freeminer.org/earth/" + zstfile,
#if defined(__EMSCRIPTEN__)
"/"
#else
"http://cdn.freeminer.org/"
#endif
"earth/" +
zstfile,
},
zstdfull);
if (std::filesystem::exists(zstdfull) && std::filesystem::file_size(zstdfull)) {
Expand Down Expand Up @@ -438,6 +447,7 @@ bool height_hgt::load(ll_t lat, ll_t lon)
//#if 1 //!defined(_WIN32)
// DUMP(filefull, zipfull);

#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if (srtmTile.empty() && !std::filesystem::exists(filefull)) {

// TODO: https://viewfinderpanoramas.org/Coverage%20map%20viewfinderpanoramas_org15.htm
Expand Down Expand Up @@ -466,7 +476,7 @@ bool height_hgt::load(ll_t lat, ll_t lon)
set_ratio(filesize);
}
}
//#endif
#endif

// TODO: first try load unpached file, then unpack zip
if (srtmTile.empty()) {
Expand Down

0 comments on commit 57b2dbf

Please sign in to comment.