download.hpp (2838B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_DOWNLOAD_HPP 19 #define REAPACK_DOWNLOAD_HPP 20 21 #include "config.hpp" 22 #include "path.hpp" 23 #include "thread.hpp" 24 25 #include <curl/curl.h> 26 #include <fstream> 27 #include <memory> 28 #include <sstream> 29 30 class Hash; 31 32 class DownloadContext { 33 public: 34 static void GlobalInit(); 35 static void GlobalCleanup(); 36 37 DownloadContext(); 38 ~DownloadContext(); 39 40 operator CURL*() { return m_curl; } 41 42 private: 43 CURL *m_curl; 44 }; 45 46 class Download : public ThreadTask { 47 public: 48 enum Flag { 49 NoCacheFlag = 1<<0, 50 }; 51 52 Download(const std::string &url, const NetworkOpts &, int flags = 0); 53 54 void setName(const std::string &); 55 void setExpectedChecksum(const std::string &checksum) { 56 m_expectedChecksum = checksum; 57 } 58 const std::string &url() const { return m_url; } 59 60 bool concurrent() const override { return true; } 61 bool run() override; 62 63 protected: 64 virtual std::ostream *openStream() = 0; 65 virtual void closeStream() {} 66 67 private: 68 struct WriteContext { 69 std::ostream *stream; 70 std::unique_ptr<Hash> hash; 71 72 void write(const char *data, size_t len); 73 bool checkChecksum(const std::string &expected) const; 74 }; 75 76 bool has(Flag f) const { return (m_flags & f) != 0; } 77 static size_t WriteData(char *, size_t, size_t, void *); 78 static int UpdateProgress(void *, double, double, double, double); 79 80 std::string m_url; 81 std::string m_expectedChecksum; 82 NetworkOpts m_opts; 83 int m_flags; 84 }; 85 86 class MemoryDownload : public Download { 87 public: 88 MemoryDownload(const std::string &url, const NetworkOpts &, int flags = 0); 89 90 std::string contents() const { return m_stream.str(); } 91 92 protected: 93 std::ostream *openStream() override { return &m_stream; } 94 95 private: 96 std::stringstream m_stream; 97 }; 98 99 class FileDownload : public Download { 100 public: 101 FileDownload(const Path &target, const std::string &url, 102 const NetworkOpts &, int flags = 0); 103 104 const TempPath &path() const { return m_path; } 105 bool save(); 106 107 protected: 108 std::ostream *openStream() override; 109 void closeStream() override; 110 111 private: 112 TempPath m_path; 113 std::ofstream m_stream; 114 }; 115 116 #endif