This repository has been archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
file_system_utils.dart
442 lines (411 loc) · 13.5 KB
/
file_system_utils.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// dart
import 'dart:io';
import 'dart:async';
import 'dart:collection';
// packages
import 'package:path/path.dart' as pathlib;
// local
import 'exceptions.dart';
import 'sorting.dart';
import 'time_tools.dart';
import 'io_extensions.dart';
import 'package:path_provider/path_provider.dart';
import 'file_manager.dart';
// returns [File] or [Directory]
/// * argument objects = [File] or [Directory]
/// * argument by [String]: 'date', 'alpha', 'size'
List<dynamic> sortBy(List<dynamic> objects, FlutterFileUtilsSorting by,
{bool reversed: false}) {
switch (by) {
case FlutterFileUtilsSorting.Alpha:
objects
.sort((a, b) => getBaseName(a.path).compareTo(getBaseName(b.path)));
break;
case FlutterFileUtilsSorting.Date:
objects.sort((a, b) {
return a
.statSync()
.modified
.millisecondsSinceEpoch
.compareTo(b.statSync().modified.millisecondsSinceEpoch);
});
break;
case FlutterFileUtilsSorting.Size:
objects.sort((a, b) {
return a.statSync().size.compareTo(b.statSync().size);
});
break;
case FlutterFileUtilsSorting.Type:
objects.sort((a, b) {
return pathlib.extension(a.path).compareTo(pathlib.extension(b.path));
});
break;
default:
objects
.sort((a, b) => getBaseName(a.path).compareTo(getBaseName(b.path)));
}
if (reversed == true) {
return objects.reversed.toList();
}
return objects;
}
/// Return the name of the file or the folder
/// i.e: /root/home/myfile.zip = myfile.zip
/// [extension]: with extension [true] or not [false], [true]
/// by default
String getBaseName(String path, {bool extension: true}) {
if (extension) {
return pathlib.split(path).last;
} else {
return pathlib.split(path).last.split(new RegExp(r'\.\w+'))[0];
}
}
/// Returns a [HashMap] containing detials of the file or the directory
/// in organised style. you can use details from [Directory] or
/// [File] instead this function.
/// ### arguments
/// * [path] should be of [File] or [Directory]
///
/// ### keys
/// * type
/// * lastChanged
/// * lastModified
/// * size
/// * permissions
/// * lastAccessed
/// * extension
/// * path
Future<Map> details(dynamic path) async {
HashMap _details = HashMap();
if (path == null || (!path.existsSync() && !File(path).existsSync())) {
print("file or dir does not exists");
return null;
// directory
} else if (path.existsSync()) {
// directory or file
_details["type"] = path.statSync().type.toString();
_details["lastChanged"] = TimeTools.timeNormalize(path.statSync().changed);
_details["lastModified"] = TimeTools.timeNormalize(
Directory.fromUri(Uri.parse(path)).statSync().modified);
_details["size"] = path.statSync().size;
_details["type"] = path.statSync().type.toString();
_details["lastAccessed"] =
TimeTools.timeNormalize(path.statSync().accessed);
_details["permissions"] = path.statSync().modeString();
_details["path"] = path;
return _details;
// file
} else if (File(path).existsSync()) {
var fileStat = File(path).statSync();
// directory or file
_details["lastModified"] = TimeTools.timeNormalize(fileStat.modified);
_details["lastAccessed"] = TimeTools.timeNormalize(fileStat.accessed);
_details["lastChanged"] = TimeTools.timeNormalize(fileStat.changed);
_details["type"] = fileStat.type.toString();
_details["size"] = fileStat.size;
_details["permissions"] = fileStat.modeString();
_details["extension"] = pathlib.extension(path.path).replaceFirst('.', '');
_details["path"] = path;
return _details;
}
return null;
}
bool isHidden(String path, String root) {
// trying to infer relative path
if (pathlib.relative(path, from: root).startsWith('.')) {
return true;
} else {
return false;
}
}
/// keepHidden: show files that start with .
Stream<List<FileSystemEntity>> fileStream(String path,
{changeCurrentPath: true,
reverse: false,
recursive: false,
keepHidden: false}) async* {
Directory _path = Directory(path);
List<FileSystemEntity> _files = List<FileSystemEntity>();
try {
// Checking if the target directory contains files inside or not!
// so that [StreamBuilder] won't emit the same old data if there are
// no elements inside that directory.
if (_path.listSync(recursive: recursive).length != 0) {
if (!keepHidden) {
yield* _path.list(recursive: recursive).transform(
StreamTransformer.fromHandlers(
handleData: (FileSystemEntity data, sink) {
print("filsytem_utils -> fileStream: $data");
_files.add(data);
sink.add(_files);
}));
} else {
yield* _path.list(recursive: recursive).transform(
StreamTransformer.fromHandlers(
handleData: (FileSystemEntity data, sink) {
print("filsytem_utils -> fileStream: $data");
if (data.basename().startsWith('.')) {
_files.add(data);
sink.add(_files);
}
}));
}
} else {
yield [];
}
} on FileSystemException catch (e) {
print(e);
yield [];
}
}
/// search for files and folder in current directory & sub-directories,
/// and return [File] or [Directory]
///
/// `path`: start point
/// `query`: regex or simple string
Stream<List<FileSystemEntity>> searchStream(dynamic path, String query,
{bool matchCase: false, recursive: true, bool hidden: false}) async* {
yield* fileStream(path, recursive: recursive)
.transform(StreamTransformer.fromHandlers(handleData: (data, sink) {
// Filtering
data.retainWhere(
(test) => test.basename().toLowerCase().contains(query.toLowerCase()));
sink.add(data);
}));
}
// Future<int> getFreeSpace(String path) async {
// MethodChannel platform = const MethodChannel('samples.flutter.dev/battery');
// int freeSpace = await platform.invokeMethod("getFreeStorageSpace");
// return freeSpace;
// }
/// Create folder by path
/// * i.e: `.createFolderByPath("/storage/emulated/0/", "folder name" )`
///
/// Supply path alone to create by already combined path, or path + filename
/// to be combined
Future<Directory> createFolderByPath(String path, {String folderName}) async {
print("filesystem_utils->createFolderByPath: $folderName @ $path");
var _directory;
if (folderName != null) {
_directory = Directory(pathlib.join(path, folderName));
} else {
_directory = Directory(path);
}
try {
if (!_directory.existsSync()) {
_directory.create();
} else {
FileSystemException("File already exists");
}
return _directory;
} catch (e) {
throw FileSystemException(e);
}
}
/// This function returns every [Directory] in the path
/// independently
///
/// e.g:
///
/// path: /lib/user/share/var/foo
/// * `Directory: /`
/// * `Directory: /lib/`
/// * `Directory: /lib/user`
/// * `Directory: /lib/user/share`
/// * `....`
List<Directory> splitPathToDirectories(String fullPath) {
List<Directory> splittedPath = List();
Directory fullPathDir = Directory(fullPath);
splittedPath.add(fullPathDir);
for (int i = 0; i == pathlib.split(fullPath).length; i++) {
splittedPath.add(fullPathDir.parent);
fullPathDir = fullPathDir.parent;
}
return splittedPath.reversed.toList();
}
void copy(String targetPath, String destination) {
Directory targetDir = Directory(targetPath);
try {
print("Trying Copying directory: $targetPath");
if (targetDir.existsSync()) {
print("Target path exists, copying directory...");
String targetBasename = pathlib.basename(targetPath);
// Create target
Directory newPath = Directory(pathlib.join(destination, targetBasename))
..create();
for (var fileOrDir in targetDir.listSync()) {
// if it was file
if (fileOrDir is File) {
print("Copying file: ${fileOrDir.path} to ${newPath.path}\n");
fileOrDir.copy(
pathlib.join(newPath.path, pathlib.basename(fileOrDir.path)));
} else if (fileOrDir is Directory) {
print("Copying directory: ${fileOrDir.path} to ${newPath.path}\n");
// recursion
copy(fileOrDir.path, newPath.path);
}
// if ? is link then ...
else {
fileOrDir.rename(
pathlib.join(newPath.path, pathlib.basename(fileOrDir.path)));
}
}
} else {
throw FileSystemException("Target does not exist", targetPath);
}
} catch (e) {
rethrow;
}
}
Future<Directory> rename(String target, String destination) async {
Directory targetDir = Directory(target);
String basename = pathlib.basename(targetDir.path);
return await targetDir.rename(pathlib.join(destination, basename));
}
/// This function creates temporary file on the device storage
/// Return [File]
/// You can call normal [File] methods
Future<File> cacheFile(String name) async {
Directory tempDir = await getTemporaryDirectory();
return File(pathlib.join(tempDir.path, name));
}
/// This function returns files' paths list only from specific location.
/// * You may specify the types of the files you want to get by supplying the optional
/// [extensions].
/// * sortedBy: [FlutterFileUtilsSorting]
/// * [bool] reversed: in case parameter sortedBy is used
Future<List<File>> listFiles(String path,
{List<String> extensions,
followsLinks = false,
excludeHidden = false,
FlutterFileUtilsSorting sortedBy,
bool reversed: false}) async {
List<File> files = [];
try {
List contents =
Directory(path).listSync(followLinks: followsLinks, recursive: false);
if (extensions != null) {
// Future<List<String>> extensionsPatterns =
// RegexTools.makeExtensionPatterns(extensions);
for (var fileOrDir in contents) {
if (fileOrDir is File) {
String file = pathlib.normalize(fileOrDir.path);
for (var extension in extensions) {
if (pathlib.extension(file).replaceFirst(".", "") ==
extension.replaceFirst('.', '')) {
if (excludeHidden) {
if (file.startsWith('.'))
files.add(File(pathlib.normalize(fileOrDir.absolute.path)));
} else {
files.add(File(pathlib.normalize(fileOrDir.absolute.path)));
}
}
}
}
}
} else {
for (var fileOrDir in contents) {
if (fileOrDir is File) {
files.add(File(pathlib.normalize(fileOrDir.absolute.path)));
}
}
}
} catch (error) {
throw FileManagerError(error.toString());
}
if (files != null) {
return sortBy(files, sortedBy, reversed: reversed);
}
return files;
}
/// This function return list of folders of type [String] , not full paths [Directory].
///
/// e.g: listFolders(Directory("/")) = root, usr, var, proc, mnt ...
/// * [hidden]: this parameter excludes folders starts with " . "
/// * [excludedFolders]: this parameter excludes folders from the result
/// * sortedBy: [Sorting]
/// * [bool] reversed: in case parameter sortedBy is used
/// * examples: ["Android", "Download", "DCIM", ....]
Future<List<String>> listFolders(Directory path,
{List<String> excludedFolders,
List<String> excludedPaths,
bool excludeHidden: false,
followLinks: false,
FlutterFileUtilsSorting sortedBy,
bool reversed: false}) async {
List<String> folders = (await listDirectories(path,
excludeHidden: excludeHidden,
followLinks: false,
reversed: reversed,
sortedBy: sortedBy))
.map((Directory directory) => pathlib.split(directory.absolute.path).last)
.toList();
return folders;
}
/// Return a [List] of directories starting from the given path
/// * [hidden] : [true] or [false] return hidden directory, like: "/storage/.thumbnails"
/// * [true] will return hidden directories
/// * sortedBy: [Sorting]
/// * [bool] reversed: in case parameter sortedBy is used
Future<List<Directory>> listDirectories(Directory path,
{excludeHidden: false,
followLinks = false,
FlutterFileUtilsSorting sortedBy,
bool reversed: false}) async {
List<Directory> directories = [];
try {
List contents = path.listSync(followLinks: followLinks, recursive: false);
if (excludeHidden == true) {
for (var fileOrDir in contents) {
if (fileOrDir is Directory) {
if (!fileOrDir.path.startsWith("."))
directories
.add(Directory(pathlib.normalize(fileOrDir.absolute.path)));
}
}
} else {
for (var fileOrDir in contents) {
if (fileOrDir is Directory) {
// dir/../dir3 to dir/dir2/dir3
directories
.add(Directory(pathlib.normalize(fileOrDir.absolute.path)));
}
}
}
} catch (error) {
throw FileManagerError(permissionMessage + error.toString());
}
if (directories != null) {
return sortBy(directories, sortedBy);
}
return directories;
}
/// e.g:
Future<void> deleteAll(List<FileSystemEntity> files) async {
try {
for (var file in files) {
file.delete();
}
} on FileSystemException catch (e) {
throw FileManagerError(e.toString());
} catch (e) {
rethrow;
}
}
/// Delete a directory recursively or not
///
/// e.g:
/// * deleteFile(/storage/emulated/0/myFile.txt")
bool deleteDir(String path, {recursive: false}) {
//print("~ deleting:" + path);
if (File(path).existsSync()) {
throw Exception("This is a file path not a directory path");
}
var file = File(path);
try {
file.delete(recursive: recursive);
return true;
} catch (error) {
throw FileManagerError(error.toString());
}
}