game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
file_download_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// file_download_service.h
4// ==============================================================================
5// File download service with progress tracking
6// ==============================================================================
7// Provides file download functionality using curl with progress callbacks,
8// resume support, checksum verification, and batch downloads.
9// ==============================================================================
10
11#pragma once
12
13#include <atomic>
14#include <cstdint>
15#include <functional>
16#include <map>
17#include <mutex>
18#include <string>
19#include <vector>
20
21// ==============================================================================
22// Download Progress Information
23// ==============================================================================
24
26{
27 uint64_t bytesDownloaded = 0;
28 uint64_t totalBytes = 0;
29 double speedBytesPerSec = 0.0;
31 std::string currentFile;
32 std::string statusMessage;
33 bool paused = false;
34 bool cancelled = false;
35};
36
37// ==============================================================================
38// Download Request
39// ==============================================================================
40
42{
43 std::string url;
44 std::string localPath;
45 std::string expectedChecksum; // Optional MD5/SHA256
46 std::string checksumType; // "md5", "sha256", or empty
47 bool allowResume = true;
48 int timeoutSeconds = 300; // 5 minute default timeout
49 std::map<std::string, std::string> headers; // Custom HTTP headers
50};
51
52// ==============================================================================
53// Download Result
54// ==============================================================================
55
57{
58 bool success = false;
59 std::string localPath;
60 std::string errorMessage;
61 uint64_t bytesDownloaded = 0;
63 bool checksumVerified = false;
64};
65
66// ==============================================================================
67// File Download Service
68// ==============================================================================
69
70/**
71 * @class FileDownloadService
72 * @brief Provides file download functionality with progress tracking
73 *
74 * Uses curl command-line for downloads, following the existing pattern
75 * in the codebase. Supports progress callbacks, resume, and checksums.
76 */
78{
79 public:
80 using ProgressCallback = std::function<void(const DownloadProgress&)>;
81 using CompletionCallback = std::function<void(const DownloadResult&)>;
82
83 // =========================================================================
84 // Synchronous Downloads
85 // =========================================================================
86
87 /**
88 * @brief Download a file with optional progress tracking
89 * @param request Download configuration
90 * @param progressCallback Called periodically with progress updates
91 * @return Result of the download operation
92 */
93 static DownloadResult download(const DownloadRequest& request,
94 const ProgressCallback& progressCallback = nullptr);
95
96 /**
97 * @brief Download multiple files sequentially
98 * @param requests List of download requests
99 * @param progressCallback Called with aggregate progress
100 * @return Vector of results (one per request)
101 */
102 static std::vector<DownloadResult> downloadBatch(const std::vector<DownloadRequest>& requests,
103 ProgressCallback progressCallback = nullptr);
104
105 // =========================================================================
106 // Asynchronous Downloads
107 // =========================================================================
108
109 /**
110 * @brief Download a file asynchronously
111 * @param request Download configuration
112 * @param onComplete Called when download finishes
113 * @param onProgress Called periodically with progress updates
114 */
115 static void downloadAsync(const DownloadRequest& request, const CompletionCallback& onComplete,
116 const ProgressCallback& onProgress = nullptr);
117
118 /**
119 * @brief Download multiple files asynchronously
120 * @param requests List of download requests
121 * @param onComplete Called when all downloads finish
122 * @param onProgress Called with aggregate progress
123 */
124 static void
125 downloadBatchAsync(const std::vector<DownloadRequest>& requests,
126 const std::function<void(std::vector<DownloadResult>)>& onComplete,
127 const ProgressCallback& onProgress = nullptr);
128
129 // =========================================================================
130 // Control
131 // =========================================================================
132
133 /**
134 * @brief Cancel the current download operation
135 */
136 static void cancel();
137
138 /**
139 * @brief Check if a download is currently in progress
140 */
141 static bool isDownloading();
142
143 // =========================================================================
144 // Utilities
145 // =========================================================================
146
147 /**
148 * @brief Verify file checksum
149 * @param filePath Path to the file
150 * @param expected Expected checksum value
151 * @param type Checksum type: "md5" or "sha256"
152 * @return True if checksum matches
153 */
154 static bool verifyChecksum(const std::string& filePath, const std::string& expected,
155 const std::string& type);
156
157 /**
158 * @brief Calculate file checksum
159 * @param filePath Path to the file
160 * @param type Checksum type: "md5" or "sha256"
161 * @return Checksum string (lowercase hex) or empty on error
162 */
163 static std::string calculateChecksum(const std::string& filePath, const std::string& type);
164
165 /**
166 * @brief Get file size from URL without downloading
167 * @param url URL to check
168 * @return File size in bytes, or 0 if unknown
169 */
170 static uint64_t getRemoteFileSize(const std::string& url);
171
172 /**
173 * @brief Check if URL is accessible
174 * @param url URL to check
175 * @return True if URL returns 200 OK
176 */
177 static bool isUrlAccessible(const std::string& url);
178
179 private:
180 // Parse curl progress output
181 static DownloadProgress parseProgressOutput(const std::string& line,
182 const std::string& filename);
183
184 // Build curl command with appropriate options
185 static std::string buildCurlCommand(const DownloadRequest& request, bool withProgress);
186
187 // State
188 static std::atomic<bool> cancelled_;
189 static std::atomic<bool> downloading_;
190 static std::mutex downloadMutex_;
191};
Provides file download functionality with progress tracking.
static DownloadResult download(const DownloadRequest &request, const ProgressCallback &progressCallback=nullptr)
Download a file with optional progress tracking.
static void downloadBatchAsync(const std::vector< DownloadRequest > &requests, const std::function< void(std::vector< DownloadResult >)> &onComplete, const ProgressCallback &onProgress=nullptr)
Download multiple files asynchronously.
static std::vector< DownloadResult > downloadBatch(const std::vector< DownloadRequest > &requests, ProgressCallback progressCallback=nullptr)
Download multiple files sequentially.
static bool isDownloading()
Check if a download is currently in progress.
static std::string calculateChecksum(const std::string &filePath, const std::string &type)
Calculate file checksum.
static void cancel()
Cancel the current download operation.
std::function< void(const DownloadResult &)> CompletionCallback
static bool isUrlAccessible(const std::string &url)
Check if URL is accessible.
static uint64_t getRemoteFileSize(const std::string &url)
Get file size from URL without downloading.
static bool verifyChecksum(const std::string &filePath, const std::string &expected, const std::string &type)
Verify file checksum.
static void downloadAsync(const DownloadRequest &request, const CompletionCallback &onComplete, const ProgressCallback &onProgress=nullptr)
Download a file asynchronously.
std::function< void(const DownloadProgress &)> ProgressCallback
Progress information for downloads.
std::string currentFile
Current file being processed.
std::map< std::string, std::string > headers