@simplysm/capacitor-plugin-file-system 12.16.31 → 12.16.36
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 +129 -201
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,213 +1,30 @@
|
|
|
1
1
|
# @simplysm/capacitor-plugin-file-system
|
|
2
2
|
|
|
3
|
-
Capacitor
|
|
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
|
|
3
|
+
Capacitor File System Plugin -- Full file system access with Android permission management. Supports reading/writing files, directory operations, storage path retrieval, and FileProvider URI generation. Uses `MANAGE_EXTERNAL_STORAGE` on Android 11+ and `READ/WRITE_EXTERNAL_STORAGE` on earlier versions. Falls back to IndexedDB on browser.
|
|
8
4
|
|
|
9
5
|
## Installation
|
|
10
6
|
|
|
11
7
|
```bash
|
|
12
8
|
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
9
|
```
|
|
190
10
|
|
|
191
|
-
|
|
11
|
+
## API Overview
|
|
192
12
|
|
|
193
|
-
|
|
13
|
+
| API | Type | Description |
|
|
14
|
+
|-----|------|-------------|
|
|
15
|
+
| `FileSystem` | Abstract class | Static methods for file system operations with permission management |
|
|
16
|
+
| `TStorage` | Type alias | Union type of storage location identifiers |
|
|
17
|
+
| `IFileInfo` | Interface | File/directory entry information |
|
|
18
|
+
| `IFileSystemPlugin` | Interface | Low-level Capacitor plugin interface for file system operations |
|
|
194
19
|
|
|
195
|
-
|
|
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
|
|
20
|
+
## API Reference
|
|
206
21
|
|
|
207
22
|
### `TStorage`
|
|
208
23
|
|
|
209
|
-
|
|
210
|
-
|
|
24
|
+
Union type representing available storage locations.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
export type TStorage =
|
|
211
28
|
| "external"
|
|
212
29
|
| "externalFiles"
|
|
213
30
|
| "externalCache"
|
|
@@ -217,21 +34,38 @@ type TStorage =
|
|
|
217
34
|
| "appCache";
|
|
218
35
|
```
|
|
219
36
|
|
|
37
|
+
| Value | Description |
|
|
38
|
+
|-------|-------------|
|
|
39
|
+
| `"external"` | External storage root (`Environment.getExternalStorageDirectory`) |
|
|
40
|
+
| `"externalFiles"` | App-specific external files directory |
|
|
41
|
+
| `"externalCache"` | App-specific external cache directory |
|
|
42
|
+
| `"externalMedia"` | App-specific external media directory |
|
|
43
|
+
| `"appData"` | App data directory |
|
|
44
|
+
| `"appFiles"` | App files directory |
|
|
45
|
+
| `"appCache"` | App cache directory |
|
|
46
|
+
|
|
220
47
|
### `IFileInfo`
|
|
221
48
|
|
|
222
|
-
|
|
223
|
-
|
|
49
|
+
File or directory entry information returned by directory listing.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
export interface IFileInfo {
|
|
224
53
|
name: string;
|
|
225
54
|
isDirectory: boolean;
|
|
226
55
|
}
|
|
227
56
|
```
|
|
228
57
|
|
|
58
|
+
| Field | Type | Description |
|
|
59
|
+
|-------|------|-------------|
|
|
60
|
+
| `name` | `string` | Name of the file or directory |
|
|
61
|
+
| `isDirectory` | `boolean` | `true` if the entry is a directory, `false` if a file |
|
|
62
|
+
|
|
229
63
|
### `IFileSystemPlugin`
|
|
230
64
|
|
|
231
|
-
Low-level plugin interface
|
|
65
|
+
Low-level Capacitor plugin interface. Use `FileSystem` instead for a simplified API.
|
|
232
66
|
|
|
233
|
-
```
|
|
234
|
-
interface IFileSystemPlugin {
|
|
67
|
+
```typescript
|
|
68
|
+
export interface IFileSystemPlugin {
|
|
235
69
|
checkPermission(): Promise<{ granted: boolean }>;
|
|
236
70
|
requestPermission(): Promise<void>;
|
|
237
71
|
readdir(options: { path: string }): Promise<{ files: IFileInfo[] }>;
|
|
@@ -244,3 +78,97 @@ interface IFileSystemPlugin {
|
|
|
244
78
|
exists(options: { path: string }): Promise<{ exists: boolean }>;
|
|
245
79
|
}
|
|
246
80
|
```
|
|
81
|
+
|
|
82
|
+
| Method | Parameters | Returns | Description |
|
|
83
|
+
|--------|------------|---------|-------------|
|
|
84
|
+
| `checkPermission` | -- | `Promise<{ granted: boolean }>` | Check file system permission |
|
|
85
|
+
| `requestPermission` | -- | `Promise<void>` | Request file system permission |
|
|
86
|
+
| `readdir` | `options: { path }` | `Promise<{ files: IFileInfo[] }>` | List directory contents |
|
|
87
|
+
| `getStoragePath` | `options: { type: TStorage }` | `Promise<{ path: string }>` | Get absolute path for a storage type |
|
|
88
|
+
| `getFileUri` | `options: { path }` | `Promise<{ uri: string }>` | Get FileProvider content:// URI |
|
|
89
|
+
| `writeFile` | `options: { path, data, encoding? }` | `Promise<void>` | Write file with utf8 or base64 encoding |
|
|
90
|
+
| `readFile` | `options: { path, encoding? }` | `Promise<{ data: string }>` | Read file as utf8 or base64 string |
|
|
91
|
+
| `remove` | `options: { path }` | `Promise<void>` | Remove file or directory recursively |
|
|
92
|
+
| `mkdir` | `options: { path }` | `Promise<void>` | Create directory recursively |
|
|
93
|
+
| `exists` | `options: { path }` | `Promise<{ exists: boolean }>` | Check if file or directory exists |
|
|
94
|
+
|
|
95
|
+
### `FileSystem`
|
|
96
|
+
|
|
97
|
+
Abstract class with static methods for full file system access.
|
|
98
|
+
|
|
99
|
+
- **Android 11+**: `MANAGE_EXTERNAL_STORAGE` permission for full file system access
|
|
100
|
+
- **Android 10-**: `READ/WRITE_EXTERNAL_STORAGE` permissions
|
|
101
|
+
- **Browser**: IndexedDB-based emulation
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
export abstract class FileSystem {
|
|
105
|
+
static async checkPermissionAsync(): Promise<boolean>;
|
|
106
|
+
static async requestPermissionAsync(): Promise<void>;
|
|
107
|
+
static async readdirAsync(dirPath: string): Promise<IFileInfo[]>;
|
|
108
|
+
static async getStoragePathAsync(type: TStorage): Promise<string>;
|
|
109
|
+
static async getFileUriAsync(filePath: string): Promise<string>;
|
|
110
|
+
static async writeFileAsync(filePath: string, data: string | Buffer): Promise<void>;
|
|
111
|
+
static async readFileStringAsync(filePath: string): Promise<string>;
|
|
112
|
+
static async readFileBufferAsync(filePath: string): Promise<Buffer>;
|
|
113
|
+
static async removeAsync(targetPath: string): Promise<void>;
|
|
114
|
+
static async mkdirsAsync(targetPath: string): Promise<void>;
|
|
115
|
+
static async existsAsync(targetPath: string): Promise<boolean>;
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
| Method | Parameters | Returns | Description |
|
|
120
|
+
|--------|------------|---------|-------------|
|
|
121
|
+
| `checkPermissionAsync` | -- | `Promise<boolean>` | Check if file system permission is granted |
|
|
122
|
+
| `requestPermissionAsync` | -- | `Promise<void>` | Request file system permission (Android 11+: opens settings; Android 10-: shows dialog) |
|
|
123
|
+
| `readdirAsync` | `dirPath: string` | `Promise<IFileInfo[]>` | List files and directories in a directory |
|
|
124
|
+
| `getStoragePathAsync` | `type: TStorage` | `Promise<string>` | Get the absolute path for the specified storage type |
|
|
125
|
+
| `getFileUriAsync` | `filePath: string` | `Promise<string>` | Get a FileProvider `content://` URI for a file path |
|
|
126
|
+
| `writeFileAsync` | `filePath: string, data: string \| Buffer` | `Promise<void>` | Write a file; `Buffer` data is base64-encoded, `string` is written as UTF-8 |
|
|
127
|
+
| `readFileStringAsync` | `filePath: string` | `Promise<string>` | Read a file as a UTF-8 string |
|
|
128
|
+
| `readFileBufferAsync` | `filePath: string` | `Promise<Buffer>` | Read a file as a Buffer |
|
|
129
|
+
| `removeAsync` | `targetPath: string` | `Promise<void>` | Delete a file or directory recursively |
|
|
130
|
+
| `mkdirsAsync` | `targetPath: string` | `Promise<void>` | Create a directory and all parent directories |
|
|
131
|
+
| `existsAsync` | `targetPath: string` | `Promise<boolean>` | Check whether a file or directory exists |
|
|
132
|
+
|
|
133
|
+
## Usage Examples
|
|
134
|
+
|
|
135
|
+
### Read and write files with permission handling
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { FileSystem } from "@simplysm/capacitor-plugin-file-system";
|
|
139
|
+
|
|
140
|
+
// Ensure permission is granted
|
|
141
|
+
const hasPermission = await FileSystem.checkPermissionAsync();
|
|
142
|
+
if (!hasPermission) {
|
|
143
|
+
await FileSystem.requestPermissionAsync();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Get app cache directory
|
|
147
|
+
const cachePath = await FileSystem.getStoragePathAsync("appCache");
|
|
148
|
+
|
|
149
|
+
// Write a text file
|
|
150
|
+
await FileSystem.writeFileAsync(`${cachePath}/config.json`, '{"key": "value"}');
|
|
151
|
+
|
|
152
|
+
// Read it back
|
|
153
|
+
const content = await FileSystem.readFileStringAsync(`${cachePath}/config.json`);
|
|
154
|
+
|
|
155
|
+
// Write binary data
|
|
156
|
+
const buffer = Buffer.from([0x00, 0x01, 0x02]);
|
|
157
|
+
await FileSystem.writeFileAsync(`${cachePath}/data.bin`, buffer);
|
|
158
|
+
|
|
159
|
+
// Read binary data
|
|
160
|
+
const data = await FileSystem.readFileBufferAsync(`${cachePath}/data.bin`);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### List directory contents
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { FileSystem } from "@simplysm/capacitor-plugin-file-system";
|
|
167
|
+
|
|
168
|
+
const externalPath = await FileSystem.getStoragePathAsync("external");
|
|
169
|
+
const files = await FileSystem.readdirAsync(`${externalPath}/Documents`);
|
|
170
|
+
|
|
171
|
+
for (const file of files) {
|
|
172
|
+
console.log(`${file.name} (${file.isDirectory ? "directory" : "file"})`);
|
|
173
|
+
}
|
|
174
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/capacitor-plugin-file-system",
|
|
3
|
-
"version": "12.16.
|
|
3
|
+
"version": "12.16.36",
|
|
4
4
|
"description": "심플리즘 패키지 - Capacitor File System Plugin",
|
|
5
5
|
"author": "김석래",
|
|
6
6
|
"repository": {
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"@capacitor/core": "^7.4.4"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@capacitor/core": "^7.
|
|
24
|
+
"@capacitor/core": "^7.6.0"
|
|
25
25
|
}
|
|
26
26
|
}
|