613 lines
17 KiB
C++
613 lines
17 KiB
C++
/*
|
|
* Copyright (c) 2026 Robin E. R. Davies
|
|
* All rights reserved.
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include "ss.hpp"
|
|
#include "Tone3000Download.hpp"
|
|
#include "TemporaryFile.hpp"
|
|
#include <array>
|
|
#include <ctime>
|
|
#include "util.hpp"
|
|
|
|
using namespace pipedal;
|
|
using namespace pipedal::tone3000;
|
|
namespace fs = std::filesystem;
|
|
|
|
JSON_MAP_BEGIN(Tone3000Model)
|
|
JSON_MAP_REFERENCE(Tone3000Model, id)
|
|
JSON_MAP_REFERENCE(Tone3000Model, name)
|
|
JSON_MAP_REFERENCE(Tone3000Model, model_url)
|
|
JSON_MAP_REFERENCE(Tone3000Model, created_at)
|
|
JSON_MAP_REFERENCE(Tone3000Model, size)
|
|
JSON_MAP_REFERENCE(Tone3000Model, user_id)
|
|
JSON_MAP_END()
|
|
|
|
JSON_MAP_BEGIN(Tone3000User)
|
|
JSON_MAP_REFERENCE(Tone3000User, id)
|
|
JSON_MAP_REFERENCE(Tone3000User, username)
|
|
JSON_MAP_REFERENCE(Tone3000User, avatar_url)
|
|
JSON_MAP_REFERENCE(Tone3000User, url)
|
|
JSON_MAP_END()
|
|
|
|
JSON_MAP_BEGIN(Tone3000Download)
|
|
JSON_MAP_REFERENCE(Tone3000Download, id)
|
|
JSON_MAP_REFERENCE(Tone3000Download, user_id)
|
|
JSON_MAP_REFERENCE(Tone3000Download, title)
|
|
JSON_MAP_REFERENCE(Tone3000Download, description)
|
|
JSON_MAP_REFERENCE(Tone3000Download, created_at)
|
|
JSON_MAP_REFERENCE(Tone3000Download, updated_at)
|
|
JSON_MAP_REFERENCE(Tone3000Download, platform)
|
|
JSON_MAP_REFERENCE(Tone3000Download, gear)
|
|
JSON_MAP_REFERENCE(Tone3000Download, images)
|
|
JSON_MAP_REFERENCE(Tone3000Download, is_public)
|
|
JSON_MAP_REFERENCE(Tone3000Download, links)
|
|
JSON_MAP_REFERENCE(Tone3000Download, model_count)
|
|
JSON_MAP_REFERENCE(Tone3000Download, favorites_count)
|
|
JSON_MAP_REFERENCE(Tone3000Download, license)
|
|
JSON_MAP_REFERENCE(Tone3000Download, sizes)
|
|
JSON_MAP_REFERENCE(Tone3000Download, user)
|
|
JSON_MAP_REFERENCE(Tone3000Download, models)
|
|
JSON_MAP_END()
|
|
|
|
JSON_MAP_BEGIN(Tone3000DownloadResult)
|
|
JSON_MAP_REFERENCE(Tone3000DownloadResult, success)
|
|
JSON_MAP_REFERENCE(Tone3000DownloadResult, errorMessage)
|
|
JSON_MAP_END()
|
|
|
|
static const std::filesystem::path WEB_TEMP_DIR{"/var/pipedal/web_temp"};
|
|
|
|
static std::string shellEscape(const std::string &input)
|
|
{
|
|
// Escape string for safe use in shell commands by using single quotes
|
|
// and escaping any single quotes in the input
|
|
std::string result = "'";
|
|
for (char c : input)
|
|
{
|
|
if (c == '\'')
|
|
{
|
|
// End quote, add escaped single quote, start quote again
|
|
result += "'\\''";
|
|
}
|
|
else
|
|
{
|
|
result += c;
|
|
}
|
|
}
|
|
result += "'";
|
|
return result;
|
|
}
|
|
|
|
static std::string getCurlErrorFromExitCode(int exitCode)
|
|
{
|
|
|
|
switch (exitCode)
|
|
{
|
|
case 3 * 256: // malformed URL
|
|
return "Invalid URL.";
|
|
case 6 * 256: // could not resolve hostname
|
|
return "Could not resolve hostname. Check that the PiPedal server is connected to the Internet.";
|
|
case 7 * 256: // failed to connect to host
|
|
return "Failed to connect to host.";
|
|
case 22 * 256: // HTTP error
|
|
return "HTTP error downloading file.";
|
|
case 28 * 256: // timeout
|
|
return "Download timeout.";
|
|
case 63 * 256: // file too large
|
|
return "File too large.";
|
|
default:
|
|
return SS("Failed to download file (exit code: " << (exitCode / 256) << ")");
|
|
}
|
|
}
|
|
static void downloadFile(const std::string &url, const fs::path &path)
|
|
{
|
|
// Create temporary file for HTTP status code
|
|
TemporaryFile httpCodeFile{WEB_TEMP_DIR};
|
|
fs::path httpCodePath = httpCodeFile.Path();
|
|
|
|
// Build curl command with error detection and proper escaping
|
|
std::ostringstream os;
|
|
os << "/usr/bin/curl -f -s -S -L --max-time 300 --max-filesize 104857600";
|
|
os << " -w '%{http_code}' -o ";
|
|
os << shellEscape(path.string());
|
|
os << " ";
|
|
os << shellEscape(url);
|
|
os << " > ";
|
|
os << shellEscape(httpCodePath.string());
|
|
|
|
std::string command = os.str();
|
|
|
|
// Execute curl command
|
|
int exitCode = std::system(command.c_str());
|
|
|
|
// Read HTTP status code
|
|
int httpCode = 0;
|
|
if (fs::exists(httpCodePath))
|
|
{
|
|
std::ifstream httpCodeStream(httpCodePath);
|
|
std::string httpCodeStr;
|
|
std::getline(httpCodeStream, httpCodeStr);
|
|
try
|
|
{
|
|
httpCode = std::stoi(httpCodeStr);
|
|
}
|
|
catch (...)
|
|
{
|
|
// If parsing fails, keep httpCode as 0
|
|
}
|
|
}
|
|
|
|
if (exitCode != 0)
|
|
{
|
|
if (exitCode == 22 * 256) // HTTP error
|
|
{
|
|
throw std::runtime_error(
|
|
SS("Server was unabled to download the file. Http error: " << httpCode << " url: " << url));
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error(
|
|
SS("Server was unable to download the file. "
|
|
<< getCurlErrorFromExitCode(exitCode)
|
|
<< " " << "url: " << url));
|
|
}
|
|
}
|
|
|
|
// Verify file was downloaded
|
|
if (!fs::exists(path) || fs::file_size(path) == 0)
|
|
{
|
|
throw std::runtime_error("Downloaded file is empty or missing");
|
|
}
|
|
}
|
|
|
|
|
|
static std::string mdSanitize(const std::string &str, const std::string &illegalCharacters = "")
|
|
{
|
|
std::ostringstream os;
|
|
for (char c : str)
|
|
{
|
|
if ((unsigned char)c < 0x20)
|
|
continue;
|
|
if (c == '<')
|
|
{
|
|
os << "<";
|
|
continue;
|
|
}
|
|
auto npos = illegalCharacters.find_first_of(c);
|
|
if (npos != std::string::npos)
|
|
{
|
|
continue;
|
|
}
|
|
os << c;
|
|
}
|
|
return os.str();
|
|
}
|
|
|
|
|
|
static std::string mdDate(const tone3000_time_point &date_)
|
|
{
|
|
std::ostringstream os;
|
|
// C++ doesn't handle timezones. Just zap the timezone, and replace it with "Z"
|
|
std::string date = date_;
|
|
|
|
// Remove timezone offset if present and replace with Z
|
|
size_t plusPos = date.find('+');
|
|
if (plusPos != std::string::npos)
|
|
{
|
|
date = date.substr(0, plusPos) + "Z";
|
|
}
|
|
// Parse ISO 8601 date string into tm
|
|
std::tm tm = {};
|
|
std::istringstream ss(date);
|
|
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
|
|
if (ss.fail())
|
|
{
|
|
os << date;
|
|
return os.str();
|
|
}
|
|
|
|
os << std::put_time(&tm, "%x");
|
|
return os.str();
|
|
}
|
|
|
|
static std::string mdEnum(const std::string &value, const std::map<std::string, std::string> &enumValues)
|
|
{
|
|
auto ff = enumValues.find(value);
|
|
if (ff == enumValues.end())
|
|
{
|
|
return value;
|
|
}
|
|
return ff->second;
|
|
}
|
|
|
|
static std::string mdEnumList(
|
|
const std::vector<std::string> &values,
|
|
const std::map<std::string, std::string> &enumValues)
|
|
{
|
|
if (values.size() == 1)
|
|
{
|
|
return mdEnum(values[0], enumValues);
|
|
}
|
|
std::ostringstream os;
|
|
os << '[';
|
|
bool first = true;
|
|
for (const auto &value : values)
|
|
{
|
|
if (first)
|
|
{
|
|
first = false;
|
|
}
|
|
else
|
|
{
|
|
os << ", ";
|
|
}
|
|
os << mdEnum(value, enumValues);
|
|
}
|
|
os << ']';
|
|
return os.str();
|
|
}
|
|
|
|
static std::map<std::string, std::string> sizeEnumValues =
|
|
{
|
|
{"standard", "Standard"},
|
|
{"lite", "Lite"},
|
|
{"feather", "Feather"},
|
|
{"nano", "Nano"},
|
|
{"custom", "Custom"}};
|
|
static std::string mdSizes(const std::vector<std::string> &sizes)
|
|
{
|
|
return mdEnumList(sizes, sizeEnumValues);
|
|
}
|
|
|
|
static std::map<std::string, std::string> gearEnumValues =
|
|
{
|
|
{"amp", "Amp only"},
|
|
{"full-rig", "Full rig"},
|
|
{"pedal", "Pedal"},
|
|
{"outboard", "Outboard"},
|
|
{"ir", "I/R"}};
|
|
|
|
static std::string mdGear(const std::string &gear)
|
|
{
|
|
return mdEnum(gear, gearEnumValues);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
enum class LicenseFlags
|
|
{
|
|
None = 0,
|
|
Cc = 1,
|
|
By = 2,
|
|
CcBy = 3, // = Cc | By
|
|
Sa = 4,
|
|
Nc = 8,
|
|
Nd = 16,
|
|
Cc0 = 32,
|
|
};
|
|
|
|
static bool operator&(LicenseFlags v1, LicenseFlags v2)
|
|
{
|
|
return (((int)v1) & ((int)v2)) != 0;
|
|
}
|
|
static LicenseFlags operator|(LicenseFlags v1, LicenseFlags v2)
|
|
{
|
|
return (LicenseFlags)(((int)(v1)) | ((int)(v2)));
|
|
}
|
|
|
|
struct LicenseInfo
|
|
{
|
|
std::string key;
|
|
std::string displayName;
|
|
std::string url;
|
|
LicenseFlags licenseFlags;
|
|
};
|
|
}
|
|
|
|
static std::vector<LicenseInfo> licenses{
|
|
{
|
|
.key = "t3k",
|
|
.displayName = "t3k",
|
|
.url = "",
|
|
.licenseFlags = LicenseFlags::None
|
|
},
|
|
{
|
|
.key = "cc-by",
|
|
.displayName = "CC BY 4.0",
|
|
.url = "https://creativecommons.org/licenses/by/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy
|
|
},
|
|
{
|
|
.key = "cc-by-sa",
|
|
.displayName = "CC BY-SA 4.0",
|
|
.url = "https://creativecommons.org/licenses/by-sa/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy | LicenseFlags::Sa
|
|
},
|
|
{
|
|
.key = "cc-by-nc",
|
|
.displayName = "CC BY-NC 4.0",
|
|
.url = "https://creativecommons.org/licenses/by-nc/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy | LicenseFlags::Nc
|
|
},
|
|
{
|
|
.key = "cc-by-nc-sa",
|
|
.displayName = "CC BY-NC-SA 4.0",
|
|
.url = "https://creativecommons.org/licenses/by-nc-sa/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy | LicenseFlags::Nc | LicenseFlags::Sa
|
|
},
|
|
{
|
|
.key = "cc-by-nd",
|
|
.displayName = "CC BY-ND",
|
|
.url = "https://creativecommons.org/licenses/by-nd/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy | LicenseFlags::Nd
|
|
},
|
|
{
|
|
.key = "cc-by-nc-nd",
|
|
.displayName = "CC BY-NC-ND",
|
|
.url = "https://creativecommons.org/licenses/by-nc-nd/4.0/",
|
|
.licenseFlags = LicenseFlags::CcBy | LicenseFlags::Nc | LicenseFlags::Nd
|
|
},
|
|
{
|
|
.key = "cco",
|
|
.displayName = "CC0",
|
|
.url = "https://creativecommons.org/publicdomain/zero/1.0/",
|
|
.licenseFlags = LicenseFlags::Cc | LicenseFlags::Cc0,
|
|
|
|
},
|
|
};
|
|
|
|
static std::map<std::string, LicenseInfo> makeLicenseEnum()
|
|
{
|
|
std::map<std::string, LicenseInfo> result;
|
|
for (const auto &license : licenses)
|
|
{
|
|
result[license.key] = license;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static std::map<std::string, LicenseInfo> licenseEnum = makeLicenseEnum();
|
|
|
|
static std::string mdLicenseIcon(LicenseFlags licenseFlag)
|
|
{
|
|
std::ostringstream os;
|
|
std::string url;
|
|
|
|
switch (licenseFlag)
|
|
{
|
|
case LicenseFlags::Cc:
|
|
url = "img/cc.svg";
|
|
break;
|
|
case LicenseFlags::By:
|
|
url = "img/by.svg";
|
|
break;
|
|
case LicenseFlags::Sa:
|
|
url = "img/sa.svg";
|
|
break;
|
|
case LicenseFlags::Nc:
|
|
url = "img/nc.svg";
|
|
break;
|
|
case LicenseFlags::Nd:
|
|
url = "img/nd.svg";
|
|
break;
|
|
case LicenseFlags::Cc0:
|
|
url = "img/cc0.svg";
|
|
break;
|
|
|
|
case LicenseFlags::None:
|
|
throw std::runtime_error("Invalid argument.");
|
|
}
|
|
os << "<img id='cc_img' src='" << url << "'/>";
|
|
return os.str();
|
|
}
|
|
static std::string mdLicense(const std::string &license)
|
|
{
|
|
auto ff = licenseEnum.find(license);
|
|
LicenseInfo licenseInfo;
|
|
if (ff != licenseEnum.end())
|
|
{
|
|
licenseInfo = ff->second;
|
|
}
|
|
else
|
|
{
|
|
licenseInfo.displayName = license;
|
|
}
|
|
|
|
std::ostringstream os;
|
|
|
|
if (licenseInfo.licenseFlags != LicenseFlags::None) {
|
|
for (LicenseFlags licenseFlag: std::vector<LicenseFlags>{LicenseFlags::Cc, LicenseFlags::By, LicenseFlags::Cc0, LicenseFlags::Nc, LicenseFlags::Sa, LicenseFlags::Nd})
|
|
{
|
|
if (licenseInfo.licenseFlags & licenseFlag)
|
|
{
|
|
os << mdLicenseIcon(licenseFlag);
|
|
}
|
|
}
|
|
os << " ";
|
|
}
|
|
|
|
if (licenseInfo.url != "")
|
|
{
|
|
os << "<a target='_blank' rel='noopener noreferrer' href='"
|
|
<< licenseInfo.url
|
|
<< "'>" << mdSanitize(licenseInfo.displayName) << "</a>";
|
|
}
|
|
else
|
|
{
|
|
os << mdSanitize(licenseInfo.displayName);
|
|
}
|
|
return os.str();
|
|
}
|
|
static std::map<std::string, std::string> platformEnumValues =
|
|
{
|
|
{"nam", "NAM"},
|
|
{"ir", "I/R"},
|
|
{"aida-x", "Aida X"},
|
|
{"aa-snapshot", "aa-snapshot"},
|
|
{"proteus", "Proteus"}};
|
|
static std::string mdPlatform(const std::string &platform)
|
|
{
|
|
return mdEnum(platform, platformEnumValues);
|
|
}
|
|
static std::string mdUser(const tone3000::Tone3000User &user)
|
|
{
|
|
// clang-format off
|
|
std::ostringstream os;
|
|
os <<
|
|
"<a target='_blank' rel='noopener noreferrer' href='"
|
|
<< mdSanitize(user.url(),"'")
|
|
<< "'>" << mdSanitize(user.username()) << "</a>";
|
|
return os.str();
|
|
// clang-format on
|
|
}
|
|
|
|
static void mdEscapeString(std::ostream &f, const std::string &str)
|
|
{
|
|
size_t ix = 0;
|
|
while (ix < str.size())
|
|
{
|
|
char c = str[ix++];
|
|
if (c == '\n')
|
|
{
|
|
if (ix < str.size() && str[ix] == '\n')
|
|
{
|
|
f << "\n\n";
|
|
}
|
|
else
|
|
{
|
|
f << " \n"; // a <br> in md.
|
|
}
|
|
}
|
|
else
|
|
{
|
|
f << c;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void mdLink(std::ostream &f, const std::string &label, const std::string &url)
|
|
{
|
|
f << "[" << mdSanitize(label, "]") << "](" << mdSanitize(url, ")") << ")";
|
|
}
|
|
|
|
static void mdImage(std::ostream &f, const std::string url)
|
|
{
|
|
f << "![" << mdSanitize("Plugin Thumbname", "]") << "](" << mdSanitize(url, "]") << "#tone3000_thumbnail" << ")";
|
|
f << "\n\n";
|
|
}
|
|
static void writeReadme(const fs::path &path, const Tone3000Download &download)
|
|
{
|
|
std::ofstream of{path};
|
|
if (!of.is_open())
|
|
{
|
|
throw std::runtime_error(SS("Unable to create file " << path));
|
|
}
|
|
std::string imageLink;
|
|
if (download.images().size() > 0)
|
|
{
|
|
imageLink = download.images()[0];
|
|
}
|
|
|
|
// clang-format off
|
|
of <<
|
|
|
|
"<div id='tone3000_float' >\n"
|
|
<< " <img id='tone3000_thumbnail' src='"
|
|
<< imageLink << "' alt='' />\n"
|
|
<< " <div id='tone3000_float_content' >\n"
|
|
<< " <h3 id='tone3000_title'>" << mdSanitize(download.title()) << "</h3>\n"
|
|
<< " <div>\n"
|
|
<< " <p id='tone3000_subtitle'>\n"
|
|
<< mdDate(download.updated_at())
|
|
<< " "
|
|
<< mdUser(download.user())
|
|
<< " <br/>\n"
|
|
<< " " << mdSizes(download.sizes()) << ", "
|
|
<< mdSizes(download.sizes()) << ", "
|
|
<< mdGear(download.gear()) << ", "
|
|
<< mdPlatform(download.platform())
|
|
<< "<br/>\n"
|
|
<< " License: xxx" << mdLicense(download.license()) << "\n"
|
|
<< " </p>\n"
|
|
<< " </div>\n"
|
|
<< " </div>\n"
|
|
<< "</div>\n";
|
|
// clang-format on
|
|
of << "\n\n";
|
|
mdEscapeString(of, download.description());
|
|
|
|
if (download.links().size() > 0)
|
|
{
|
|
of << std::endl;
|
|
of << "## Links" << std::endl
|
|
<< std::endl;
|
|
for (auto &link : download.links())
|
|
{
|
|
of << "- " << "[" << link << "](" << link << ")" << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
void pipedal::tone3000::DownloadTone3000Bundle(
|
|
const std::filesystem::path &destinationFolder,
|
|
const std::string &downloadUrl)
|
|
{
|
|
TemporaryFile downloadTemporaryFile{WEB_TEMP_DIR};
|
|
fs::path downloadPath = downloadTemporaryFile.Path();
|
|
|
|
downloadFile(downloadUrl, downloadPath);
|
|
|
|
std::ifstream f{downloadPath};
|
|
if (!f.is_open())
|
|
{
|
|
throw std::runtime_error(SS("Can't open file " << downloadPath));
|
|
}
|
|
json_reader reader(f);
|
|
|
|
Tone3000Download tone3000Download;
|
|
reader.read(&tone3000Download);
|
|
|
|
if (tone3000Download.platform() != "nam")
|
|
{
|
|
throw std::runtime_error(SS("Unexpected file format. (platform=\"" << tone3000Download.platform() << "\")"));
|
|
}
|
|
fs::path bundlePath = destinationFolder / stringToSafeFilename(tone3000Download.title());
|
|
|
|
fs::create_directories(bundlePath);
|
|
|
|
for (auto &model : tone3000Download.models())
|
|
{
|
|
fs::path modelPath = bundlePath / SS(stringToSafeFilename(model.name()) << ".nam");
|
|
downloadFile(model.model_url(), modelPath);
|
|
}
|
|
fs::path readmePath = bundlePath / "README.md";
|
|
writeReadme(readmePath, tone3000Download);
|
|
}
|
|
|
|
Tone3000DownloadResult::Tone3000DownloadResult()
|
|
: success_(true)
|
|
{
|
|
}
|
|
Tone3000DownloadResult::Tone3000DownloadResult(const std::exception &e)
|
|
: success_(false), errorMessage_(e.what())
|
|
{
|
|
}
|