64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
#include "local_uploader.h"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
|
|
#include "utils/logger.h"
|
|
|
|
namespace rk3588 {
|
|
|
|
bool LocalUploader::Init(const SimpleJson& config) {
|
|
base_path_ = config.ValueOr<std::string>("path", "/tmp/alarms");
|
|
url_prefix_ = config.ValueOr<std::string>("url_prefix", "file://");
|
|
|
|
try {
|
|
std::filesystem::create_directories(base_path_);
|
|
} catch (const std::exception& e) {
|
|
LogError(std::string("[LocalUploader] failed to create directory: ") + e.what());
|
|
return false;
|
|
}
|
|
|
|
LogInfo("[LocalUploader] initialized, path=" + base_path_);
|
|
return true;
|
|
}
|
|
|
|
UploadResult LocalUploader::Upload(const std::string& key,
|
|
const uint8_t* data,
|
|
size_t size,
|
|
const std::string& /*content_type*/) {
|
|
UploadResult result;
|
|
|
|
std::string full_path = base_path_ + "/" + key;
|
|
|
|
// Create parent directories if needed
|
|
try {
|
|
std::filesystem::path file_path(full_path);
|
|
if (file_path.has_parent_path()) {
|
|
std::filesystem::create_directories(file_path.parent_path());
|
|
}
|
|
} catch (const std::exception& e) {
|
|
result.error = std::string("failed to create directory: ") + e.what();
|
|
return result;
|
|
}
|
|
|
|
std::ofstream file(full_path, std::ios::binary);
|
|
if (!file) {
|
|
result.error = "failed to open file: " + full_path;
|
|
return result;
|
|
}
|
|
|
|
file.write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));
|
|
file.close();
|
|
|
|
if (!file) {
|
|
result.error = "failed to write file: " + full_path;
|
|
return result;
|
|
}
|
|
|
|
result.success = true;
|
|
result.url = url_prefix_ + full_path;
|
|
return result;
|
|
}
|
|
|
|
} // namespace rk3588
|