From e31c9a29770aceb10f7d8dbb803035bac79ef5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Juan=20Fern=C3=A1ndez=20Cotrina?= Date: Fri, 29 Aug 2025 23:55:01 +0200 Subject: [PATCH] feature: enable excluding file from backup --- packages/capacitor-plugin/README.md | 27 ++++++++++++++++ .../FilesystemConstants.swift | 1 + .../FilesystemPlugin/FilesystemError.swift | 1 + .../FilesystemOperation.swift | 1 + .../FilesystemOperationExecutor.swift | 5 +++ .../FilesystemPlugin/FilesystemPlugin.swift | 11 +++++++ packages/capacitor-plugin/src/definitions.ts | 31 +++++++++++++++++++ packages/capacitor-plugin/src/web.ts | 10 ++++++ 8 files changed, 87 insertions(+) diff --git a/packages/capacitor-plugin/README.md b/packages/capacitor-plugin/README.md index 8c620cb..2d4454e 100644 --- a/packages/capacitor-plugin/README.md +++ b/packages/capacitor-plugin/README.md @@ -191,6 +191,7 @@ const readFilePath = async () => { * [`stat(...)`](#stat) * [`rename(...)`](#rename) * [`copy(...)`](#copy) +* [`excludeFromBackup(...)`](#excludefrombackup) * [`downloadFile(...)`](#downloadfile) * [`addListener('progress', ...)`](#addlistenerprogress-) * [`removeAllListeners()`](#removealllisteners) @@ -461,6 +462,23 @@ Copy a file or directory -------------------- +### excludeFromBackup(...) + +```typescript +excludeFromBackup(options: ExcludeFromBackupOptions) => Promise +``` + +Exclude a file or directory from backup (iOS only). + +| Param | Type | +| ------------- | ----------------------------------------------------------------------------- | +| **`options`** | ExcludeFromBackupOptions | + +**Since:** 7.2.0 + +-------------------- + + ### downloadFile(...) ```typescript @@ -676,6 +694,15 @@ We recommend using the @capacitor/file-transfer plugin instead, in conjunction w | **`uri`** | string | The uri where the file was copied into | 4.0.0 | +#### ExcludeFromBackupOptions + +| Prop | Type | Description | Default | Since | +| --------------- | ----------------------------------------------- | ------------------------------------------------------------------------------ | ------------------ | ----- | +| **`path`** | string | The path of the file or directory to exclude from backup | | 7.2.0 | +| **`directory`** | Directory | The `Directory` to exclude the file or directory from | | 7.2.0 | +| **`excluded`** | boolean | Whether to exclude the file or directory from backup | false | 7.2.0 | + + #### DownloadFileResult | Prop | Type | Description | Since | diff --git a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemConstants.swift b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemConstants.swift index 909e290..a630e9c 100644 --- a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemConstants.swift +++ b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemConstants.swift @@ -25,6 +25,7 @@ struct Constants { static let data = "data" static let directory = "directory" static let encoding = "encoding" + static let excluded = "excluded" static let chunkSize = "chunkSize" static let from = "from" static let path = "path" diff --git a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemError.swift b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemError.swift index 030e336..05faf5d 100644 --- a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemError.swift +++ b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemError.swift @@ -11,6 +11,7 @@ enum IONFileMethod: String { case getUri case rename case copy + case excludeFromBackup } enum FilesystemError: Error { diff --git a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperation.swift b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperation.swift index 238420f..7bb4b68 100644 --- a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperation.swift +++ b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperation.swift @@ -21,4 +21,5 @@ enum FilesystemOperation { case delete(url: URL) case rename(source: URL, destination: URL) case copy(source: URL, destination: URL) + case excludeFromBackup(url: URL, excluded: Bool) } diff --git a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift index afcf637..32bca1a 100644 --- a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift +++ b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemOperationExecutor.swift @@ -46,6 +46,10 @@ class FilesystemOperationExecutor { case .copy(let source, let destination): try service.copyItem(fromURL: source, toURL: destination) resultData = [Constants.ResultDataKey.uri: destination.absoluteString] + case .excludeFromBackup(var url, let excluded): + var values = URLResourceValues() + values.isExcludedFromBackup = excluded + try url.setResourceValues(values) } call.handleSuccess(resultData) @@ -94,6 +98,7 @@ private extension FilesystemOperationExecutor { case .getUri(let url): return FilesystemError.invalidPath(url.absoluteString) case .rename(let sourceUrl, _): path = sourceUrl.absoluteString; method = .rename case .copy(let sourceUrl, _): path = sourceUrl.absoluteString; method = .copy + case .excludeFromBackup(let url, _): path = url.absoluteString; method = .excludeFromBackup } return mapError(error, withPath: path, andMethod: method) diff --git a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift index 763601a..8bf036d 100644 --- a/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift +++ b/packages/capacitor-plugin/ios/Sources/FilesystemPlugin/FilesystemPlugin.swift @@ -25,6 +25,7 @@ public class FilesystemPlugin: CAPPlugin, CAPBridgedPlugin { CAPPluginMethod(name: "stat", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "rename", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "copy", returnType: CAPPluginReturnPromise), + CAPPluginMethod(name: "excludeFromBackup", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "checkPermissions", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "requestPermissions", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "downloadFile", returnType: CAPPluginReturnPromise) @@ -172,6 +173,16 @@ private extension FilesystemPlugin { } } + /** + * Excludes a file or directory from backup. + */ + @objc func excludeFromBackup(_ call: CAPPluginCall) { + let excluded = call.getBool(Constants.MethodParameter.excluded, false) + performSinglePathOperation(call) { + .excludeFromBackup(url: $0, excluded: excluded) + } + } + /** * [DEPRECATED] Download a file */ diff --git a/packages/capacitor-plugin/src/definitions.ts b/packages/capacitor-plugin/src/definitions.ts index d7834c3..8ce6cb1 100644 --- a/packages/capacitor-plugin/src/definitions.ts +++ b/packages/capacitor-plugin/src/definitions.ts @@ -385,6 +385,30 @@ export interface CopyOptions { export type RenameOptions = CopyOptions; +export interface ExcludeFromBackupOptions { + /** + * The path of the file or directory to exclude from backup + * + * @since 7.2.0 + */ + path: string; + + /** + * The `Directory` to exclude the file or directory from + * + * @since 7.2.0 + */ + directory?: Directory; + + /** + * Whether to exclude the file or directory from backup + * + * @default false + * @since 7.2.0 + */ + excluded: boolean; +} + export interface ReadFileResult { /** * The representation of the data contained in the file @@ -668,6 +692,13 @@ export interface FilesystemPlugin { */ copy(options: CopyOptions): Promise; + /** + * Exclude a file or directory from backup (iOS only). + * + * @since 7.2.0 + */ + excludeFromBackup(options: ExcludeFromBackupOptions): Promise; + /** * Perform a http request to a server and download the file to the specified destination. * diff --git a/packages/capacitor-plugin/src/web.ts b/packages/capacitor-plugin/src/web.ts index 9530dc2..00c64d5 100644 --- a/packages/capacitor-plugin/src/web.ts +++ b/packages/capacitor-plugin/src/web.ts @@ -27,6 +27,7 @@ import type { DownloadFileResult, ProgressStatus, ReadFileInChunksCallback, + ExcludeFromBackupOptions, } from './definitions'; import { Encoding } from './definitions'; @@ -438,6 +439,15 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { return this._copy(options, false); } + /** + * Exclude a file or directory from backup (iOS only). + * @param options the options for the exclude operation + * @return a promise that resolves with the exclude result + */ + async excludeFromBackup(_: ExcludeFromBackupOptions): Promise { + throw this.unavailable('Method not implemented.'); + } + async requestPermissions(): Promise { return { publicStorage: 'granted' }; }