Skip to content

Commit b320079

Browse files
committed
feature: enable excluding file from backup
1 parent da235d4 commit b320079

File tree

8 files changed

+87
-0
lines changed

8 files changed

+87
-0
lines changed

packages/capacitor-plugin/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ const readFilePath = async () => {
191191
* [`stat(...)`](#stat)
192192
* [`rename(...)`](#rename)
193193
* [`copy(...)`](#copy)
194+
* [`excludeFromBackup(...)`](#excludefrombackup)
194195
* [`downloadFile(...)`](#downloadfile)
195196
* [`addListener('progress', ...)`](#addlistenerprogress-)
196197
* [`removeAllListeners()`](#removealllisteners)
@@ -461,6 +462,23 @@ Copy a file or directory
461462
--------------------
462463

463464

465+
### excludeFromBackup(...)
466+
467+
```typescript
468+
excludeFromBackup(options: ExcludeFromBackupOptions) => Promise<void>
469+
```
470+
471+
Exclude a file or directory from backup (iOS only).
472+
473+
| Param | Type |
474+
| ------------- | ----------------------------------------------------------------------------- |
475+
| **`options`** | <code><a href="#excludefrombackupoptions">ExcludeFromBackupOptions</a></code> |
476+
477+
**Since:** 7.2.0
478+
479+
--------------------
480+
481+
464482
### downloadFile(...)
465483

466484
```typescript
@@ -676,6 +694,15 @@ We recommend using the @capacitor/file-transfer plugin instead, in conjunction w
676694
| **`uri`** | <code>string</code> | The uri where the file was copied into | 4.0.0 |
677695

678696

697+
#### ExcludeFromBackupOptions
698+
699+
| Prop | Type | Description | Default | Since |
700+
| --------------- | ----------------------------------------------- | ------------------------------------------------------------------------------ | ------------------ | ----- |
701+
| **`path`** | <code>string</code> | The path of the file or directory to exclude from backup | | 7.2.0 |
702+
| **`directory`** | <code><a href="#directory">Directory</a></code> | The <a href="#directory">`Directory`</a> to exclude the file or directory from | | 7.2.0 |
703+
| **`excluded`** | <code>boolean</code> | Whether to exclude the file or directory from backup | <code>false</code> | 7.2.0 |
704+
705+
679706
#### DownloadFileResult
680707

681708
| Prop | Type | Description | Since |

packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemConstants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct Constants {
2525
static let data = "data"
2626
static let directory = "directory"
2727
static let encoding = "encoding"
28+
static let excluded = "excluded"
2829
static let chunkSize = "chunkSize"
2930
static let from = "from"
3031
static let path = "path"

packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum IONFileMethod: String {
1111
case getUri
1212
case rename
1313
case copy
14+
case excludeFromBackup
1415
}
1516

1617
enum FilesystemError: Error {

packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ enum FilesystemOperation {
2121
case delete(url: URL)
2222
case rename(source: URL, destination: URL)
2323
case copy(source: URL, destination: URL)
24+
case excludeFromBackup(url: URL, excluded: Bool)
2425
}

packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class FilesystemOperationExecutor {
4646
case .copy(let source, let destination):
4747
try service.copyItem(fromURL: source, toURL: destination)
4848
resultData = [Constants.ResultDataKey.uri: destination.absoluteString]
49+
case .excludeFromBackup(var url, let excluded):
50+
var values = URLResourceValues()
51+
values.isExcludedFromBackup = excluded
52+
try url.setResourceValues(values)
4953
}
5054

5155
call.handleSuccess(resultData)
@@ -94,6 +98,7 @@ private extension FilesystemOperationExecutor {
9498
case .getUri(let url): return FilesystemError.invalidPath(url.absoluteString)
9599
case .rename(let sourceUrl, _): path = sourceUrl.absoluteString; method = .rename
96100
case .copy(let sourceUrl, _): path = sourceUrl.absoluteString; method = .copy
101+
case .excludeFromBackup(let url, _): path = url.absoluteString; method = .excludeFromBackup
97102
}
98103

99104
return mapError(error, withPath: path, andMethod: method)

packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class FilesystemPlugin: CAPPlugin, CAPBridgedPlugin {
2525
CAPPluginMethod(name: "stat", returnType: CAPPluginReturnPromise),
2626
CAPPluginMethod(name: "rename", returnType: CAPPluginReturnPromise),
2727
CAPPluginMethod(name: "copy", returnType: CAPPluginReturnPromise),
28+
CAPPluginMethod(name: "excludeFromBackup", returnType: CAPPluginReturnPromise),
2829
CAPPluginMethod(name: "checkPermissions", returnType: CAPPluginReturnPromise),
2930
CAPPluginMethod(name: "requestPermissions", returnType: CAPPluginReturnPromise),
3031
CAPPluginMethod(name: "downloadFile", returnType: CAPPluginReturnPromise)
@@ -172,6 +173,16 @@ private extension FilesystemPlugin {
172173
}
173174
}
174175

176+
/**
177+
* Excludes a file or directory from backup.
178+
*/
179+
@objc func excludeFromBackup(_ call: CAPPluginCall) {
180+
let excluded = call.getBool(Constants.MethodParameter.excluded, false)
181+
performSinglePathOperation(call) {
182+
.excludeFromBackup(url: $0, excluded: excluded)
183+
}
184+
}
185+
175186
/**
176187
* [DEPRECATED] Download a file
177188
*/

packages/capacitor-plugin/src/definitions.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ export interface CopyOptions {
385385

386386
export type RenameOptions = CopyOptions;
387387

388+
export interface ExcludeFromBackupOptions {
389+
/**
390+
* The path of the file or directory to exclude from backup
391+
*
392+
* @since 7.2.0
393+
*/
394+
path: string;
395+
396+
/**
397+
* The `Directory` to exclude the file or directory from
398+
*
399+
* @since 7.2.0
400+
*/
401+
directory?: Directory;
402+
403+
/**
404+
* Whether to exclude the file or directory from backup
405+
*
406+
* @default false
407+
* @since 7.2.0
408+
*/
409+
excluded: boolean;
410+
}
411+
388412
export interface ReadFileResult {
389413
/**
390414
* The representation of the data contained in the file
@@ -668,6 +692,13 @@ export interface FilesystemPlugin {
668692
*/
669693
copy(options: CopyOptions): Promise<CopyResult>;
670694

695+
/**
696+
* Exclude a file or directory from backup (iOS only).
697+
*
698+
* @since 7.2.0
699+
*/
700+
excludeFromBackup(options: ExcludeFromBackupOptions): Promise<void>;
701+
671702
/**
672703
* Perform a http request to a server and download the file to the specified destination.
673704
*

packages/capacitor-plugin/src/web.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
DownloadFileResult,
2828
ProgressStatus,
2929
ReadFileInChunksCallback,
30+
ExcludeFromBackupOptions,
3031
} from './definitions';
3132
import { Encoding } from './definitions';
3233

@@ -438,6 +439,15 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
438439
return this._copy(options, false);
439440
}
440441

442+
/**
443+
* Exclude a file or directory from backup (iOS only).
444+
* @param options the options for the exclude operation
445+
* @return a promise that resolves with the exclude result
446+
*/
447+
async excludeFromBackup(_: ExcludeFromBackupOptions): Promise<void> {
448+
throw this.unavailable('Method not implemented.');
449+
}
450+
441451
async requestPermissions(): Promise<PermissionStatus> {
442452
return { publicStorage: 'granted' };
443453
}

0 commit comments

Comments
 (0)