add file backup
This commit is contained in:
parent
9f25377514
commit
3fffaa576d
3 changed files with 17 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue