@simplysm/capacitor-plugin-file-system 12.16.30 → 12.16.31
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/README.md +246 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# @simplysm/capacitor-plugin-file-system
|
|
2
|
+
|
|
3
|
+
Capacitor plugin for file system access on Android and web.
|
|
4
|
+
|
|
5
|
+
- **Android 11+**: Uses `MANAGE_EXTERNAL_STORAGE` permission for full file system access
|
|
6
|
+
- **Android 10 and below**: Uses `READ/WRITE_EXTERNAL_STORAGE` permissions
|
|
7
|
+
- **Web (browser)**: IndexedDB-based virtual file system emulation
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @simplysm/capacitor-plugin-file-system
|
|
13
|
+
# or
|
|
14
|
+
yarn add @simplysm/capacitor-plugin-file-system
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**Peer dependency**: `@capacitor/core` ^7.4.4
|
|
18
|
+
|
|
19
|
+
### Android Setup
|
|
20
|
+
|
|
21
|
+
Register the plugin in your app's `MainActivity`:
|
|
22
|
+
|
|
23
|
+
```java
|
|
24
|
+
import kr.co.simplysm.capacitor.filesystem.FileSystemPlugin;
|
|
25
|
+
|
|
26
|
+
public class MainActivity extends BridgeActivity {
|
|
27
|
+
@Override
|
|
28
|
+
protected void init(Bundle savedInstanceState) {
|
|
29
|
+
super.init(savedInstanceState);
|
|
30
|
+
registerPlugin(FileSystemPlugin.class);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The plugin's `AndroidManifest.xml` automatically merges the required permissions and `FileProvider` configuration.
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
All methods are static on the `FileSystem` class. Each returns a `Promise`.
|
|
40
|
+
|
|
41
|
+
### `FileSystem.checkPermissionAsync()`
|
|
42
|
+
|
|
43
|
+
Checks whether file system permissions are granted.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { FileSystem } from "@simplysm/capacitor-plugin-file-system";
|
|
47
|
+
|
|
48
|
+
const granted = await FileSystem.checkPermissionAsync();
|
|
49
|
+
// granted: boolean
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### `FileSystem.requestPermissionAsync()`
|
|
53
|
+
|
|
54
|
+
Requests file system permissions from the user.
|
|
55
|
+
|
|
56
|
+
- **Android 11+**: Opens the system settings screen for all-files access
|
|
57
|
+
- **Android 10 and below**: Shows the standard permission dialog
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
await FileSystem.requestPermissionAsync();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### `FileSystem.readdirAsync(dirPath)`
|
|
64
|
+
|
|
65
|
+
Lists files and directories in the given directory.
|
|
66
|
+
|
|
67
|
+
| Parameter | Type | Description |
|
|
68
|
+
|-----------|----------|--------------------------|
|
|
69
|
+
| `dirPath` | `string` | Absolute path to the directory |
|
|
70
|
+
|
|
71
|
+
**Returns**: `IFileInfo[]`
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const files = await FileSystem.readdirAsync("/storage/emulated/0/Documents");
|
|
75
|
+
// files: Array<{ name: string; isDirectory: boolean }>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `FileSystem.getStoragePathAsync(type)`
|
|
79
|
+
|
|
80
|
+
Returns the absolute path for a storage location.
|
|
81
|
+
|
|
82
|
+
| Parameter | Type | Description |
|
|
83
|
+
|-----------|------------|----------------|
|
|
84
|
+
| `type` | `TStorage` | Storage type |
|
|
85
|
+
|
|
86
|
+
**`TStorage` values**:
|
|
87
|
+
|
|
88
|
+
| Value | Description |
|
|
89
|
+
|------------------|----------------------------------------------------------|
|
|
90
|
+
| `"external"` | External storage root (`Environment.getExternalStorageDirectory`) |
|
|
91
|
+
| `"externalFiles"`| App-specific external files directory |
|
|
92
|
+
| `"externalCache"`| App-specific external cache directory |
|
|
93
|
+
| `"externalMedia"`| App-specific external media directory |
|
|
94
|
+
| `"appData"` | App data directory |
|
|
95
|
+
| `"appFiles"` | App files directory |
|
|
96
|
+
| `"appCache"` | App cache directory |
|
|
97
|
+
|
|
98
|
+
**Returns**: `string` (absolute path)
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const extPath = await FileSystem.getStoragePathAsync("external");
|
|
102
|
+
// e.g. "/storage/emulated/0"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `FileSystem.getFileUriAsync(filePath)`
|
|
106
|
+
|
|
107
|
+
Returns a content URI for the file via `FileProvider` (Android) or a blob URL (web).
|
|
108
|
+
|
|
109
|
+
| Parameter | Type | Description |
|
|
110
|
+
|------------|----------|-------------------------|
|
|
111
|
+
| `filePath` | `string` | Absolute path to the file |
|
|
112
|
+
|
|
113
|
+
**Returns**: `string` (URI)
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const uri = await FileSystem.getFileUriAsync("/storage/emulated/0/Documents/report.pdf");
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### `FileSystem.writeFileAsync(filePath, data)`
|
|
120
|
+
|
|
121
|
+
Writes data to a file. Creates parent directories automatically.
|
|
122
|
+
|
|
123
|
+
| Parameter | Type | Description |
|
|
124
|
+
|------------|--------------------|-------------------------------------|
|
|
125
|
+
| `filePath` | `string` | Absolute path to the file |
|
|
126
|
+
| `data` | `string \| Buffer` | Content to write (UTF-8 string or Buffer) |
|
|
127
|
+
|
|
128
|
+
When `data` is a `Buffer`, it is encoded as base64 for transfer. When `data` is a `string`, it is written as UTF-8.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
// Write text
|
|
132
|
+
await FileSystem.writeFileAsync("/path/to/file.txt", "Hello, world!");
|
|
133
|
+
|
|
134
|
+
// Write binary
|
|
135
|
+
const buf = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
136
|
+
await FileSystem.writeFileAsync("/path/to/file.bin", buf);
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### `FileSystem.readFileStringAsync(filePath)`
|
|
140
|
+
|
|
141
|
+
Reads a file as a UTF-8 string.
|
|
142
|
+
|
|
143
|
+
| Parameter | Type | Description |
|
|
144
|
+
|------------|----------|-------------------------|
|
|
145
|
+
| `filePath` | `string` | Absolute path to the file |
|
|
146
|
+
|
|
147
|
+
**Returns**: `string`
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const content = await FileSystem.readFileStringAsync("/path/to/file.txt");
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### `FileSystem.readFileBufferAsync(filePath)`
|
|
154
|
+
|
|
155
|
+
Reads a file as a `Buffer`.
|
|
156
|
+
|
|
157
|
+
| Parameter | Type | Description |
|
|
158
|
+
|------------|----------|-------------------------|
|
|
159
|
+
| `filePath` | `string` | Absolute path to the file |
|
|
160
|
+
|
|
161
|
+
**Returns**: `Buffer`
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
const buf = await FileSystem.readFileBufferAsync("/path/to/file.bin");
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### `FileSystem.removeAsync(targetPath)`
|
|
168
|
+
|
|
169
|
+
Deletes a file or directory recursively.
|
|
170
|
+
|
|
171
|
+
| Parameter | Type | Description |
|
|
172
|
+
|--------------|----------|----------------------------------|
|
|
173
|
+
| `targetPath` | `string` | Absolute path to file or directory |
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
await FileSystem.removeAsync("/path/to/dir");
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### `FileSystem.mkdirsAsync(targetPath)`
|
|
180
|
+
|
|
181
|
+
Creates a directory and all parent directories recursively.
|
|
182
|
+
|
|
183
|
+
| Parameter | Type | Description |
|
|
184
|
+
|--------------|----------|------------------------------|
|
|
185
|
+
| `targetPath` | `string` | Absolute path to the directory |
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
await FileSystem.mkdirsAsync("/path/to/new/dir");
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### `FileSystem.existsAsync(targetPath)`
|
|
192
|
+
|
|
193
|
+
Checks whether a file or directory exists.
|
|
194
|
+
|
|
195
|
+
| Parameter | Type | Description |
|
|
196
|
+
|--------------|----------|----------------------------------|
|
|
197
|
+
| `targetPath` | `string` | Absolute path to file or directory |
|
|
198
|
+
|
|
199
|
+
**Returns**: `boolean`
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
const exists = await FileSystem.existsAsync("/path/to/file.txt");
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Types
|
|
206
|
+
|
|
207
|
+
### `TStorage`
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
type TStorage =
|
|
211
|
+
| "external"
|
|
212
|
+
| "externalFiles"
|
|
213
|
+
| "externalCache"
|
|
214
|
+
| "externalMedia"
|
|
215
|
+
| "appData"
|
|
216
|
+
| "appFiles"
|
|
217
|
+
| "appCache";
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### `IFileInfo`
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
interface IFileInfo {
|
|
224
|
+
name: string;
|
|
225
|
+
isDirectory: boolean;
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### `IFileSystemPlugin`
|
|
230
|
+
|
|
231
|
+
Low-level plugin interface registered with Capacitor. Use the `FileSystem` static class instead for a simpler API.
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
interface IFileSystemPlugin {
|
|
235
|
+
checkPermission(): Promise<{ granted: boolean }>;
|
|
236
|
+
requestPermission(): Promise<void>;
|
|
237
|
+
readdir(options: { path: string }): Promise<{ files: IFileInfo[] }>;
|
|
238
|
+
getStoragePath(options: { type: TStorage }): Promise<{ path: string }>;
|
|
239
|
+
getFileUri(options: { path: string }): Promise<{ uri: string }>;
|
|
240
|
+
writeFile(options: { path: string; data: string; encoding?: "utf8" | "base64" }): Promise<void>;
|
|
241
|
+
readFile(options: { path: string; encoding?: "utf8" | "base64" }): Promise<{ data: string }>;
|
|
242
|
+
remove(options: { path: string }): Promise<void>;
|
|
243
|
+
mkdir(options: { path: string }): Promise<void>;
|
|
244
|
+
exists(options: { path: string }): Promise<{ exists: boolean }>;
|
|
245
|
+
}
|
|
246
|
+
```
|