Add new Lua function to save base64-encoded files to disk

This commit is contained in:
Alexander Bock
2025-06-11 15:39:27 +02:00
parent 4bc2fdb83c
commit eb8f621390
3 changed files with 20 additions and 1 deletions
+1
View File
@@ -1727,6 +1727,7 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() {
codegen::lua::RemoveTag,
codegen::lua::DownloadFile,
codegen::lua::CreateSingleColorImage,
codegen::lua::SaveBase64File,
codegen::lua::IsMaster,
codegen::lua::Version,
codegen::lua::ReadCSVFile,
+18
View File
@@ -36,6 +36,7 @@
#include <ghoul/glm.h>
#include <ghoul/io/texture/texturewriter.h>
#include <ghoul/lua/lua_helper.h>
#include <ghoul/misc/base64.h>
#include <ghoul/misc/csvreader.h>
#include <ghoul/misc/dictionary.h>
#include <ghoul/opengl/texture.h>
@@ -198,6 +199,23 @@ namespace {
return fileName;
}
/**
* This function takes a base64 encoded data string, decodes it and saves the resulting
* data to the provided filepath.
*
* \param filePath The location where the data will be saved. Any file that already exists
* in that location will be overwritten
* \param base64Data The base64 encoded data that should be saved to the provided file
*/
[[codegen::luawrap]] void saveBase64File(std::filesystem::path filepath,
std::string base64Data)
{
std::vector<uint8_t> data = ghoul::decodeBase64(base64Data);
std::ofstream file = std::ofstream(filepath, std::ofstream::binary);
file.write(reinterpret_cast<char*>(data.data()), data.size());
}
/**
* Returns whether the current OpenSpace instance is the master node of a cluster
* configuration. If this instance is not part of a cluster, this function also returns