@simplysm/capacitor-plugin-file-system 13.0.99 → 14.0.1

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.
@@ -1,305 +0,0 @@
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.app.ActivityCompat;
15
- import androidx.core.content.ContextCompat;
16
- import androidx.core.content.FileProvider;
17
-
18
- import com.getcapacitor.JSArray;
19
- import com.getcapacitor.JSObject;
20
- import com.getcapacitor.Plugin;
21
- import com.getcapacitor.PluginCall;
22
- import com.getcapacitor.PluginMethod;
23
- import com.getcapacitor.annotation.CapacitorPlugin;
24
-
25
- import java.io.*;
26
- import java.nio.charset.StandardCharsets;
27
-
28
- @CapacitorPlugin(name = "FileSystem")
29
- public class FileSystemPlugin extends Plugin {
30
-
31
- private static final String TAG = "FileSystemPlugin";
32
- private static final int PERMISSION_REQUEST_CODE = 1001;
33
-
34
- @PluginMethod
35
- public void checkPermissions(PluginCall call) {
36
- boolean granted;
37
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
38
- granted = Environment.isExternalStorageManager();
39
- } else {
40
- Context ctx = getContext();
41
- granted = ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
42
- && ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
43
- }
44
- JSObject ret = new JSObject();
45
- ret.put("granted", granted);
46
- call.resolve(ret);
47
- }
48
-
49
- @PluginMethod
50
- public void requestPermissions(PluginCall call) {
51
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
52
- if (!Environment.isExternalStorageManager()) {
53
- Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
54
- intent.setData(Uri.parse("package:" + getContext().getPackageName()));
55
- getActivity().startActivity(intent);
56
- }
57
- } else {
58
- boolean readGranted = ContextCompat.checkSelfPermission(getContext(),
59
- Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
60
- boolean writeGranted = ContextCompat.checkSelfPermission(getContext(),
61
- Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
62
-
63
- if (!readGranted || !writeGranted) {
64
- ActivityCompat.requestPermissions(getActivity(),
65
- new String[]{
66
- Manifest.permission.READ_EXTERNAL_STORAGE,
67
- Manifest.permission.WRITE_EXTERNAL_STORAGE
68
- },
69
- PERMISSION_REQUEST_CODE);
70
- }
71
- }
72
- call.resolve();
73
- }
74
-
75
- @PluginMethod
76
- public void readdir(PluginCall call) {
77
- String path = call.getString("path");
78
- if (path == null) {
79
- call.reject("path is required");
80
- return;
81
- }
82
-
83
- File dir = new File(path);
84
- if (!dir.exists() || !dir.isDirectory()) {
85
- call.reject("Directory does not exist");
86
- return;
87
- }
88
-
89
- File[] files = dir.listFiles();
90
- if (files == null) {
91
- call.reject("Cannot read directory");
92
- return;
93
- }
94
-
95
- JSArray result = new JSArray();
96
- for (File f : files) {
97
- JSObject info = new JSObject();
98
- info.put("name", f.getName());
99
- info.put("isDirectory", f.isDirectory());
100
- result.put(info);
101
- }
102
-
103
- JSObject ret = new JSObject();
104
- ret.put("files", result);
105
- call.resolve(ret);
106
- }
107
-
108
- @PluginMethod
109
- public void getStoragePath(PluginCall call) {
110
- String type = call.getString("type");
111
- if (type == null) {
112
- call.reject("type is required");
113
- return;
114
- }
115
-
116
- Context ctx = getContext();
117
- File path;
118
-
119
- switch (type) {
120
- case "external":
121
- path = Environment.getExternalStorageDirectory();
122
- break;
123
- case "externalFiles":
124
- path = ctx.getExternalFilesDir(null);
125
- break;
126
- case "externalCache":
127
- path = ctx.getExternalCacheDir();
128
- break;
129
- case "externalMedia":
130
- File[] dirs = ctx.getExternalMediaDirs();
131
- path = (dirs.length > 0) ? dirs[0] : null;
132
- break;
133
- case "appData":
134
- path = new File(ctx.getApplicationInfo().dataDir);
135
- break;
136
- case "appFiles":
137
- path = ctx.getFilesDir();
138
- break;
139
- case "appCache":
140
- path = ctx.getCacheDir();
141
- break;
142
- default:
143
- call.reject("Unknown type: " + type);
144
- return;
145
- }
146
-
147
- if (path == null) {
148
- call.reject("Path not available");
149
- return;
150
- }
151
-
152
- JSObject ret = new JSObject();
153
- ret.put("path", path.getAbsolutePath());
154
- call.resolve(ret);
155
- }
156
-
157
- @PluginMethod
158
- public void getUri(PluginCall call) {
159
- String path = call.getString("path");
160
- if (path == null) {
161
- call.reject("path is required");
162
- return;
163
- }
164
-
165
- try {
166
- String authority = getContext().getPackageName() + ".filesystem.provider";
167
- Uri uri = FileProvider.getUriForFile(getContext(), authority, new File(path));
168
- JSObject ret = new JSObject();
169
- ret.put("uri", uri.toString());
170
- call.resolve(ret);
171
- } catch (Exception e) {
172
- Log.e(TAG, "getUri failed", e);
173
- call.reject("getUri failed: " + e.getMessage());
174
- }
175
- }
176
-
177
- @PluginMethod
178
- public void writeFile(PluginCall call) {
179
- String path = call.getString("path");
180
- String data = call.getString("data");
181
- String encoding = call.getString("encoding", "utf8");
182
-
183
- if (path == null || data == null) {
184
- call.reject("path and data are required");
185
- return;
186
- }
187
-
188
- try {
189
- File file = new File(path);
190
- File parent = file.getParentFile();
191
- if (parent != null && !parent.exists()) {
192
- parent.mkdirs();
193
- }
194
-
195
- byte[] bytes = "base64".equals(encoding)
196
- ? Base64.decode(data, Base64.DEFAULT)
197
- : data.getBytes(StandardCharsets.UTF_8);
198
-
199
- try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
200
- bos.write(bytes);
201
- }
202
-
203
- call.resolve();
204
- } catch (Exception e) {
205
- Log.e(TAG, "writeFile failed", e);
206
- call.reject("Write failed: " + e.getMessage());
207
- }
208
- }
209
-
210
- @PluginMethod
211
- public void readFile(PluginCall call) {
212
- String path = call.getString("path");
213
- String encoding = call.getString("encoding", "utf8");
214
-
215
- if (path == null) {
216
- call.reject("path is required");
217
- return;
218
- }
219
-
220
- File file = new File(path);
221
- if (!file.exists()) {
222
- call.reject("File not found: " + path);
223
- return;
224
- }
225
-
226
- try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
227
- ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
228
-
229
- byte[] buf = new byte[8192];
230
- int len;
231
- while ((len = bis.read(buf)) != -1) {
232
- baos.write(buf, 0, len);
233
- }
234
-
235
- String result = "base64".equals(encoding)
236
- ? Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP)
237
- : baos.toString("UTF-8");
238
-
239
- JSObject ret = new JSObject();
240
- ret.put("data", result);
241
- call.resolve(ret);
242
- } catch (Exception e) {
243
- Log.e(TAG, "readFile failed", e);
244
- call.reject("Read failed: " + e.getMessage());
245
- }
246
- }
247
-
248
- @PluginMethod
249
- public void remove(PluginCall call) {
250
- String path = call.getString("path");
251
- if (path == null) {
252
- call.reject("path is required");
253
- return;
254
- }
255
-
256
- if (deleteRecursively(new File(path))) {
257
- call.resolve();
258
- } else {
259
- call.reject("Delete failed");
260
- }
261
- }
262
-
263
- @PluginMethod
264
- public void mkdir(PluginCall call) {
265
- String path = call.getString("path");
266
- if (path == null) {
267
- call.reject("path is required");
268
- return;
269
- }
270
-
271
- File dir = new File(path);
272
- if (dir.exists() || dir.mkdirs()) {
273
- call.resolve();
274
- } else {
275
- call.reject("Failed to create directory");
276
- }
277
- }
278
-
279
- @PluginMethod
280
- public void exists(PluginCall call) {
281
- String path = call.getString("path");
282
- if (path == null) {
283
- call.reject("path is required");
284
- return;
285
- }
286
-
287
- JSObject ret = new JSObject();
288
- ret.put("exists", new File(path).exists());
289
- call.resolve(ret);
290
- }
291
-
292
- private boolean deleteRecursively(File file) {
293
- if (file.isDirectory()) {
294
- File[] children = file.listFiles();
295
- if (children != null) {
296
- for (File child : children) {
297
- if (!deleteRecursively(child)) {
298
- return false;
299
- }
300
- }
301
- }
302
- }
303
- return file.delete();
304
- }
305
- }
@@ -1,6 +0,0 @@
1
- package kr.co.simplysm.capacitor.filesystem;
2
-
3
- import androidx.core.content.FileProvider;
4
-
5
- public class FileSystemProvider extends FileProvider {
6
- }