From 3fffaa576d137385f943e8b78f1937c08f4bd8ee Mon Sep 17 00:00:00 2001 From: leedongmyung Date: Fri, 22 Aug 2025 13:05:45 +0900 Subject: [PATCH] add file backup --- lib/oto/commands/file/file.dart | 14 +++++++++++++- lib/oto/data/command_data.dart | 1 + lib/oto/data/command_data.g.dart | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/oto/commands/file/file.dart b/lib/oto/commands/file/file.dart index a9f86f9..466423c 100644 --- a/lib/oto/commands/file/file.dart +++ b/lib/oto/commands/file/file.dart @@ -27,7 +27,19 @@ class FileWrite extends Command { Future execute(DataCommand command) async { var data = DataFileWrite.fromJson(getParam(command)); var file = File(data.path); - if (file.existsSync()) file.deleteSync(); + if (file.existsSync()) { + if (data.backup ?? false) { + var now = DateTime.now(); + var postfix = + '_${now.year}${now.month.toString().padLeft(2, '0')}${now.day.toString().padLeft(2, '0')}${now.hour.toString().padLeft(2, '0')}${now.minute.toString().padLeft(2, '0')}${now.second.toString().padLeft(2, '0')}${now.millisecond}'; + var fileName = path.basenameWithoutExtension(data.path); + var fileExt = path.extension(data.path); + var backupPath = + path.join(path.dirname(data.path), fileName + postfix + fileExt); + await file.copy(backupPath); + } + file.deleteSync(); + } await file.writeAsString(data.content); return complete(0, command); diff --git a/lib/oto/data/command_data.dart b/lib/oto/data/command_data.dart index 26ad931..31c5bab 100644 --- a/lib/oto/data/command_data.dart +++ b/lib/oto/data/command_data.dart @@ -518,6 +518,7 @@ class DataFileWrite extends DataParam { late String path; late String content; + late bool? backup; factory DataFileWrite.fromJson(Map json) => _$DataFileWriteFromJson(json); diff --git a/lib/oto/data/command_data.g.dart b/lib/oto/data/command_data.g.dart index d7411e9..5a984d3 100644 --- a/lib/oto/data/command_data.g.dart +++ b/lib/oto/data/command_data.g.dart @@ -914,7 +914,8 @@ DataFileWrite _$DataFileWriteFromJson(Map json) => ..setExitCode = json['setExitCode'] as String? ..showPrint = json['showPrint'] as bool? ..path = json['path'] as String - ..content = json['content'] as String; + ..content = json['content'] as String + ..backup = json['backup'] as bool?; Map _$DataFileWriteToJson(DataFileWrite instance) => { @@ -925,6 +926,7 @@ Map _$DataFileWriteToJson(DataFileWrite instance) => 'showPrint': instance.showPrint, 'path': instance.path, 'content': instance.content, + 'backup': instance.backup, }; DataFiles _$DataFilesFromJson(Map json) => DataFiles()