From 41939fa942c757ca35171806e84da27e42ce0b02 Mon Sep 17 00:00:00 2001 From: Max Ast Date: Mon, 10 Jun 2024 15:40:21 +0200 Subject: [PATCH] add date arg to clearAppGroupContainer to only delete files older than date --- examples/with-media/app/index.tsx | 8 +++++ examples/with-media/package-lock.json | 1 + examples/with-media/package.json | 11 ++++--- ios/ExpoShareExtensionModule.swift | 45 +++++++++++++++++++++++++-- src/index.ts | 6 ++-- 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/examples/with-media/app/index.tsx b/examples/with-media/app/index.tsx index c15521c..840c009 100644 --- a/examples/with-media/app/index.tsx +++ b/examples/with-media/app/index.tsx @@ -1,5 +1,13 @@ +import dayjs from "dayjs"; +import { clearAppGroupContainer } from "expo-share-extension"; import { StyleSheet, Text, View } from "react-native"; +const cleanUpBefore = dayjs().subtract(1, "day").toDate(); + +clearAppGroupContainer(cleanUpBefore).catch((error) => { + console.error(error); +}); + export default function Index() { return ( diff --git a/examples/with-media/package-lock.json b/examples/with-media/package-lock.json index 75bc311..8de6106 100644 --- a/examples/with-media/package-lock.json +++ b/examples/with-media/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "hasInstallScript": true, "dependencies": { + "dayjs": "^1.11.11", "expo": "~51.0.8", "expo-constants": "~16.0.1", "expo-dev-client": "~4.0.14", diff --git a/examples/with-media/package.json b/examples/with-media/package.json index 6aeb115..1f2a3e3 100644 --- a/examples/with-media/package.json +++ b/examples/with-media/package.json @@ -12,20 +12,21 @@ "prebuild": "expo prebuild -p ios --clean" }, "dependencies": { + "dayjs": "^1.11.11", "expo": "~51.0.8", + "expo-constants": "~16.0.1", "expo-dev-client": "~4.0.14", + "expo-file-system": "~17.0.1", + "expo-linking": "~6.3.1", + "expo-router": "~3.5.14", "expo-splash-screen": "~0.27.4", "expo-status-bar": "~1.12.1", "expo-updates": "^0.25.14", "react": "18.2.0", "react-native": "0.74.1", - "zod": "^3.23.8", - "expo-router": "~3.5.14", "react-native-safe-area-context": "4.10.1", "react-native-screens": "3.31.1", - "expo-linking": "~6.3.1", - "expo-constants": "~16.0.1", - "expo-file-system": "~17.0.1" + "zod": "^3.23.8" }, "devDependencies": { "@babel/core": "^7.24.6", diff --git a/ios/ExpoShareExtensionModule.swift b/ios/ExpoShareExtensionModule.swift index 949439a..274a1b1 100644 --- a/ios/ExpoShareExtensionModule.swift +++ b/ios/ExpoShareExtensionModule.swift @@ -13,7 +13,7 @@ public class ExpoShareExtensionModule: Module { NotificationCenter.default.post(name: NSNotification.Name("openHostApp"), object: nil, userInfo: userInfo) } - AsyncFunction("clearAppGroupContainer") { (promise: Promise) in + AsyncFunction("clearAppGroupContainer") { (date: String?, promise: Promise) in DispatchQueue.global(qos: .background).async { guard let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as? String else { DispatchQueue.main.async { @@ -29,6 +29,21 @@ public class ExpoShareExtensionModule: Module { return } + var comparisonDate: Date? = nil + if let isoDateString = date { + let isoFormatter = ISO8601DateFormatter() + // new Date().toISOString() returns fractional seconds, which we need to account for: + isoFormatter.formatOptions.insert(.withFractionalSeconds) + if let date = isoFormatter.date(from: isoDateString) { + comparisonDate = date + } else { + DispatchQueue.main.async { + promise.reject("ERR_INVALID_DATE", "The provided date string is not in a valid ISO 8601 format") + } + return + } + } + let fileManager = FileManager.default let sharedDataUrl = containerUrl.deletingLastPathComponent().appendingPathComponent("sharedData") @@ -37,7 +52,20 @@ public class ExpoShareExtensionModule: Module { let contents = try fileManager.contentsOfDirectory(atPath: sharedDataUrl.path) for item in contents { let itemPath = sharedDataUrl.appendingPathComponent(item).path - try fileManager.removeItem(atPath: itemPath) + if let creationDate = self.getCreationDate(of: itemPath) { + if let comparisonDate = comparisonDate { + if creationDate < comparisonDate { + try fileManager.removeItem(atPath: itemPath) + } + } else { + try fileManager.removeItem(atPath: itemPath) + } + } else { + DispatchQueue.main.async { + promise.reject("ERR_REMOVE_CONTENTS", "Unable to retrieve creation date") + } + return + } } DispatchQueue.main.async { print("sharedData directory contents removed successfully.") @@ -57,4 +85,17 @@ public class ExpoShareExtensionModule: Module { } } } + + internal func getCreationDate(of filePath: String) -> Date? { + let fileManager = FileManager.default + do { + let attributes = try fileManager.attributesOfItem(atPath: filePath) + if let creationDate = attributes[.creationDate] as? Date { + return creationDate + } + } catch { + print("Error getting file attributes: \(error.localizedDescription)") + } + return nil + } } diff --git a/src/index.ts b/src/index.ts index eb8cc38..f4f009e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,8 +8,10 @@ export function openHostApp(path: string): void { return ExpoShareExtensionModule.openHostApp(path); } -export async function clearAppGroupContainer(): Promise { - return await ExpoShareExtensionModule.clearAppGroupContainer(); +export async function clearAppGroupContainer(date?: Date): Promise { + return await ExpoShareExtensionModule.clearAppGroupContainer( + date?.toISOString(), + ); } export interface IExtensionPreprocessingJS {