104 if (!fs::exists(draftPath))
111 std::ifstream f(draftPath);
115 auto now = std::chrono::system_clock::now();
116 std::time_t now_c = std::chrono::system_clock::to_time_t(now);
117 std::stringstream ss;
118 ss << std::put_time(std::gmtime(&now_c),
"%Y-%m-%dT%H:%M:%SZ");
120 if (!j.contains(
"meta"))
121 j[
"meta"] =
nlohmann::json::object();
122 j[
"meta"][
"created"] = ss.str();
123 j[
"meta"][
"version"] = std::to_string(std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count());
125 std::ofstream out(draftPath, std::ios::trunc);
128 }
catch (
const std::exception& e) {
129 VLOG(1) <<
"Failed to inject metadata into draft: " << e.what();
133 if (fs::exists(mapFilePath)) {
134 fs::path versionDir = fs::path(mapFilePath).parent_path() /
"versions";
135 if (!fs::exists(versionDir)) {
136 fs::create_directories(versionDir);
139 auto now = std::chrono::system_clock::now();
140 auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
141 std::string filename = fs::path(mapFilePath).filename().string();
142 std::string backupPath = versionDir / (filename +
"." + std::to_string(timestamp));
144 fs::copy_file(mapFilePath, backupPath, fs::copy_options::overwrite_existing);
148 std::vector<fs::path> versions;
149 for (
const auto& entry : fs::directory_iterator(versionDir)) {
150 if (entry.path().filename().string().starts_with(filename +
".")) {
151 versions.push_back(entry.path());
154 if (versions.size() > 50) {
155 std::sort(versions.begin(), versions.end(), [](
const fs::path& a,
const fs::path& b) {
156 return fs::last_write_time(a) < fs::last_write_time(b);
158 for (size_t i = 0; i < versions.size() - 50; ++i) {
159 fs::remove(versions[i]);
167 fs::remove(draftPath);
181 fs::path versionDir = fs::path(mapFilePath).parent_path() /
"versions";
182 std::string baseName = fs::path(mapFilePath).filename().string();
184 if (!fs::exists(versionDir))
187 for (
const auto& entry : fs::directory_iterator(versionDir)) {
188 if (entry.path().filename().string().starts_with(baseName +
".")) {
192 v
.id = entry.path().extension().string().substr(1);
199 if (j.contains(
"meta")) {
200 if (j[
"meta"].contains(
"comment"))
202 if (j[
"meta"].contains(
"created"))
203 v
.date = j[
"meta"][
"created"];
209 if (v
.date.empty()) {
211 long long ts = std::stoll(v
.id);
212 std::time_t t =
static_cast<std::time_t>(ts);
213 std::stringstream ss;
214 ss << std::put_time(std::gmtime(&t),
"%Y-%m-%dT%H:%M:%SZ");
221 history.push_back(v);
228 return std::stoll(a
.id) > std::stoll(b
.id);
239 fs::path versionDir = fs::path(mapFilePath).parent_path() /
"versions";
240 std::string baseName = fs::path(mapFilePath).filename().string();
241 fs::path backupPath = versionDir / (baseName +
"." + versionId);
243 if (!fs::exists(backupPath)) {
244 throw std::runtime_error(
"Version not found: " + versionId);
249 std::ifstream f(backupPath);
252 }
catch (
const std::exception& e) {
253 throw std::runtime_error(std::string(
"Cannot rollback: Version is invalid against current schema: ") + e.what());
257 fs::copy_file(backupPath, mapFilePath, fs::copy_options::overwrite_existing);