2026-05-11 23:06:39 +03:00
|
|
|
/*
|
|
|
|
|
* InfiniPaint
|
|
|
|
|
* Copyright (C) 2025-2026 Yousef Khadadeh
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* (at your option) 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-01 17:54:23 +03:00
|
|
|
#pragma once
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <atomic>
|
2026-02-06 08:55:51 +03:00
|
|
|
#include <string>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <algorithm>
|
2025-10-09 13:04:39 +03:00
|
|
|
|
|
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <emscripten/fetch.h>
|
|
|
|
|
#endif
|
2025-10-01 17:54:23 +03:00
|
|
|
|
|
|
|
|
class FileDownloader {
|
|
|
|
|
public:
|
|
|
|
|
struct DownloadData {
|
|
|
|
|
enum class Status {
|
|
|
|
|
IN_PROGRESS = 0,
|
|
|
|
|
SUCCESS,
|
|
|
|
|
FAILURE
|
|
|
|
|
};
|
|
|
|
|
std::string str;
|
2025-10-11 13:02:07 +03:00
|
|
|
std::string fileName;
|
2025-10-09 17:58:12 +03:00
|
|
|
std::atomic<float> progress = 0;
|
|
|
|
|
std::atomic<Status> status = DownloadData::Status::IN_PROGRESS;
|
2025-10-01 17:54:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void init();
|
2025-10-11 13:02:07 +03:00
|
|
|
static std::shared_ptr<DownloadData> download_data_from_url(const std::string& url);
|
2025-10-01 17:54:23 +03:00
|
|
|
static void cleanup();
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
struct DownloadHandler {
|
2025-10-09 13:04:39 +03:00
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
|
CURL* eHandle;
|
|
|
|
|
#endif
|
2025-10-01 17:54:23 +03:00
|
|
|
std::shared_ptr<DownloadData> data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static std::vector<DownloadHandler> currentDownloads;
|
|
|
|
|
|
2025-10-11 13:02:07 +03:00
|
|
|
static std::string get_potential_filename_from_url(const std::string& url);
|
|
|
|
|
static std::string parse_http_headers_for_filename(std::string header);
|
|
|
|
|
|
2025-10-09 13:04:39 +03:00
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
|
static std::mutex newDownloadsMutex;
|
|
|
|
|
static std::vector<DownloadHandler> newDownloads;
|
|
|
|
|
static std::atomic<bool> shutdownThread;
|
|
|
|
|
static CURLM* mHandle;
|
|
|
|
|
static std::unique_ptr<std::thread> downloadThread;
|
|
|
|
|
|
|
|
|
|
static void save_to_string(CURL* curl, std::string* str);
|
2025-10-09 17:58:12 +03:00
|
|
|
static int download_progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
|
2025-10-11 13:02:07 +03:00
|
|
|
static size_t header_callback(char* buffer, size_t size, size_t nitems, void* userData);
|
2025-10-09 13:04:39 +03:00
|
|
|
static void disable_ssl_verification(CURL* curl);
|
|
|
|
|
static size_t write_to_string(void* contents, size_t size, size_t nmemb, void* userp);
|
|
|
|
|
static void add_new_handlers_to_multi();
|
|
|
|
|
static void download_thread_update();
|
|
|
|
|
#else
|
2025-10-09 17:58:12 +03:00
|
|
|
static void download_progress(emscripten_fetch_t *fetch);
|
2025-10-09 13:04:39 +03:00
|
|
|
static void download_succeeded(emscripten_fetch_t *fetch);
|
|
|
|
|
static void download_failed(emscripten_fetch_t *fetch);
|
|
|
|
|
#endif
|
2025-10-01 17:54:23 +03:00
|
|
|
};
|