From 26b5700957e9061fecf8329609254fb8176fc893 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Mon, 5 Sep 2022 10:38:47 -0600 Subject: [PATCH] filearchive: extend API --- libs/pbd/file_archive.cc | 49 +++++++++++++++++++++++++++++++++++++ libs/pbd/pbd/file_archive.h | 4 +++ 2 files changed, 53 insertions(+) diff --git a/libs/pbd/file_archive.cc b/libs/pbd/file_archive.cc index 410426fc97..2fa4c6db24 100644 --- a/libs/pbd/file_archive.cc +++ b/libs/pbd/file_archive.cc @@ -178,6 +178,55 @@ FileArchive::~FileArchive () } } +std::string +FileArchive::fetch (const std::string & url, const std::string & destdir) const +{ + std::string pwd (Glib::get_current_dir ()); + + if (g_chdir (destdir.c_str ())) { + fprintf (stderr, "Archive: cannot chdir to '%s'\n", destdir.c_str ()); + return std::string(); + } + + CURL* curl = curl_easy_init (); + + if (!curl) { + return std::string (); + } + + curl_easy_setopt (curl, CURLOPT_URL, url); + curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L); + CURLcode res = curl_easy_perform (curl); + curl_easy_cleanup (curl); + + g_chdir (pwd.c_str()); + if (res != CURLE_OK) { + return std::string(); + } + + return Glib::build_filename (destdir, Glib::path_get_basename (url)); +} + + +int +FileArchive::make_local (const std::string& destdir) +{ + if (!_req.is_remote()) { + return 0; + } + + std::string downloaded = fetch (_req.url, destdir); + + if (downloaded.empty()) { + return -1; + } + + _req.url = strdup (downloaded.c_str()); + _req.mp.progress = 0; + + return 0; +} + int FileArchive::inflate (const std::string& destdir) { diff --git a/libs/pbd/pbd/file_archive.h b/libs/pbd/pbd/file_archive.h index 7244a4e16e..45a954377e 100644 --- a/libs/pbd/pbd/file_archive.h +++ b/libs/pbd/pbd/file_archive.h @@ -38,6 +38,8 @@ class LIBPBD_API FileArchive int inflate (const std::string& destdir); std::vector contents (); + int make_local (const std::string& destdir); + std::string next_file_name (); int extract_current_file (const std::string& destpath); @@ -158,6 +160,8 @@ class LIBPBD_API FileArchive struct archive_entry* _current_entry; struct archive* _archive; + + std::string fetch (const std::string & url, const std::string& destdir) const; }; } /* namespace */