| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #ifndef GUL_WGET_H
- #define GUL_WGET_H
- // C++17 includes the <filesystem> library, but
- // unfortunately gcc7 does not have a finalized version of it
- // it is in the <experimental/filesystem lib
- // this section includes the proper header
- // depending on whether the header exists and
- // includes that. It also sets the
- // nf namespace
- #if __has_include(<filesystem>)
- #include <filesystem>
- namespace gul
- {
- namespace fs = std::filesystem;
- }
- #elif __has_include(<experimental/filesystem>)
- #include <experimental/filesystem>
- namespace gul
- {
- namespace fs = std::experimental::filesystem;
- }
- #else
- #error There is no <filesystem> or <experimental/filesystem>
- #endif
- #include <map>
- #include "uri.h"
- namespace gul
- {
- template<typename callable_t>
- void split_path(std::string const &s, std::string const &delimiter, callable_t &&C )
- {
- size_t pos_start = 0, pos_end, delim_len = delimiter.length();
- while ((pos_end = s.find (delimiter, pos_start)) != std::string::npos)
- {
- std::string_view S( &s[pos_start], pos_end-pos_start);
- pos_start = pos_end + delim_len;
- C(S);
- }
- std::string_view S( &s[pos_start], s.size()-pos_start);
- C(S);
- }
- /**
- * @brief The curl_get class
- *
- * curl_get class is used to download data using curl.
- * This performs a system call to /usr/bin/curl or curl.exe (on windows)
- * to download the file
- */
- struct HTTP
- {
- static inline std::string CACHE_PATH = (fs::temp_directory_path() / fs::path("gul_wget")).string();
- static inline std::string CURL_PATH =
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
- "C:\\Windows\\System32\\curl.exe";
- #else
- "/usr/bin/curl";
- #endif
- static inline std::string CURL_ADDITIONAL_FLAGS = "";
- fs::path get(gul::uri const & Pd)
- {
- auto P = cache_file(Pd);
- fs::create_directories( P.parent_path() );
- std::string cmd = CURL_PATH + " "
- + CURL_ADDITIONAL_FLAGS +
- + " -s -o " + P.string() + " "
- + Pd.toString();
- std::system(cmd.c_str());
- return P;
- }
- static fs::path cache_file(gul::uri const & _uri)
- {
- std::hash<std::string> H;
- std::string urlPath = _uri.toString();
- auto h = H(urlPath);
- auto fn = fs::path(_uri.path).filename();
- if(fn.empty())
- {
- fn = _uri.host;
- }
- return fs::path(CACHE_PATH) / _uri.host / std::to_string(h) / fn;
- }
- };
- /**
- * @brief The RC class
- *
- * Local path resources. set the RESOURCE list
- */
- struct RC
- {
- static inline std::string RESOURCE_LIST = "";
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
- static inline std::string PATH_DELIMITER = ";";
- #else
- static inline std::string PATH_DELIMITER = ":";
- #endif
- fs::path get(gul::uri const & Pd)
- {
- fs::path ret;
- split_path(RESOURCE_LIST, PATH_DELIMITER, [&Pd, &ret](auto & p)
- {
- if( ret.empty() && fs::exists( fs::path(p) / Pd.path) )
- {
- ret = fs::path(p) / Pd.path;
- }
- });
- return ret;
- }
- };
- /**
- * @brief The URIGetter class
- *
- * The URIGetter class is used to access data from a URI.
- * For example, a http:// or https:// scheme will download the file form the
- * http url and provide a local cached path so that it can be read from.
- */
- struct URIGetter
- {
- using function_type = std::function<fs::path(gul::uri const&)>;
- std::map<std::string, function_type > uri_getter =
- {
- {
- "file",
- function_type([](gul::uri const & _uri)
- {
- (void)_uri;
- return _uri.path;
- })
- },
- {
- "http",
- function_type([](gul::uri const & _uri)
- {
- return HTTP().get(_uri);
- })
- },
- {
- "https",
- function_type([](gul::uri const & _uri)
- {
- return HTTP().get(_uri);
- })
- },
- {
- "rc",
- function_type([](gul::uri const & _uri)
- {
- return RC().get(_uri);
- })
- }
- };
- /**
- * @brief get
- * @param _uri
- * @param ignoreCache
- * @return
- *
- * Download a file from _uri. If ignoreCache==true, then the previously
- * downloaded file will be overwritten.
- *
- * Returns the local path to the file after it has downloaded.
- *
- * if _uri schema is file://, then it will retun the uri's path
- *
- * if _uri's schela is http or https, then it will use curl to download the file to
- * a cached location and return the filepath to the cache.
- *
- * This is a blocking call.
- */
- fs::path get(gul::uri const & _uri) const
- {
- return uri_getter.at(_uri.scheme)(_uri);
- }
- };
- }
- #endif
|