Only use the appGroupTemporaryDirectory to access a file from the share extension. (#4002)

… and switch back to the plain `URL.temporaryDirectory` for everything else.

* Fix documentation

Co-authored-by: Stefan Ceriu <stefan.ceriu@gmail.com>
This commit is contained in:
Doug
2025-04-10 09:56:25 +01:00
committed by GitHub
parent 58a0709a1b
commit 9ba4048462
6 changed files with 99 additions and 27 deletions
@@ -125,6 +125,9 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
if let previousVersion = appSettings.lastVersionLaunched.flatMap(Version.init) {
performMigrationsIfNecessary(from: previousVersion, to: currentVersion)
// Manual clean to handle the potential case where the app crashes before moving a shared file.
cleanAppGroupTemporaryDirectory()
} else {
// The app has been deleted since the previous run. Reset everything.
wipeUserData(includingSettings: true)
@@ -252,12 +255,17 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
} else {
handleAppRoute(.childEventOnRoomAlias(eventID: eventID, alias: alias))
}
case .share:
case .share(let payload):
guard isExternalURL else {
MXLog.error("Received unexpected internal share route")
break
}
handleAppRoute(route)
do {
try handleAppRoute(.share(payload.withDefaultTemporaryDirectory()))
} catch {
MXLog.error("Failed moving payload out of the app group container: \(error)")
}
default:
break
}
@@ -408,6 +416,31 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
userSessionStore.reset()
}
/// Manually cleans up any files in the app group's `tmp` directory.
///
/// **Note:** If there is a single file we consider it to be an active share payload and ignore it.
private func cleanAppGroupTemporaryDirectory() {
let fileURLs: [URL]
do {
fileURLs = try FileManager.default.contentsOfDirectory(at: URL.appGroupTemporaryDirectory, includingPropertiesForKeys: nil, options: [])
} catch {
MXLog.warning("Failed to enumerate app group temporary directory: \(error)")
return
}
guard fileURLs.count > 1 else {
return // If there is only a single item in here, there's likely a pending share payload that is yet to be processed.
}
for url in fileURLs {
do {
try FileManager.default.removeItem(at: url)
} catch {
MXLog.warning("Failed to remove file from app group temporary directory: \(error)")
}
}
}
private func setupStateMachine() {
stateMachine.addTransitionHandler { [weak self] context in
guard let self else { return }