safesight-edge/plugins/alarm/uploaders/local_uploader.cpp
2025-12-26 16:58:18 +08:00

63 lines
1.8 KiB
C++

#include "local_uploader.h"
#include <filesystem>
#include <fstream>
#include <iostream>
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) {
std::cerr << "[LocalUploader] failed to create directory: " << e.what() << "\n";
return false;
}
std::cout << "[LocalUploader] initialized, path=" << base_path_ << "\n";
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