DownloadUtils.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #pragma once
  2. #include "Path.hpp"
  3. #include <string>
  4. #include <vector>
  5. #include <functional>
  6. #include <rapidjson/fwd.h>
  7. namespace QuickMedia {
  8. enum class DownloadResult {
  9. OK,
  10. ERR,
  11. NET_ERR
  12. };
  13. struct CommandArg {
  14. std::string option;
  15. std::string value; // Optional
  16. };
  17. // Return true the return DownloadResult::OK for the download, which also saves the result in cache if |download_to_string_cache| is used
  18. using DownloadErrorHandler = std::function<bool(std::string&)>;
  19. DownloadResult download_head_to_string(const std::string &url, std::string &result, bool use_browser_useragent = false, bool fail_on_error = true);
  20. // Returns the remote name from the content-disposition header or tries to extract the file name from url. Can return empty name
  21. DownloadResult url_get_remote_name(const std::string &url, std::string &result, bool use_browser_useragent);
  22. // Note: if |cloudflare_bypass| is set to true then tls is limited to version 1.1 and the user agent is changed
  23. DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_browser_useragent = false, bool fail_on_error = true, bool cloudflare_bypass = false, std::vector<std::string> *headers = nullptr, int download_limit = 1024 * 1024 * 100); // 100mb download limit
  24. // Note: This function saves the content to the file atomically
  25. DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_browser_useragent = false, DownloadErrorHandler error_handler = nullptr, Path cache_path = "");
  26. // Note: This function saves the content to the file atomically.
  27. // Note: if |cloudflare_bypass| is set to true then tls is limited to version 1.1 and the user agent is changed.
  28. DownloadResult download_to_file(const std::string &url, const std::string &destination_filepath, const std::vector<CommandArg> &additional_args, bool use_browser_useragent = false, bool cloudflare_bypass = false);
  29. // Returns false if there was an error trying to create the download process
  30. bool download_async_gui(const std::string &url, const std::string &file_manager_start_dir, bool no_video, const std::string &filename, bool download_no_dialog, long transient_for_window);
  31. DownloadResult download_to_json(const std::string &url, rapidjson::Document &result, const std::vector<CommandArg> &additional_args, bool use_browser_useragent = false, bool fail_on_error = true);
  32. }