Skip to content

Commit

Permalink
🚨Fix : use httplib and remove curl
Browse files Browse the repository at this point in the history
  • Loading branch information
smallboyc committed Jul 24, 2024
1 parent 33b2a5f commit 1ce8e79
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 93 deletions.
15 changes: 3 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ include(FetchContent)
# Make sure we link the static version
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build static library" FORCE)
set(BULD_STATIC_LIBS ON CACHE BOOL "" FORCE)
set(CURL_STATICLIB ON CACHE BOOL "" FORCE)
set(CURL_ZLIB OFF CACHE BOOL "" FORCE)

# JSON
FetchContent_Declare(
Expand All @@ -42,12 +40,6 @@ FetchContent_Declare(
GIT_TAG v3.11.3
)

# CURL
FetchContent_Declare(
curl
URL https://curl.se/download/curl-7.80.0.tar.gz
)

# MINIZ
# Configurer FetchContent pour miniz
FetchContent_Declare(
Expand All @@ -59,20 +51,19 @@ FetchContent_Declare(
# Get and build JSON
FetchContent_MakeAvailable(json)

# Get and build CURL
FetchContent_MakeAvailable(curl)

FetchContent_MakeAvailable(miniz)

# # Grab all the source files
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json CURL::libcurl miniz)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json miniz)

add_subdirectory(Lab/Cool/lib/stringify/lib/fmt)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)
add_subdirectory(Lab/lib/cpp-httplib)
target_link_libraries(${PROJECT_NAME} PRIVATE httplib)
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE Lab/Cool/lib/expected/include)

# ---------------------
Expand Down
115 changes: 56 additions & 59 deletions src/download.cpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,76 @@
#include "download.hpp"
#include "release.hpp"
#include "utils.hpp"
#include <cstdlib>
#include <curl/curl.h>
#include <filesystem>
#include <iostream>
#include <vector>
#include "httplib.h"
#include "release.hpp"
#include "utils.hpp"

namespace fs = std::filesystem;

// download file from url
auto download_zip(nlohmann::basic_json<> const &release)
-> tl::expected<std::vector<char>, std::string> {
auto const url = get_coollab_download_url(release);
std::cout << url << std::endl;
CURL *curl;
curl = curl_easy_init();
std::vector<char> out_data;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out_data);
CURLcode res;
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
return out_data;
else {
std::cerr << "Failed to download zip file from URL: " << url << std::endl;
std::cerr << curl_easy_strerror(res) << std::endl;
return tl::make_unexpected("error"); // TODO : set a correct error message
auto download_zip(nlohmann::json const& release) -> tl::expected<std::vector<char>, std::string>
{
std::string url = "https://github.com";
httplib::Client cli(url);
auto const path = get_coollab_download_url(release);

cli.set_follow_location(true); // Allow the client to follow redirects

auto res = cli.Get(path);
if (res && res->status == 200)
{
// Copy the response body into a vector
std::vector<char> zip_data(res->body.begin(), res->body.end());
return zip_data;
}
} else {
std::cerr << "Failed to initialize curl." << std::endl;
return tl::make_unexpected("error"); // TODO : set a correct error message
}
return tl::unexpected("Failed to download the file. Status code: " + std::to_string(res->status));
}

auto install_macos_dependencies_if_necessary() -> void {
// Check if Homebrew is already installed
if (std::system("command -v brew >/dev/null 2>&1"))
install_homebrew();
else
std::cout << "Homebrew is already installed." << std::endl;
auto install_macos_dependencies_if_necessary() -> void
{
// Check if Homebrew is already installed
if (std::system("command -v brew >/dev/null 2>&1"))
install_homebrew();
else
std::cout << "Homebrew is already installed." << std::endl;

// Check if FFmpeg is already installed
if (std::system("command -v ffmpeg >/dev/null 2>&1"))
install_ffmpeg();
else
std::cout << "FFmpeg is already installed." << std::endl;
// Check if FFmpeg is already installed
if (std::system("command -v ffmpeg >/dev/null 2>&1"))
install_ffmpeg();
else
std::cout << "FFmpeg is already installed." << std::endl;
}

auto install_homebrew() -> void {
std::cout << "Installing Homebrew..." << std::endl;
int result = std::system(
"/bin/bash -c \"$(curl -fsSL "
"https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"");
if (result != 0) {
std::cerr << "Homebrew installation failed!" << std::endl;
exit(result);
}
std::cout << "Homebrew successfully installed." << std::endl;
auto install_homebrew() -> void
{
std::cout << "Installing Homebrew..." << std::endl;
int result = std::system(
"/bin/bash -c \"$(curl -fsSL "
"https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
);
if (result != 0)
{
std::cerr << "Homebrew installation failed!" << std::endl;
exit(result);
}
std::cout << "Homebrew successfully installed." << std::endl;
}

auto install_ffmpeg() -> void {
std::cout << "Installing FFmpeg via Homebrew..." << std::endl;
int result = std::system("brew install ffmpeg");
if (result != 0) {
std::cerr << "FFmpeg installation failed!" << std::endl;
exit(result);
}
std::cout << "FFmpeg successfully installed." << std::endl;
auto install_ffmpeg() -> void
{
std::cout << "Installing FFmpeg via Homebrew..." << std::endl;
int result = std::system("brew install ffmpeg");
if (result != 0)
{
std::cerr << "FFmpeg installation failed!" << std::endl;
exit(result);
}
std::cout << "FFmpeg successfully installed." << std::endl;
}

auto coollab_version_is_installed(std::string_view const &version) -> bool {
return fs::exists(get_PATH() + std::string(version));
auto coollab_version_is_installed(std::string_view const& version) -> bool
{
return fs::exists(get_PATH() + std::string(version));
}
40 changes: 18 additions & 22 deletions src/release.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
#include <curl/curl.h>
#include <tl/expected.hpp>

#include "release.hpp"
#include <tl/expected.hpp>
#include "fmt/core.h"
#include "httplib.h"
#include "utils.hpp"

auto get_release(std::string_view const& version) -> tl::expected<nlohmann::basic_json<>, std::string>
auto get_release(std::string_view const& version) -> tl::expected<nlohmann::json, std::string>
{
CURL* curl = curl_easy_init();
if (!curl)
return tl::make_unexpected("Failed to initialize curl");
std::string url = fmt::format("https://api.github.com/repos/CoolLibs/Lab/releases/tags/{}", version);

std::string const url = fmt::format("https://api.github.com/repos/CoolLibs/Lab/releases/tags/{}", version); // TODO(Launcher) Don't hardcode the url to the release
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.68.0");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, string_write_callback);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
httplib::Client cli("https://api.github.com");
cli.set_follow_location(true);

if (res != CURLE_OK)
return tl::make_unexpected(fmt::format("Failed to fetch release info: {}", curl_easy_strerror(res)));
auto res = cli.Get(url.c_str());

if (!res || res->status != 200)
{
return tl::make_unexpected(fmt::format("Failed to fetch release info: {}", res ? res->status : -1));
}

// Parse JSON response
try
{
auto const jsonResponse = json::parse(readBuffer);
auto jsonResponse = nlohmann::json::parse(res->body);
if (!jsonResponse.contains("assets") || jsonResponse["assets"].empty())
{
return tl::make_unexpected("No assets found in the release.");
nlohmann::basic_json<> const& assets = jsonResponse["assets"];
}
nlohmann::json const& assets = jsonResponse["assets"];
return assets;
}
catch (json::parse_error const& e)
catch (nlohmann::json::parse_error const& e)
{
return tl::make_unexpected(fmt::format("JSON parse error: {}", e.what()));
}
Expand All @@ -42,6 +37,7 @@ auto get_release(std::string_view const& version) -> tl::expected<nlohmann::basi
return tl::make_unexpected(fmt::format("Error: {}", e.what()));
}
}

auto get_coollab_download_url(nlohmann::basic_json<> const& release) -> std::string
{
auto os_path = get_OS();
Expand Down

0 comments on commit 1ce8e79

Please sign in to comment.