@simplysm/capacitor-plugin-file-system 12.16.17 → 12.16.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +14 -14
- package/android/src/main/AndroidManifest.xml +23 -23
- package/android/src/main/java/kr/co/simplysm/capacitor/filesystem/FileSystemPlugin.java +289 -289
- package/android/src/main/java/kr/co/simplysm/capacitor/filesystem/FileSystemProvider.java +6 -6
- package/android/src/main/res/xml/file_provider_paths.xml +8 -8
- package/package.json +1 -1
package/android/build.gradle
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
apply plugin: 'com.android.library'
|
|
2
|
-
|
|
3
|
-
android {
|
|
4
|
-
namespace "kr.co.simplysm.capacitor.filesystem"
|
|
5
|
-
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
|
|
6
|
-
defaultConfig {
|
|
7
|
-
minSdk project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
dependencies {
|
|
12
|
-
implementation project(':capacitor-android')
|
|
13
|
-
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
14
|
-
}
|
|
1
|
+
apply plugin: 'com.android.library'
|
|
2
|
+
|
|
3
|
+
android {
|
|
4
|
+
namespace "kr.co.simplysm.capacitor.filesystem"
|
|
5
|
+
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
|
|
6
|
+
defaultConfig {
|
|
7
|
+
minSdk project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
implementation project(':capacitor-android')
|
|
13
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
14
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
3
|
-
xmlns:tools="http://schemas.android.com/tools">
|
|
4
|
-
|
|
5
|
-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
|
6
|
-
android:maxSdkVersion="32"/>
|
|
7
|
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
|
8
|
-
android:maxSdkVersion="29"/>
|
|
9
|
-
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
|
|
10
|
-
tools:ignore="ScopedStorage"/>
|
|
11
|
-
|
|
12
|
-
<application>
|
|
13
|
-
<provider
|
|
14
|
-
android:name=".FileSystemProvider"
|
|
15
|
-
android:authorities="${applicationId}.filesystem.provider"
|
|
16
|
-
android:exported="false"
|
|
17
|
-
android:grantUriPermissions="true">
|
|
18
|
-
<meta-data
|
|
19
|
-
android:name="android.support.FILE_PROVIDER_PATHS"
|
|
20
|
-
android:resource="@xml/file_provider_paths"/>
|
|
21
|
-
</provider>
|
|
22
|
-
</application>
|
|
23
|
-
</manifest>
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
3
|
+
xmlns:tools="http://schemas.android.com/tools">
|
|
4
|
+
|
|
5
|
+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
|
6
|
+
android:maxSdkVersion="32"/>
|
|
7
|
+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
|
8
|
+
android:maxSdkVersion="29"/>
|
|
9
|
+
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
|
|
10
|
+
tools:ignore="ScopedStorage"/>
|
|
11
|
+
|
|
12
|
+
<application>
|
|
13
|
+
<provider
|
|
14
|
+
android:name=".FileSystemProvider"
|
|
15
|
+
android:authorities="${applicationId}.filesystem.provider"
|
|
16
|
+
android:exported="false"
|
|
17
|
+
android:grantUriPermissions="true">
|
|
18
|
+
<meta-data
|
|
19
|
+
android:name="android.support.FILE_PROVIDER_PATHS"
|
|
20
|
+
android:resource="@xml/file_provider_paths"/>
|
|
21
|
+
</provider>
|
|
22
|
+
</application>
|
|
23
|
+
</manifest>
|
|
@@ -1,289 +1,289 @@
|
|
|
1
|
-
package kr.co.simplysm.capacitor.filesystem;
|
|
2
|
-
|
|
3
|
-
import android.Manifest;
|
|
4
|
-
import android.content.Context;
|
|
5
|
-
import android.content.Intent;
|
|
6
|
-
import android.content.pm.PackageManager;
|
|
7
|
-
import android.net.Uri;
|
|
8
|
-
import android.os.Build;
|
|
9
|
-
import android.os.Environment;
|
|
10
|
-
import android.provider.Settings;
|
|
11
|
-
import android.util.Base64;
|
|
12
|
-
import android.util.Log;
|
|
13
|
-
|
|
14
|
-
import androidx.core.content.ContextCompat;
|
|
15
|
-
import androidx.core.content.FileProvider;
|
|
16
|
-
|
|
17
|
-
import com.getcapacitor.JSArray;
|
|
18
|
-
import com.getcapacitor.JSObject;
|
|
19
|
-
import com.getcapacitor.Plugin;
|
|
20
|
-
import com.getcapacitor.PluginCall;
|
|
21
|
-
import com.getcapacitor.PluginMethod;
|
|
22
|
-
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
23
|
-
|
|
24
|
-
import java.io.*;
|
|
25
|
-
import java.nio.charset.StandardCharsets;
|
|
26
|
-
|
|
27
|
-
@CapacitorPlugin(name = "FileSystem")
|
|
28
|
-
public class FileSystemPlugin extends Plugin {
|
|
29
|
-
|
|
30
|
-
private static final String TAG = "FileSystemPlugin";
|
|
31
|
-
|
|
32
|
-
@PluginMethod
|
|
33
|
-
public void checkPermission(PluginCall call) {
|
|
34
|
-
boolean granted;
|
|
35
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
36
|
-
granted = Environment.isExternalStorageManager();
|
|
37
|
-
} else {
|
|
38
|
-
Context ctx = getContext();
|
|
39
|
-
granted = ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
|
40
|
-
&& ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
|
41
|
-
}
|
|
42
|
-
JSObject ret = new JSObject();
|
|
43
|
-
ret.put("granted", granted);
|
|
44
|
-
call.resolve(ret);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
@PluginMethod
|
|
48
|
-
public void requestPermission(PluginCall call) {
|
|
49
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
50
|
-
if (!Environment.isExternalStorageManager()) {
|
|
51
|
-
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
|
52
|
-
intent.setData(Uri.parse("package:" + getContext().getPackageName()));
|
|
53
|
-
getActivity().startActivity(intent);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
call.resolve();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@PluginMethod
|
|
60
|
-
public void readdir(PluginCall call) {
|
|
61
|
-
String path = call.getString("path");
|
|
62
|
-
if (path == null) {
|
|
63
|
-
call.reject("path is required");
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
File dir = new File(path);
|
|
68
|
-
if (!dir.exists() || !dir.isDirectory()) {
|
|
69
|
-
call.reject("Directory does not exist");
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
File[] files = dir.listFiles();
|
|
74
|
-
if (files == null) {
|
|
75
|
-
call.reject("Cannot read directory");
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
JSArray result = new JSArray();
|
|
80
|
-
for (File f : files) {
|
|
81
|
-
JSObject info = new JSObject();
|
|
82
|
-
info.put("name", f.getName());
|
|
83
|
-
info.put("isDirectory", f.isDirectory());
|
|
84
|
-
result.put(info);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
JSObject ret = new JSObject();
|
|
88
|
-
ret.put("files", result);
|
|
89
|
-
call.resolve(ret);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
@PluginMethod
|
|
93
|
-
public void getStoragePath(PluginCall call) {
|
|
94
|
-
String type = call.getString("type");
|
|
95
|
-
if (type == null) {
|
|
96
|
-
call.reject("type is required");
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
Context ctx = getContext();
|
|
101
|
-
File path;
|
|
102
|
-
|
|
103
|
-
switch (type) {
|
|
104
|
-
case "external":
|
|
105
|
-
path = Environment.getExternalStorageDirectory();
|
|
106
|
-
break;
|
|
107
|
-
case "externalFiles":
|
|
108
|
-
path = ctx.getExternalFilesDir(null);
|
|
109
|
-
break;
|
|
110
|
-
case "externalCache":
|
|
111
|
-
path = ctx.getExternalCacheDir();
|
|
112
|
-
break;
|
|
113
|
-
case "externalMedia":
|
|
114
|
-
File[] dirs = ctx.getExternalMediaDirs();
|
|
115
|
-
path = (dirs.length > 0) ? dirs[0] : null;
|
|
116
|
-
break;
|
|
117
|
-
case "appData":
|
|
118
|
-
path = new File(ctx.getApplicationInfo().dataDir);
|
|
119
|
-
break;
|
|
120
|
-
case "appFiles":
|
|
121
|
-
path = ctx.getFilesDir();
|
|
122
|
-
break;
|
|
123
|
-
case "appCache":
|
|
124
|
-
path = ctx.getCacheDir();
|
|
125
|
-
break;
|
|
126
|
-
default:
|
|
127
|
-
call.reject("Unknown type: " + type);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (path == null) {
|
|
132
|
-
call.reject("Path not available");
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
JSObject ret = new JSObject();
|
|
137
|
-
ret.put("path", path.getAbsolutePath());
|
|
138
|
-
call.resolve(ret);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
@PluginMethod
|
|
142
|
-
public void getFileUri(PluginCall call) {
|
|
143
|
-
String path = call.getString("path");
|
|
144
|
-
if (path == null) {
|
|
145
|
-
call.reject("path is required");
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
String authority = getContext().getPackageName() + ".filesystem.provider";
|
|
151
|
-
Uri uri = FileProvider.getUriForFile(getContext(), authority, new File(path));
|
|
152
|
-
JSObject ret = new JSObject();
|
|
153
|
-
ret.put("uri", uri.toString());
|
|
154
|
-
call.resolve(ret);
|
|
155
|
-
} catch (Exception e) {
|
|
156
|
-
Log.e(TAG, "getFileUri failed", e);
|
|
157
|
-
call.reject("getFileUri failed: " + e.getMessage());
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
@PluginMethod
|
|
162
|
-
public void writeFile(PluginCall call) {
|
|
163
|
-
String path = call.getString("path");
|
|
164
|
-
String data = call.getString("data");
|
|
165
|
-
String encoding = call.getString("encoding", "utf8");
|
|
166
|
-
|
|
167
|
-
if (path == null || data == null) {
|
|
168
|
-
call.reject("path and data are required");
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
try {
|
|
173
|
-
File file = new File(path);
|
|
174
|
-
File parent = file.getParentFile();
|
|
175
|
-
if (parent != null && !parent.exists()) {
|
|
176
|
-
parent.mkdirs();
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
byte[] bytes = "base64".equals(encoding)
|
|
180
|
-
? Base64.decode(data, Base64.DEFAULT)
|
|
181
|
-
: data.getBytes(StandardCharsets.UTF_8);
|
|
182
|
-
|
|
183
|
-
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
184
|
-
bos.write(bytes);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
call.resolve();
|
|
188
|
-
} catch (Exception e) {
|
|
189
|
-
Log.e(TAG, "writeFile failed", e);
|
|
190
|
-
call.reject("Write failed: " + e.getMessage());
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
@PluginMethod
|
|
195
|
-
public void readFile(PluginCall call) {
|
|
196
|
-
String path = call.getString("path");
|
|
197
|
-
String encoding = call.getString("encoding", "utf8");
|
|
198
|
-
|
|
199
|
-
if (path == null) {
|
|
200
|
-
call.reject("path is required");
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
File file = new File(path);
|
|
205
|
-
if (!file.exists()) {
|
|
206
|
-
call.reject("File not found: " + path);
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
211
|
-
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
212
|
-
|
|
213
|
-
byte[] buf = new byte[8192];
|
|
214
|
-
int len;
|
|
215
|
-
while ((len = bis.read(buf)) != -1) {
|
|
216
|
-
baos.write(buf, 0, len);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
String result = "base64".equals(encoding)
|
|
220
|
-
? Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP)
|
|
221
|
-
: baos.toString("UTF-8");
|
|
222
|
-
|
|
223
|
-
JSObject ret = new JSObject();
|
|
224
|
-
ret.put("data", result);
|
|
225
|
-
call.resolve(ret);
|
|
226
|
-
} catch (Exception e) {
|
|
227
|
-
Log.e(TAG, "readFile failed", e);
|
|
228
|
-
call.reject("Read failed: " + e.getMessage());
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
@PluginMethod
|
|
233
|
-
public void remove(PluginCall call) {
|
|
234
|
-
String path = call.getString("path");
|
|
235
|
-
if (path == null) {
|
|
236
|
-
call.reject("path is required");
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (deleteRecursively(new File(path))) {
|
|
241
|
-
call.resolve();
|
|
242
|
-
} else {
|
|
243
|
-
call.reject("Delete failed");
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
@PluginMethod
|
|
248
|
-
public void mkdir(PluginCall call) {
|
|
249
|
-
String path = call.getString("path");
|
|
250
|
-
if (path == null) {
|
|
251
|
-
call.reject("path is required");
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
File dir = new File(path);
|
|
256
|
-
if (dir.exists() || dir.mkdirs()) {
|
|
257
|
-
call.resolve();
|
|
258
|
-
} else {
|
|
259
|
-
call.reject("Failed to create directory");
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
@PluginMethod
|
|
264
|
-
public void exists(PluginCall call) {
|
|
265
|
-
String path = call.getString("path");
|
|
266
|
-
if (path == null) {
|
|
267
|
-
call.reject("path is required");
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
JSObject ret = new JSObject();
|
|
272
|
-
ret.put("exists", new File(path).exists());
|
|
273
|
-
call.resolve(ret);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
private boolean deleteRecursively(File file) {
|
|
277
|
-
if (file.isDirectory()) {
|
|
278
|
-
File[] children = file.listFiles();
|
|
279
|
-
if (children != null) {
|
|
280
|
-
for (File child : children) {
|
|
281
|
-
if (!deleteRecursively(child)) {
|
|
282
|
-
return false;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return file.delete();
|
|
288
|
-
}
|
|
289
|
-
}
|
|
1
|
+
package kr.co.simplysm.capacitor.filesystem;
|
|
2
|
+
|
|
3
|
+
import android.Manifest;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.pm.PackageManager;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.os.Build;
|
|
9
|
+
import android.os.Environment;
|
|
10
|
+
import android.provider.Settings;
|
|
11
|
+
import android.util.Base64;
|
|
12
|
+
import android.util.Log;
|
|
13
|
+
|
|
14
|
+
import androidx.core.content.ContextCompat;
|
|
15
|
+
import androidx.core.content.FileProvider;
|
|
16
|
+
|
|
17
|
+
import com.getcapacitor.JSArray;
|
|
18
|
+
import com.getcapacitor.JSObject;
|
|
19
|
+
import com.getcapacitor.Plugin;
|
|
20
|
+
import com.getcapacitor.PluginCall;
|
|
21
|
+
import com.getcapacitor.PluginMethod;
|
|
22
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
23
|
+
|
|
24
|
+
import java.io.*;
|
|
25
|
+
import java.nio.charset.StandardCharsets;
|
|
26
|
+
|
|
27
|
+
@CapacitorPlugin(name = "FileSystem")
|
|
28
|
+
public class FileSystemPlugin extends Plugin {
|
|
29
|
+
|
|
30
|
+
private static final String TAG = "FileSystemPlugin";
|
|
31
|
+
|
|
32
|
+
@PluginMethod
|
|
33
|
+
public void checkPermission(PluginCall call) {
|
|
34
|
+
boolean granted;
|
|
35
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
36
|
+
granted = Environment.isExternalStorageManager();
|
|
37
|
+
} else {
|
|
38
|
+
Context ctx = getContext();
|
|
39
|
+
granted = ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
|
40
|
+
&& ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
|
41
|
+
}
|
|
42
|
+
JSObject ret = new JSObject();
|
|
43
|
+
ret.put("granted", granted);
|
|
44
|
+
call.resolve(ret);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@PluginMethod
|
|
48
|
+
public void requestPermission(PluginCall call) {
|
|
49
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
50
|
+
if (!Environment.isExternalStorageManager()) {
|
|
51
|
+
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
|
52
|
+
intent.setData(Uri.parse("package:" + getContext().getPackageName()));
|
|
53
|
+
getActivity().startActivity(intent);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
call.resolve();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@PluginMethod
|
|
60
|
+
public void readdir(PluginCall call) {
|
|
61
|
+
String path = call.getString("path");
|
|
62
|
+
if (path == null) {
|
|
63
|
+
call.reject("path is required");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
File dir = new File(path);
|
|
68
|
+
if (!dir.exists() || !dir.isDirectory()) {
|
|
69
|
+
call.reject("Directory does not exist");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
File[] files = dir.listFiles();
|
|
74
|
+
if (files == null) {
|
|
75
|
+
call.reject("Cannot read directory");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
JSArray result = new JSArray();
|
|
80
|
+
for (File f : files) {
|
|
81
|
+
JSObject info = new JSObject();
|
|
82
|
+
info.put("name", f.getName());
|
|
83
|
+
info.put("isDirectory", f.isDirectory());
|
|
84
|
+
result.put(info);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
JSObject ret = new JSObject();
|
|
88
|
+
ret.put("files", result);
|
|
89
|
+
call.resolve(ret);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@PluginMethod
|
|
93
|
+
public void getStoragePath(PluginCall call) {
|
|
94
|
+
String type = call.getString("type");
|
|
95
|
+
if (type == null) {
|
|
96
|
+
call.reject("type is required");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
Context ctx = getContext();
|
|
101
|
+
File path;
|
|
102
|
+
|
|
103
|
+
switch (type) {
|
|
104
|
+
case "external":
|
|
105
|
+
path = Environment.getExternalStorageDirectory();
|
|
106
|
+
break;
|
|
107
|
+
case "externalFiles":
|
|
108
|
+
path = ctx.getExternalFilesDir(null);
|
|
109
|
+
break;
|
|
110
|
+
case "externalCache":
|
|
111
|
+
path = ctx.getExternalCacheDir();
|
|
112
|
+
break;
|
|
113
|
+
case "externalMedia":
|
|
114
|
+
File[] dirs = ctx.getExternalMediaDirs();
|
|
115
|
+
path = (dirs.length > 0) ? dirs[0] : null;
|
|
116
|
+
break;
|
|
117
|
+
case "appData":
|
|
118
|
+
path = new File(ctx.getApplicationInfo().dataDir);
|
|
119
|
+
break;
|
|
120
|
+
case "appFiles":
|
|
121
|
+
path = ctx.getFilesDir();
|
|
122
|
+
break;
|
|
123
|
+
case "appCache":
|
|
124
|
+
path = ctx.getCacheDir();
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
call.reject("Unknown type: " + type);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (path == null) {
|
|
132
|
+
call.reject("Path not available");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
JSObject ret = new JSObject();
|
|
137
|
+
ret.put("path", path.getAbsolutePath());
|
|
138
|
+
call.resolve(ret);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
@PluginMethod
|
|
142
|
+
public void getFileUri(PluginCall call) {
|
|
143
|
+
String path = call.getString("path");
|
|
144
|
+
if (path == null) {
|
|
145
|
+
call.reject("path is required");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
String authority = getContext().getPackageName() + ".filesystem.provider";
|
|
151
|
+
Uri uri = FileProvider.getUriForFile(getContext(), authority, new File(path));
|
|
152
|
+
JSObject ret = new JSObject();
|
|
153
|
+
ret.put("uri", uri.toString());
|
|
154
|
+
call.resolve(ret);
|
|
155
|
+
} catch (Exception e) {
|
|
156
|
+
Log.e(TAG, "getFileUri failed", e);
|
|
157
|
+
call.reject("getFileUri failed: " + e.getMessage());
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@PluginMethod
|
|
162
|
+
public void writeFile(PluginCall call) {
|
|
163
|
+
String path = call.getString("path");
|
|
164
|
+
String data = call.getString("data");
|
|
165
|
+
String encoding = call.getString("encoding", "utf8");
|
|
166
|
+
|
|
167
|
+
if (path == null || data == null) {
|
|
168
|
+
call.reject("path and data are required");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
File file = new File(path);
|
|
174
|
+
File parent = file.getParentFile();
|
|
175
|
+
if (parent != null && !parent.exists()) {
|
|
176
|
+
parent.mkdirs();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
byte[] bytes = "base64".equals(encoding)
|
|
180
|
+
? Base64.decode(data, Base64.DEFAULT)
|
|
181
|
+
: data.getBytes(StandardCharsets.UTF_8);
|
|
182
|
+
|
|
183
|
+
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
184
|
+
bos.write(bytes);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
call.resolve();
|
|
188
|
+
} catch (Exception e) {
|
|
189
|
+
Log.e(TAG, "writeFile failed", e);
|
|
190
|
+
call.reject("Write failed: " + e.getMessage());
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
@PluginMethod
|
|
195
|
+
public void readFile(PluginCall call) {
|
|
196
|
+
String path = call.getString("path");
|
|
197
|
+
String encoding = call.getString("encoding", "utf8");
|
|
198
|
+
|
|
199
|
+
if (path == null) {
|
|
200
|
+
call.reject("path is required");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
File file = new File(path);
|
|
205
|
+
if (!file.exists()) {
|
|
206
|
+
call.reject("File not found: " + path);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
211
|
+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
212
|
+
|
|
213
|
+
byte[] buf = new byte[8192];
|
|
214
|
+
int len;
|
|
215
|
+
while ((len = bis.read(buf)) != -1) {
|
|
216
|
+
baos.write(buf, 0, len);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
String result = "base64".equals(encoding)
|
|
220
|
+
? Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP)
|
|
221
|
+
: baos.toString("UTF-8");
|
|
222
|
+
|
|
223
|
+
JSObject ret = new JSObject();
|
|
224
|
+
ret.put("data", result);
|
|
225
|
+
call.resolve(ret);
|
|
226
|
+
} catch (Exception e) {
|
|
227
|
+
Log.e(TAG, "readFile failed", e);
|
|
228
|
+
call.reject("Read failed: " + e.getMessage());
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
@PluginMethod
|
|
233
|
+
public void remove(PluginCall call) {
|
|
234
|
+
String path = call.getString("path");
|
|
235
|
+
if (path == null) {
|
|
236
|
+
call.reject("path is required");
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (deleteRecursively(new File(path))) {
|
|
241
|
+
call.resolve();
|
|
242
|
+
} else {
|
|
243
|
+
call.reject("Delete failed");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
@PluginMethod
|
|
248
|
+
public void mkdir(PluginCall call) {
|
|
249
|
+
String path = call.getString("path");
|
|
250
|
+
if (path == null) {
|
|
251
|
+
call.reject("path is required");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
File dir = new File(path);
|
|
256
|
+
if (dir.exists() || dir.mkdirs()) {
|
|
257
|
+
call.resolve();
|
|
258
|
+
} else {
|
|
259
|
+
call.reject("Failed to create directory");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
@PluginMethod
|
|
264
|
+
public void exists(PluginCall call) {
|
|
265
|
+
String path = call.getString("path");
|
|
266
|
+
if (path == null) {
|
|
267
|
+
call.reject("path is required");
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
JSObject ret = new JSObject();
|
|
272
|
+
ret.put("exists", new File(path).exists());
|
|
273
|
+
call.resolve(ret);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
private boolean deleteRecursively(File file) {
|
|
277
|
+
if (file.isDirectory()) {
|
|
278
|
+
File[] children = file.listFiles();
|
|
279
|
+
if (children != null) {
|
|
280
|
+
for (File child : children) {
|
|
281
|
+
if (!deleteRecursively(child)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return file.delete();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
package kr.co.simplysm.capacitor.filesystem;
|
|
2
|
-
|
|
3
|
-
import androidx.core.content.FileProvider;
|
|
4
|
-
|
|
5
|
-
public class FileSystemProvider extends FileProvider {
|
|
6
|
-
}
|
|
1
|
+
package kr.co.simplysm.capacitor.filesystem;
|
|
2
|
+
|
|
3
|
+
import androidx.core.content.FileProvider;
|
|
4
|
+
|
|
5
|
+
public class FileSystemProvider extends FileProvider {
|
|
6
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
-
<paths>
|
|
3
|
-
<external-path name="external" path="." />
|
|
4
|
-
<external-files-path name="external_files" path="." />
|
|
5
|
-
<external-cache-path name="external_cache" path="." />
|
|
6
|
-
<files-path name="files" path="." />
|
|
7
|
-
<cache-path name="cache" path="." />
|
|
8
|
-
</paths>
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<paths>
|
|
3
|
+
<external-path name="external" path="." />
|
|
4
|
+
<external-files-path name="external_files" path="." />
|
|
5
|
+
<external-cache-path name="external_cache" path="." />
|
|
6
|
+
<files-path name="files" path="." />
|
|
7
|
+
<cache-path name="cache" path="." />
|
|
8
|
+
</paths>
|