.. _program_listing_file_include_downloader.h: Program Listing for File downloader.h ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/downloader.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /* * Copyright 2018 Matthieu Gautier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ #ifndef KIWIX_DOWNLOADER_H #define KIWIX_DOWNLOADER_H #include #include #include #include #include #include namespace kiwix { class Aria2; struct DownloadedFile { DownloadedFile() : success(false) {} bool success; std::string path; }; class AriaError : public std::runtime_error { public: AriaError(const std::string& message) : std::runtime_error(message) {} }; class Download { public: typedef enum { K_ACTIVE, K_WAITING, K_PAUSED, K_ERROR, K_COMPLETE, K_REMOVED, K_UNKNOWN } StatusResult; Download() : m_status(K_UNKNOWN) {} Download(std::shared_ptr p_aria, std::string did) : mp_aria(p_aria), m_status(K_UNKNOWN), m_did(did) {}; void updateStatus(bool follow); void pauseDownload(); void resumeDownload(); void cancelDownload(); /* * Get the status of the download. */ StatusResult getStatus() const { return m_status; } /* * Get the id of the download. */ const std::string& getDid() const { return m_did; } /* * Get the id of the "second" download. * * Set only if the "first" download is a metalink and is complete. */ const std::string& getFollowedBy() const { return m_followedBy; } /* * Get the total length of the download. */ uint64_t getTotalLength() const { return m_totalLength; } /* * Get the completed length of the download. */ uint64_t getCompletedLength() const { return m_completedLength; } /* * Get the download speed of the download. */ uint64_t getDownloadSpeed() const { return m_downloadSpeed; } /* * Get the verified length of the download. */ uint64_t getVerifiedLength() const { return m_verifiedLength; } /* * Get the path (local file) of the download. */ const std::string& getPath() const { return m_path; } /* * Get the download uris of the download. */ const std::vector& getUris() const { return m_uris; } protected: std::shared_ptr mp_aria; StatusResult m_status; std::string m_did = ""; std::string m_followedBy = ""; uint64_t m_totalLength; uint64_t m_completedLength; uint64_t m_downloadSpeed; uint64_t m_verifiedLength; std::vector m_uris; std::string m_path; }; class Downloader { public: // types typedef std::vector> Options; public: // functions /* * Create a new Downloader object. * * @param sessionFileDir: The directory where aria2 will store its session file. */ explicit Downloader(std::string sessionFileDir); virtual ~Downloader(); void close(); std::shared_ptr startDownload(const std::string& uri, const std::string& downloadDir, Options options = {}); std::shared_ptr getDownload(const std::string& did); size_t getNbDownload() const; std::vector getDownloadIds() const; private: // data mutable std::mutex m_lock; std::map> m_knownDownloads; std::shared_ptr mp_aria; }; } #endif