10 #include <mrpt/system/filesystem.h> 16 using namespace mvsim;
19 :
mrpt::system::COutputLogger(
"mvsim::RemoteResourcesManager")
25 return 0 == ::strncmp(url.c_str(),
"http://", strlen(
"http://")) ||
26 0 == ::strncmp(url.c_str(),
"https://", strlen(
"https://"));
35 else if (uri.substr(0, 7) ==
"file://")
47 std::string local_directory;
49 local_directory = std::getenv(
"APPDATA");
51 local_directory = std::getenv(
"HOME");
52 local_directory +=
"/.cache/";
55 local_directory +=
"mvsim-storage/";
57 return local_directory;
60 std::tuple<bool, std::string, std::string>
63 auto pos = uri.find(
".zip/");
65 if (pos == std::string::npos)
return {
false, uri,
""};
67 const auto zipUri = uri.substr(0, pos + 4);
68 const auto internalUri = uri.substr(pos + 5);
70 return {
true, zipUri, internalUri};
75 using namespace std::string_literals;
78 std::string cacheUsageStats;
80 if (!mrpt::system::directoryExists(localDir))
83 mrpt::system::createDirectory(localDir);
85 const auto cmd =
"mkdir -p \""s + localDir +
"\""s;
86 if (
int ret = ::system(
cmd.c_str()); ret != 0)
88 MRPT_LOG_ERROR_STREAM(
"Error executing command: " <<
cmd);
91 ASSERT_DIRECTORY_EXISTS_(localDir);
96 if (0 == mrpt::system::executeCommand(
97 mrpt::format(
"du -sh0 \"%s\"", localDir.c_str()),
104 "Using local storage directory: '"s + localDir +
"' (Usage: "s +
105 cacheUsageStats +
")"s);
107 const auto [isZipPkg, zipOrFileURI, internalURI] =
zip_uri_split(uri);
109 MRPT_LOG_DEBUG_STREAM(
110 "Split URI: isZipPkg=" << isZipPkg <<
" zipOrFileURI=" << zipOrFileURI
111 <<
" internalURI=" << internalURI);
113 const auto fileName = mrpt::system::extractFileName(zipOrFileURI) +
"." +
114 mrpt::system::extractFileExtension(zipOrFileURI);
115 const auto localFil = localDir +
fileName;
118 if (!mrpt::system::fileExists(localFil))
120 const auto cmd = mrpt::format(
121 "wget -q -O \"%s\" %s", localFil.c_str(), zipOrFileURI.c_str());
123 MRPT_LOG_INFO_STREAM(
124 "Downloading remote resources from: '" << uri <<
"'");
126 if (
int ret = ::system(
cmd.c_str()); ret != 0)
129 "[mvsim] Error (code=%i) executing the following command " 130 "trying to acquire a remote resource:\n%s",
149 const std::string& localZipFil,
const std::string& internalURI)
151 using namespace std::string_literals;
153 ASSERT_FILE_EXISTS_(localZipFil);
155 const auto filExtension = mrpt::system::extractFileExtension(localZipFil);
156 ASSERT_EQUAL_(filExtension,
"zip");
159 const auto zipOutDir =
160 mrpt::system::fileNameChangeExtension(localZipFil,
"out");
162 if (!mrpt::system::directoryExists(zipOutDir))
164 mrpt::system::createDirectory(zipOutDir);
165 ASSERT_DIRECTORY_EXISTS_(zipOutDir);
167 const std::string
cmd =
168 "unzip -q \""s + localZipFil +
"\" -d \""s + zipOutDir +
"\""s;
170 if (
int ret = ::system(cmd.c_str()); ret != 0)
173 "[mvsim] Error (code=%i) executing the following command " 174 "trying to unzip a remote resource:\n%s",
179 return zipOutDir +
"/"s + internalURI;
static const char * fileName
std::string resolve_path(const std::string &uri)
static bool is_remote(const std::string &uri)
std::string handle_local_zip_package(const std::string &localZipFil, const std::string &internalURI)
static std::string cache_directory()
static std::tuple< bool, std::string, std::string > zip_uri_split(const std::string &uri)
std::string handle_remote_uri(const std::string &uri)