-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathfile_utils.dart
More file actions
151 lines (126 loc) · 4.32 KB
/
file_utils.dart
File metadata and controls
151 lines (126 loc) · 4.32 KB
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
import 'dart:convert';
import 'dart:io';
import 'package:core/domain/exceptions/download_file_exception.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter/services.dart';
import 'package:flutter_charset_detector/flutter_charset_detector.dart';
import 'package:path_provider/path_provider.dart';
class FileUtils {
static const String DEFAULT_CHARSET = 'utf-8';
static const String TEXT_PLAIN_MIME_TYPE = 'text/plain';
Future<String> _getInternalStorageDirPath({
required String nameFile,
String? folderPath,
String? extensionFile
}) async {
if (!PlatformInfo.isWeb) {
String fileDirectory = (await getApplicationDocumentsDirectory()).absolute.path;
if (folderPath != null) {
fileDirectory = '$fileDirectory/$folderPath';
}
Directory directory = Directory(fileDirectory);
if (!await directory.exists()) {
await directory.create(recursive: true);
}
fileDirectory = '$fileDirectory/$nameFile';
if (extensionFile != null) {
fileDirectory = '$fileDirectory.$extensionFile';
}
return fileDirectory;
} else {
throw const DeviceNotSupportedException();
}
}
Future<File> saveToFile({
required String nameFile,
required String content,
String? folderPath,
String? extensionFile
}) async {
final internalStorageDirPath = await _getInternalStorageDirPath(
nameFile: nameFile,
folderPath: folderPath,
extensionFile: extensionFile
);
final file = File(internalStorageDirPath);
log("FileUtils()::saveToFile: $file");
return await file.writeAsString(content, mode: FileMode.write);
}
Future<void> deleteFile(String filePath) async {
final file = File(filePath);
if (await file.exists()) {
await file.delete();
log("FileUtils()::deleteFile: $file");
} else {
log("FileUtils()::deleteFile: File does not exist");
}
}
Future<String> getContentFromFile({
required String nameFile,
String? folderPath,
String? extensionFile
}) async {
final internalStorageDirPath = await _getInternalStorageDirPath(
nameFile: nameFile,
folderPath: folderPath,
extensionFile: extensionFile
);
final file = File(internalStorageDirPath);
final emailContent = await file.readAsString();
log("FileUtils()::getFromFile: $emailContent");
return emailContent;
}
Future<Directory?> getFolder(String folderName) async {
try {
String folderPath = (await getApplicationDocumentsDirectory()).absolute.path;
folderPath = '$folderPath/$folderName';
log('FileUtils::getFolder(): $folderPath');
return Directory(folderPath);
} catch (e) {
logWarning('FileUtils::getFolder():EXCEPTION: $e');
return null;
}
}
Future<void> removeFolder(String folderName) async {
try {
final dir = await getFolder(folderName);
if (dir == null) return;
if (await dir.exists()) {
await dir.delete(recursive: true);
await dir.create(); // Re-create folder
log('FileUtils::removeFolder: Remove ${dir.path} success');
}
} catch (e) {
logWarning('FileUtils::removeFolder():EXCEPTION: $e');
}
}
Future<String> convertImageAssetToBase64(String assetImage) async {
ByteData bytes = await rootBundle.load(assetImage);
final buffer = bytes.buffer;
final base64Data = base64Encode(Uint8List.view(buffer));
return base64Data;
}
Future<void> deleteCompressedFileOnMobile(String filePath, {required String pathContains}) async {
log('$runtimeType::deleteCompressedFileOnMobile: filePath: $filePath');
if (!PlatformInfo.isMobile) return;
try {
final File file = File(filePath);
if (await file.exists() && file.path.contains(pathContains)) {
await file.delete();
}
} catch (e) {
logWarning('$runtimeType::deleteCompressedFileOnMobile: error: $e');
}
}
Future<String> getCharsetFromBytes(Uint8List bytes) async {
try {
final decodedResult = await CharsetDetector.autoDecode(bytes);
log('FileUtils::getCharsetFromBytes: FILE_CHARSET = ${decodedResult.charset}');
return decodedResult.charset;
} catch (e) {
logWarning('FileUtils::getCharsetFromBytes: Exception: $e');
return DEFAULT_CHARSET;
}
}
}