curl.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef GUL_CURL_H
  2. #define GUL_CURL_H
  3. #include <string.h>
  4. #include "../uri.h"
  5. // C++17 includes the <filesystem> library, but
  6. // unfortunately gcc7 does not have a finalized version of it
  7. // it is in the <experimental/filesystem lib
  8. // this section includes the proper header
  9. // depending on whether the header exists and
  10. // includes that. It also sets the
  11. // nf namespace
  12. #if __has_include(<filesystem>)
  13. #include <filesystem>
  14. namespace gul
  15. {
  16. namespace fs = std::filesystem;
  17. }
  18. #elif __has_include(<experimental/filesystem>)
  19. #include <experimental/filesystem>
  20. namespace gul
  21. {
  22. namespace fs = std::experimental::filesystem;
  23. }
  24. #else
  25. #error There is no <filesystem> or <experimental/filesystem>
  26. #endif
  27. namespace gul
  28. {
  29. /**
  30. * @brief The HTTP2 class
  31. *
  32. * A helper class for curl to get files from the internet
  33. */
  34. struct CURL
  35. {
  36. std::string CACHE_PATH = (fs::temp_directory_path() / fs::path("gul_wget")).string();
  37. std::string CURL_PATH =
  38. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  39. "C:\\Windows\\System32\\curl.exe";
  40. #else
  41. "/usr/bin/curl";
  42. #endif
  43. std::string CURL_ADDITIONAL_FLAGS = " -s ";
  44. /**
  45. * @brief get_or_cached
  46. * @param _url
  47. * @return
  48. *
  49. * Gets the path to the local cached file if it exists,
  50. * or downloads it if it doesn't exist
  51. */
  52. fs::path get_cached_or_download(gul::uri const & _url) const
  53. {
  54. auto P = cache_file(_url);
  55. if(fs::exists(P))
  56. {
  57. return P;
  58. }
  59. return get(_url);
  60. }
  61. /**
  62. * @brief get
  63. * @param Pd
  64. * @return
  65. *
  66. * Calls the curl command to get the file from _url
  67. * and returns the file path of where it's stored.
  68. *
  69. * This is a blocking call.
  70. */
  71. fs::path get(gul::uri const & _url) const
  72. {
  73. auto P = cache_file(_url);
  74. fs::create_directories( P.parent_path() );
  75. std::string cmd = CURL_PATH + " "
  76. + CURL_ADDITIONAL_FLAGS +
  77. + " -o " + P.string() + " "
  78. + _url.toString();
  79. std::system(cmd.c_str());
  80. return P;
  81. }
  82. /**
  83. * @brief cache_file
  84. * @param _uri
  85. * @return
  86. *
  87. * Returns the path to where this uri will be stored
  88. */
  89. fs::path cache_file(gul::uri const & _uri) const
  90. {
  91. std::hash<std::string> H;
  92. std::string urlPath = _uri.toString();
  93. auto h = H(urlPath);
  94. auto fn = fs::path(_uri.path).filename();
  95. if(fn.empty())
  96. {
  97. fn = _uri.host;
  98. }
  99. return fs::path(CACHE_PATH) / _uri.host / std::to_string(h) / fn;
  100. }
  101. };
  102. }
  103. #endif