@capacitor/filesystem 7.0.2-nightly-20250526T150552.0 → 7.1.0-dev.2
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/CapacitorFilesystem.podspec +4 -3
- package/LICENSE +17 -19
- package/Package.swift +10 -4
- package/README.md +149 -78
- package/android/build.gradle +12 -22
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/FilesystemErrors.kt +101 -0
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/FilesystemMethodOptions.kt +129 -0
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/FilesystemMethodResults.kt +65 -0
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/FilesystemPlugin.kt +412 -0
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/LegacyFilesystemImplementation.kt +169 -0
- package/android/src/main/kotlin/com/capacitorjs/plugins/filesystem/PluginResultExtensions.kt +25 -0
- package/dist/docs.json +227 -145
- package/dist/esm/definitions.d.ts +102 -64
- package/dist/esm/definitions.js +25 -3
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +10 -10
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +40 -14
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +41 -16
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/FilesystemPlugin/CAPPluginCall+Accelerators.swift +73 -0
- package/ios/Sources/FilesystemPlugin/FilesystemConstants.swift +61 -0
- package/ios/Sources/FilesystemPlugin/FilesystemError.swift +57 -0
- package/ios/Sources/FilesystemPlugin/FilesystemLocationResolver.swift +39 -0
- package/ios/Sources/FilesystemPlugin/FilesystemOperation.swift +24 -0
- package/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift +116 -0
- package/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift +103 -264
- package/ios/Sources/FilesystemPlugin/IONFileStructures+Converters.swift +60 -0
- package/ios/Sources/FilesystemPlugin/{Filesystem.swift → LegacyFilesystemImplementation.swift} +18 -179
- package/package.json +28 -24
- package/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java +0 -414
- package/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java +0 -551
- package/android/src/main/java/com/capacitorjs/plugins/filesystem/exceptions/CopyFailedException.java +0 -16
- package/android/src/main/java/com/capacitorjs/plugins/filesystem/exceptions/DirectoryExistsException.java +0 -16
- package/android/src/main/java/com/capacitorjs/plugins/filesystem/exceptions/DirectoryNotFoundException.java +0 -16
|
@@ -1,414 +0,0 @@
|
|
|
1
|
-
package com.capacitorjs.plugins.filesystem;
|
|
2
|
-
|
|
3
|
-
import android.content.Context;
|
|
4
|
-
import android.net.Uri;
|
|
5
|
-
import android.os.Environment;
|
|
6
|
-
import android.os.Handler;
|
|
7
|
-
import android.os.Looper;
|
|
8
|
-
import android.util.Base64;
|
|
9
|
-
import com.capacitorjs.plugins.filesystem.exceptions.CopyFailedException;
|
|
10
|
-
import com.capacitorjs.plugins.filesystem.exceptions.DirectoryExistsException;
|
|
11
|
-
import com.capacitorjs.plugins.filesystem.exceptions.DirectoryNotFoundException;
|
|
12
|
-
import com.getcapacitor.Bridge;
|
|
13
|
-
import com.getcapacitor.JSObject;
|
|
14
|
-
import com.getcapacitor.PluginCall;
|
|
15
|
-
import com.getcapacitor.plugin.util.CapacitorHttpUrlConnection;
|
|
16
|
-
import com.getcapacitor.plugin.util.HttpRequestHandler;
|
|
17
|
-
import java.io.BufferedWriter;
|
|
18
|
-
import java.io.ByteArrayOutputStream;
|
|
19
|
-
import java.io.File;
|
|
20
|
-
import java.io.FileInputStream;
|
|
21
|
-
import java.io.FileNotFoundException;
|
|
22
|
-
import java.io.FileOutputStream;
|
|
23
|
-
import java.io.IOException;
|
|
24
|
-
import java.io.InputStream;
|
|
25
|
-
import java.io.OutputStreamWriter;
|
|
26
|
-
import java.net.URISyntaxException;
|
|
27
|
-
import java.net.URL;
|
|
28
|
-
import java.nio.channels.FileChannel;
|
|
29
|
-
import java.nio.charset.Charset;
|
|
30
|
-
import java.nio.charset.StandardCharsets;
|
|
31
|
-
import java.util.Locale;
|
|
32
|
-
import java.util.concurrent.ExecutorService;
|
|
33
|
-
import java.util.concurrent.Executors;
|
|
34
|
-
import org.json.JSONException;
|
|
35
|
-
|
|
36
|
-
public class Filesystem {
|
|
37
|
-
|
|
38
|
-
private Context context;
|
|
39
|
-
|
|
40
|
-
Filesystem(Context context) {
|
|
41
|
-
this.context = context;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public String readFile(String path, String directory, Charset charset) throws IOException {
|
|
45
|
-
InputStream is = getInputStream(path, directory);
|
|
46
|
-
String dataStr;
|
|
47
|
-
if (charset != null) {
|
|
48
|
-
dataStr = readFileAsString(is, charset.name());
|
|
49
|
-
} else {
|
|
50
|
-
dataStr = readFileAsBase64EncodedData(is);
|
|
51
|
-
}
|
|
52
|
-
return dataStr;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public void saveFile(File file, String data, Charset charset, Boolean append) throws IOException {
|
|
56
|
-
// if charset is not null assume its a plain text file the user wants to save
|
|
57
|
-
if (charset != null) {
|
|
58
|
-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), charset));
|
|
59
|
-
writer.write(data);
|
|
60
|
-
writer.close();
|
|
61
|
-
} else {
|
|
62
|
-
//remove header from dataURL
|
|
63
|
-
if (data.contains(",")) {
|
|
64
|
-
data = data.split(",")[1];
|
|
65
|
-
}
|
|
66
|
-
FileOutputStream fos = new FileOutputStream(file, append);
|
|
67
|
-
fos.write(Base64.decode(data, Base64.NO_WRAP));
|
|
68
|
-
fos.close();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public boolean deleteFile(String file, String directory) throws FileNotFoundException {
|
|
73
|
-
File fileObject = getFileObject(file, directory);
|
|
74
|
-
if (!fileObject.exists()) {
|
|
75
|
-
throw new FileNotFoundException("File does not exist");
|
|
76
|
-
}
|
|
77
|
-
return fileObject.delete();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public boolean mkdir(String path, String directory, Boolean recursive) throws DirectoryExistsException {
|
|
81
|
-
File fileObject = getFileObject(path, directory);
|
|
82
|
-
|
|
83
|
-
if (fileObject.exists()) {
|
|
84
|
-
throw new DirectoryExistsException("Directory exists");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
boolean created = false;
|
|
88
|
-
if (recursive) {
|
|
89
|
-
created = fileObject.mkdirs();
|
|
90
|
-
} else {
|
|
91
|
-
created = fileObject.mkdir();
|
|
92
|
-
}
|
|
93
|
-
return created;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public File[] readdir(String path, String directory) throws DirectoryNotFoundException {
|
|
97
|
-
File[] files = null;
|
|
98
|
-
File fileObject = getFileObject(path, directory);
|
|
99
|
-
if (fileObject != null && fileObject.exists()) {
|
|
100
|
-
files = fileObject.listFiles();
|
|
101
|
-
} else {
|
|
102
|
-
throw new DirectoryNotFoundException("Directory does not exist");
|
|
103
|
-
}
|
|
104
|
-
return files;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public File copy(String from, String directory, String to, String toDirectory, boolean doRename)
|
|
108
|
-
throws IOException, CopyFailedException {
|
|
109
|
-
if (toDirectory == null) {
|
|
110
|
-
toDirectory = directory;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
File fromObject = getFileObject(from, directory);
|
|
114
|
-
File toObject = getFileObject(to, toDirectory);
|
|
115
|
-
|
|
116
|
-
if (fromObject == null) {
|
|
117
|
-
throw new CopyFailedException("from file is null");
|
|
118
|
-
}
|
|
119
|
-
if (toObject == null) {
|
|
120
|
-
throw new CopyFailedException("to file is null");
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (toObject.equals(fromObject)) {
|
|
124
|
-
return toObject;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!fromObject.exists()) {
|
|
128
|
-
throw new CopyFailedException("The source object does not exist");
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (toObject.getParentFile().isFile()) {
|
|
132
|
-
throw new CopyFailedException("The parent object of the destination is a file");
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!toObject.getParentFile().exists()) {
|
|
136
|
-
throw new CopyFailedException("The parent object of the destination does not exist");
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (toObject.isDirectory()) {
|
|
140
|
-
throw new CopyFailedException("Cannot overwrite a directory");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
toObject.delete();
|
|
144
|
-
|
|
145
|
-
if (doRename) {
|
|
146
|
-
boolean modified = fromObject.renameTo(toObject);
|
|
147
|
-
if (!modified) {
|
|
148
|
-
throw new CopyFailedException("Unable to rename, unknown reason");
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
copyRecursively(fromObject, toObject);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return toObject;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public InputStream getInputStream(String path, String directory) throws IOException {
|
|
158
|
-
if (directory == null) {
|
|
159
|
-
Uri u = Uri.parse(path);
|
|
160
|
-
if (u.getScheme().equals("content")) {
|
|
161
|
-
return this.context.getContentResolver().openInputStream(u);
|
|
162
|
-
} else {
|
|
163
|
-
return new FileInputStream(new File(u.getPath()));
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
File androidDirectory = this.getDirectory(directory);
|
|
168
|
-
|
|
169
|
-
if (androidDirectory == null) {
|
|
170
|
-
throw new IOException("Directory not found");
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return new FileInputStream(new File(androidDirectory, path));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
public String readFileAsString(InputStream is, String encoding) throws IOException {
|
|
177
|
-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
178
|
-
|
|
179
|
-
byte[] buffer = new byte[1024];
|
|
180
|
-
int length = 0;
|
|
181
|
-
|
|
182
|
-
while ((length = is.read(buffer)) != -1) {
|
|
183
|
-
outputStream.write(buffer, 0, length);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return outputStream.toString(encoding);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
public String readFileAsBase64EncodedData(InputStream is) throws IOException {
|
|
190
|
-
FileInputStream fileInputStreamReader = (FileInputStream) is;
|
|
191
|
-
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
|
192
|
-
|
|
193
|
-
byte[] buffer = new byte[1024];
|
|
194
|
-
|
|
195
|
-
int c;
|
|
196
|
-
while ((c = fileInputStreamReader.read(buffer)) != -1) {
|
|
197
|
-
byteStream.write(buffer, 0, c);
|
|
198
|
-
}
|
|
199
|
-
fileInputStreamReader.close();
|
|
200
|
-
|
|
201
|
-
return Base64.encodeToString(byteStream.toByteArray(), Base64.NO_WRAP);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
@SuppressWarnings("deprecation")
|
|
205
|
-
public File getDirectory(String directory) {
|
|
206
|
-
Context c = this.context;
|
|
207
|
-
switch (directory) {
|
|
208
|
-
case "DOCUMENTS":
|
|
209
|
-
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
|
|
210
|
-
case "DATA":
|
|
211
|
-
case "LIBRARY":
|
|
212
|
-
return c.getFilesDir();
|
|
213
|
-
case "CACHE":
|
|
214
|
-
return c.getCacheDir();
|
|
215
|
-
case "EXTERNAL":
|
|
216
|
-
return c.getExternalFilesDir(null);
|
|
217
|
-
case "EXTERNAL_STORAGE":
|
|
218
|
-
return Environment.getExternalStorageDirectory();
|
|
219
|
-
}
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
public File getFileObject(String path, String directory) {
|
|
224
|
-
if (directory == null) {
|
|
225
|
-
Uri u = Uri.parse(path);
|
|
226
|
-
if (u.getScheme() == null || u.getScheme().equals("file")) {
|
|
227
|
-
return new File(u.getPath());
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
File androidDirectory = this.getDirectory(directory);
|
|
232
|
-
|
|
233
|
-
if (androidDirectory == null) {
|
|
234
|
-
return null;
|
|
235
|
-
} else {
|
|
236
|
-
if (!androidDirectory.exists()) {
|
|
237
|
-
androidDirectory.mkdir();
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return new File(androidDirectory, path);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
public Charset getEncoding(String encoding) {
|
|
245
|
-
if (encoding == null) {
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
switch (encoding) {
|
|
250
|
-
case "utf8":
|
|
251
|
-
return StandardCharsets.UTF_8;
|
|
252
|
-
case "utf16":
|
|
253
|
-
return StandardCharsets.UTF_16;
|
|
254
|
-
case "ascii":
|
|
255
|
-
return StandardCharsets.US_ASCII;
|
|
256
|
-
}
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Helper function to recursively delete a directory
|
|
262
|
-
*
|
|
263
|
-
* @param file The file or directory to recursively delete
|
|
264
|
-
* @throws IOException
|
|
265
|
-
*/
|
|
266
|
-
public void deleteRecursively(File file) throws IOException {
|
|
267
|
-
if (file.isFile()) {
|
|
268
|
-
file.delete();
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
for (File f : file.listFiles()) {
|
|
273
|
-
deleteRecursively(f);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
file.delete();
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Helper function to recursively copy a directory structure (or just a file)
|
|
281
|
-
*
|
|
282
|
-
* @param src The source location
|
|
283
|
-
* @param dst The destination location
|
|
284
|
-
* @throws IOException
|
|
285
|
-
*/
|
|
286
|
-
public void copyRecursively(File src, File dst) throws IOException {
|
|
287
|
-
if (src.isDirectory()) {
|
|
288
|
-
dst.mkdir();
|
|
289
|
-
|
|
290
|
-
for (String file : src.list()) {
|
|
291
|
-
copyRecursively(new File(src, file), new File(dst, file));
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (!dst.getParentFile().exists()) {
|
|
298
|
-
dst.getParentFile().mkdirs();
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (!dst.exists()) {
|
|
302
|
-
dst.createNewFile();
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
try (FileChannel source = new FileInputStream(src).getChannel(); FileChannel destination = new FileOutputStream(dst).getChannel()) {
|
|
306
|
-
destination.transferFrom(source, 0, source.size());
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
public void downloadFile(
|
|
311
|
-
PluginCall call,
|
|
312
|
-
Bridge bridge,
|
|
313
|
-
HttpRequestHandler.ProgressEmitter emitter,
|
|
314
|
-
FilesystemDownloadCallback callback
|
|
315
|
-
) {
|
|
316
|
-
String urlString = call.getString("url", "");
|
|
317
|
-
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
318
|
-
Handler handler = new Handler(Looper.getMainLooper());
|
|
319
|
-
|
|
320
|
-
executor.execute(
|
|
321
|
-
() -> {
|
|
322
|
-
try {
|
|
323
|
-
JSObject result = doDownloadInBackground(urlString, call, bridge, emitter);
|
|
324
|
-
handler.post(() -> callback.onSuccess(result));
|
|
325
|
-
} catch (Exception error) {
|
|
326
|
-
handler.post(() -> callback.onError(error));
|
|
327
|
-
} finally {
|
|
328
|
-
executor.shutdown();
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private JSObject doDownloadInBackground(String urlString, PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter)
|
|
335
|
-
throws IOException, URISyntaxException, JSONException {
|
|
336
|
-
JSObject headers = call.getObject("headers", new JSObject());
|
|
337
|
-
JSObject params = call.getObject("params", new JSObject());
|
|
338
|
-
Integer connectTimeout = call.getInt("connectTimeout");
|
|
339
|
-
Integer readTimeout = call.getInt("readTimeout");
|
|
340
|
-
Boolean disableRedirects = call.getBoolean("disableRedirects");
|
|
341
|
-
Boolean shouldEncode = call.getBoolean("shouldEncodeUrlParams", true);
|
|
342
|
-
Boolean progress = call.getBoolean("progress", false);
|
|
343
|
-
|
|
344
|
-
String method = call.getString("method", "GET").toUpperCase(Locale.ROOT);
|
|
345
|
-
String path = call.getString("path");
|
|
346
|
-
String directory = call.getString("directory", Environment.DIRECTORY_DOWNLOADS);
|
|
347
|
-
|
|
348
|
-
final URL url = new URL(urlString);
|
|
349
|
-
final File file = getFileObject(path, directory);
|
|
350
|
-
|
|
351
|
-
HttpRequestHandler.HttpURLConnectionBuilder connectionBuilder = new HttpRequestHandler.HttpURLConnectionBuilder()
|
|
352
|
-
.setUrl(url)
|
|
353
|
-
.setMethod(method)
|
|
354
|
-
.setHeaders(headers)
|
|
355
|
-
.setUrlParams(params, shouldEncode)
|
|
356
|
-
.setConnectTimeout(connectTimeout)
|
|
357
|
-
.setReadTimeout(readTimeout)
|
|
358
|
-
.setDisableRedirects(disableRedirects)
|
|
359
|
-
.openConnection();
|
|
360
|
-
|
|
361
|
-
CapacitorHttpUrlConnection connection = connectionBuilder.build();
|
|
362
|
-
|
|
363
|
-
connection.setSSLSocketFactory(bridge);
|
|
364
|
-
|
|
365
|
-
InputStream connectionInputStream = connection.getInputStream();
|
|
366
|
-
FileOutputStream fileOutputStream = new FileOutputStream(file, false);
|
|
367
|
-
|
|
368
|
-
String contentLength = connection.getHeaderField("content-length");
|
|
369
|
-
int bytes = 0;
|
|
370
|
-
int maxBytes = 0;
|
|
371
|
-
|
|
372
|
-
try {
|
|
373
|
-
maxBytes = contentLength != null ? Integer.parseInt(contentLength) : 0;
|
|
374
|
-
} catch (NumberFormatException ignored) {}
|
|
375
|
-
|
|
376
|
-
byte[] buffer = new byte[1024];
|
|
377
|
-
int len;
|
|
378
|
-
|
|
379
|
-
// Throttle emitter to 100ms so it doesn't slow down app
|
|
380
|
-
long lastEmitTime = System.currentTimeMillis();
|
|
381
|
-
long minEmitIntervalMillis = 100;
|
|
382
|
-
|
|
383
|
-
while ((len = connectionInputStream.read(buffer)) > 0) {
|
|
384
|
-
fileOutputStream.write(buffer, 0, len);
|
|
385
|
-
|
|
386
|
-
bytes += len;
|
|
387
|
-
|
|
388
|
-
if (progress && null != emitter) {
|
|
389
|
-
long currentTime = System.currentTimeMillis();
|
|
390
|
-
if (currentTime - lastEmitTime > minEmitIntervalMillis) {
|
|
391
|
-
emitter.emit(bytes, maxBytes);
|
|
392
|
-
lastEmitTime = currentTime;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
if (progress && null != emitter) {
|
|
398
|
-
emitter.emit(bytes, maxBytes);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
connectionInputStream.close();
|
|
402
|
-
fileOutputStream.close();
|
|
403
|
-
|
|
404
|
-
JSObject ret = new JSObject();
|
|
405
|
-
ret.put("path", file.getAbsolutePath());
|
|
406
|
-
return ret;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
public interface FilesystemDownloadCallback {
|
|
410
|
-
void onSuccess(JSObject result);
|
|
411
|
-
|
|
412
|
-
void onError(Exception error);
|
|
413
|
-
}
|
|
414
|
-
}
|