/
io.dart
152 lines (127 loc) · 3.89 KB
/
io.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:get/get.dart';
import 'package:get/get_core/get_core.dart';
import 'package:path_provider/path_provider.dart';
import '../value.dart';
class StorageImpl {
StorageImpl(this.fileName, [this.path]);
final String? path;
final String fileName;
final ValueStorage<Map<String, dynamic>> subject =
ValueStorage<Map<String, dynamic>>(<String, dynamic>{});
RandomAccessFile? _randomAccessfile;
void clear() async {
subject
..value!.clear()
..changeValue("", null);
}
Future<void> deleteBox() async {
final box = await _fileDb(false);
final backup = await _fileDb(true);
await Future.wait([box.delete(), backup.delete()]);
}
Future<void> flush() async {
final buffer = utf8.encode(json.encode(subject.value));
final length = buffer.length;
RandomAccessFile _file = await _getRandomFile();
_randomAccessfile = await _file.lock();
_randomAccessfile = await _randomAccessfile!.setPosition(0);
_randomAccessfile = await _randomAccessfile!.writeFrom(buffer);
_randomAccessfile = await _randomAccessfile!.truncate(length);
_randomAccessfile = await _file.unlock();
_madeBackup();
}
void _madeBackup() {
_getFile(true).then(
(value) => value.writeAsString(
json.encode(subject.value),
flush: true,
),
);
}
T? read<T>(String key) {
return subject.value![key] as T?;
}
T getKeys<T>() {
return subject.value!.keys as T;
}
T getValues<T>() {
return subject.value!.values as T;
}
Future<void> init([Map<String, dynamic>? initialData]) async {
subject.value = initialData ?? <String, dynamic>{};
RandomAccessFile _file = await _getRandomFile();
return _file.lengthSync() == 0 ? flush() : _readFile();
}
void remove(String key) {
subject
..value!.remove(key)
..changeValue(key, null);
}
void write(String key, dynamic value) {
subject
..value![key] = value
..changeValue(key, value);
}
Future<void> _readFile() async {
try {
RandomAccessFile _file = await _getRandomFile();
_file = await _file.setPosition(0);
final buffer = new Uint8List(await _file.length());
await _file.readInto(buffer);
subject.value = json.decode(utf8.decode(buffer));
} catch (e) {
Get.log('Corrupted box, recovering backup file', isError: true);
final _file = await _getFile(true);
final content = await _file.readAsString()
..trim();
if (content.isEmpty) {
subject.value = {};
} else {
try {
subject.value = json.decode(content) as Map<String, dynamic>?;
} catch (e) {
Get.log('Can not recover Corrupted box', isError: true);
subject.value = {};
}
}
flush();
}
}
Future<RandomAccessFile> _getRandomFile() async {
if (_randomAccessfile != null) return _randomAccessfile!;
final fileDb = await _getFile(false);
_randomAccessfile = await fileDb.open(mode: FileMode.append);
return _randomAccessfile!;
}
Future<File> _getFile(bool isBackup) async {
final fileDb = await _fileDb(isBackup);
if (!fileDb.existsSync()) {
fileDb.createSync(recursive: true);
}
return fileDb;
}
Future<File> _fileDb(bool isBackup) async {
final dir = await _getImplicitDir();
final _path = await _getPath(isBackup, path ?? dir.path);
final _file = File(_path);
return _file;
}
Future<Directory> _getImplicitDir() async {
try {
return getApplicationDocumentsDirectory();
} catch (err) {
throw err;
}
}
Future<String> _getPath(bool isBackup, String? path) async {
final _isWindows = GetPlatform.isWindows;
final _separator = _isWindows ? '\\' : '/';
return isBackup
? '$path$_separator$fileName.bak'
: '$path$_separator$fileName.gs';
}
}