add file backup

This commit is contained in:
leedongmyung 2025-08-22 13:05:45 +09:00
parent 9f25377514
commit 3fffaa576d
3 changed files with 17 additions and 2 deletions

View file

@ -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);

View file

@ -518,6 +518,7 @@ class DataFileWrite extends DataParam {
late String path;
late String content;
late bool? backup;
factory DataFileWrite.fromJson(Map<String, dynamic> json) =>
_$DataFileWriteFromJson(json);

View file

@ -914,7 +914,8 @@ DataFileWrite _$DataFileWriteFromJson(Map<String, dynamic> 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<String, dynamic> _$DataFileWriteToJson(DataFileWrite instance) =>
<String, dynamic>{
@ -925,6 +926,7 @@ Map<String, dynamic> _$DataFileWriteToJson(DataFileWrite instance) =>
'showPrint': instance.showPrint,
'path': instance.path,
'content': instance.content,
'backup': instance.backup,
};
DataFiles _$DataFilesFromJson(Map<String, dynamic> json) => DataFiles()