-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for downloading zipped input data from URL
Closes #364.
- Loading branch information
Showing
12 changed files
with
113 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "download_file.h" | ||
#include "HealthGPS/program_dirs.h" | ||
|
||
#include <curlpp/Easy.hpp> | ||
#include <curlpp/Exception.hpp> | ||
#include <curlpp/Options.hpp> | ||
#include <curlpp/cURLpp.hpp> | ||
#include <fmt/format.h> | ||
|
||
#include <fstream> | ||
#include <random> | ||
#include <sstream> | ||
|
||
namespace { | ||
std::filesystem::path get_temporary_file_path(const std::string &file_prefix, | ||
const std::string &file_extension) { | ||
const auto tmp_dir = hgps::get_temporary_directory(); | ||
std::filesystem::create_directories(tmp_dir); | ||
|
||
std::mt19937 prng(std::random_device{}()); | ||
std::uniform_int_distribution<unsigned> rand; | ||
std::filesystem::path path; | ||
while (true) { | ||
std::stringstream ss; | ||
ss << file_prefix << std::hex << rand(prng) << file_extension; | ||
path = tmp_dir / ss.str(); | ||
if (!std::filesystem::exists(path)) { | ||
return path; | ||
} | ||
} | ||
} | ||
} // anonymous namespace | ||
|
||
namespace hgps::data { | ||
std::filesystem::path download_file(const std::string &url, | ||
const std::filesystem::path &download_path) { | ||
std::ofstream ofs{download_path}; | ||
if (!ofs) { | ||
throw std::runtime_error(fmt::format("Failed to create file {}", download_path.string())); | ||
} | ||
|
||
curlpp::Cleanup cleanup; | ||
|
||
// Our request to be sent | ||
curlpp::Easy request; | ||
request.setOpt<curlpp::options::Url>(url); | ||
request.setOpt<curlpp::options::WriteStream>(&ofs); | ||
|
||
// Make request | ||
request.perform(); | ||
|
||
return download_path; | ||
} | ||
|
||
std::filesystem::path download_file_to_temporary(const std::string &url, | ||
const std::string &file_extension) { | ||
const auto download_path = get_temporary_file_path("data", file_extension); | ||
return download_file(url, download_path); | ||
} | ||
|
||
} // namespace hgps::data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
|
||
namespace hgps::data { | ||
/// @brief Download the file at the specified URL | ||
/// @param url URL to download from | ||
/// @param download_path Destination for downloaded files, including filename | ||
/// @return Path to downloaded file | ||
std::filesystem::path download_file(const std::string &url, | ||
const std::filesystem::path &download_path); | ||
|
||
/// @brief Download the file at the specified URL to a temporary folder | ||
/// @param url URL to download from | ||
/// @param file_extension File extension (including dot) for downloaded file | ||
/// @return Path to downloaded file | ||
std::filesystem::path download_file_to_temporary(const std::string &url, | ||
const std::string &file_extension); | ||
} // namespace hgps::data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters