-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.dart
170 lines (146 loc) · 5.89 KB
/
util.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
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:media_store_plus/media_store_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'main.dart';
String getPath({
String? relativePath,
required String fileName,
required DirType dirType,
required DirName dirName,
}) {
return "${dirType.fullPath(relativePath: relativePath.orAppFolder, dirName: dirName)}/$fileName";
}
File getFile({
String? relativePath,
required String fileName,
required DirType dirType,
required DirName dirName,
}) {
return File(
"${dirType.fullPath(relativePath: relativePath.orAppFolder, dirName: dirName)}/$fileName",
);
}
extension ByteDataToFile on ByteData {
Future<void> writeToFile(File file) async {
final buffer = this.buffer;
await file.writeAsBytes(buffer.asUint8List(offsetInBytes, lengthInBytes));
}
}
// It will try to read or write first in the given folder.
// if it is not accessible, it will request for permission for that folder then try again to read or write
Future<String?> readOrWriteApiLevel33WithPermission(
{required String fileName,
required String initialRelativePath,
required Function() operation}) async {
try {
await operation();
return '';
} on FileSystemException catch (e) {
print(e);
StringBuffer buffer = StringBuffer();
// To test this place a fileName.txt file in that folder
// request for read/write access for a folder in API level 33
// Use this also get write access for a folder from API level 30
final documentTree = await mediaStorePlugin.requestForAccess(
initialRelativePath: initialRelativePath);
if (documentTree != null && documentTree.childrenUriList.isNotEmpty) {
final uriString = documentTree.children
.where((element) => element.name!.contains(".txt"))
.first
.uriString;
print("Folder Uri ${documentTree.uri.toString()}");
print("File Uri $uriString");
documentTree.children.forEach((doc) {
print(doc);
});
// check if file exists by uri
await mediaStorePlugin
.isFileUriExist(uriString: uriString)
.then((value) => value == true ? print("Exist") : print(""));
// check if file is writable by uri
await mediaStorePlugin
.isFileWritable(uriString: uriString)
.then((value) => value == true ? print("Writable") : print(""));
// check if file is deletable by uri
await mediaStorePlugin
.isFileDeletable(uriString: uriString)
.then((value) => value == true ? print("Deletable") : print(""));
// read file by uri
File tempFile =
File("${(await getApplicationSupportDirectory()).path}/$fileName");
bool status = await mediaStorePlugin.readFileUsingUri(
uriString: uriString, tempFilePath: tempFile.path);
if (status) {
final text = await tempFile.readAsString();
print(text);
buffer.writeln('\n\nText Content Before Edit: $text');
}
// edit file by uri
await tempFile.writeAsString(
"EDITED: You are reading from API level 33/33+.\nFile Name: $fileName");
await mediaStorePlugin.editFile(
uriString: uriString, tempFilePath: tempFile.path);
status = await mediaStorePlugin.readFileUsingUri(
uriString: uriString, tempFilePath: tempFile.path);
if (status) {
final text = await tempFile.readAsString();
print(text);
buffer.writeln('\n\nText Content After Edit: $text');
}
// delete file by uri
// await mediaStorePlugin.deleteFileUsingUri(uriString: uriString);
print("read folder info by folder uri");
DocumentTree? docTree = await mediaStorePlugin.getDocumentTree(
uriString: documentTree.uriString);
if (docTree != null && docTree.childrenUriList.isNotEmpty) {
final uriString = docTree.children
.where((element) => element.name!.contains(".txt"))
.first
.uriString;
print("2 Folder Uri ${docTree.uri.toString()}");
print("2 File Uri $uriString");
docTree.children.forEach((doc) {
print("2 $doc");
});
// check if file exists by uri
await mediaStorePlugin
.isFileUriExist(uriString: uriString)
.then((value) => value == true ? print("Exist") : print(""));
// check if file is writable by uri
await mediaStorePlugin
.isFileWritable(uriString: uriString)
.then((value) => value == true ? print("Writable") : print(""));
// check if file is deletable by uri
await mediaStorePlugin
.isFileDeletable(uriString: uriString)
.then((value) => value == true ? print("Deletable") : print(""));
// read file by uri
File tempFile =
File("${(await getApplicationSupportDirectory()).path}/$fileName");
bool status = await mediaStorePlugin.readFileUsingUri(
uriString: uriString, tempFilePath: tempFile.path);
if (status) {
print((await tempFile.readAsString()));
}
// edit file by uri
await tempFile.writeAsString(
"EDITED: You are reading from API level 33/33+.\nFile Name: $fileName");
await mediaStorePlugin.editFile(
uriString: uriString, tempFilePath: tempFile.path);
status = await mediaStorePlugin.readFileUsingUri(
uriString: uriString, tempFilePath: tempFile.path);
if (status) {
print((await tempFile.readAsString()));
}
// delete file by uri
// await mediaStorePlugin.deleteFileUsingUri(uriString: uriString);
}
}
return buffer.toString();
} catch (e) {
print(e);
return null;
}
return null;
}