Skip to content

Commit 3980e7e

Browse files
committed
Only clean up files in the docs directory
1 parent a7c4138 commit 3980e7e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Modules/Sources/Networking/Remote/POSCatalogSyncRemote.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,13 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
214214
/// - fileURL: Local file URL of the downloaded catalog.
215215
/// - siteID: Site ID for proper mapping.
216216
/// - Returns: Parsed POS catalog.
217-
private func parseDownloadedCatalog(from fileURL: URL, siteID: Int64) async throws -> POSCatalogResponse {
217+
func parseDownloadedCatalog(from fileURL: URL, siteID: Int64) async throws -> POSCatalogResponse {
218218
let data = try Data(contentsOf: fileURL)
219219

220-
// Clean up the downloaded file after reading.
221-
// Background downloads are moved to temporary directory by BackgroundDownloadService,
222-
// so we always clean them up after parsing.
220+
// Clean up downloaded files, but only if they're in our Documents directory.
221+
// Files in iOS temporary directories should be left for iOS to clean up automatically.
223222
defer {
224-
try? fileManager.removeItem(at: fileURL)
223+
cleanupDownloadedFileIfNeeded(at: fileURL)
225224
}
226225

227226
let mapper = ListMapper<POSProduct>(siteID: siteID)
@@ -233,6 +232,21 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
233232
return POSCatalogResponse(products: products, variations: variations)
234233
}
235234

235+
/// Cleans up the downloaded catalog file if it's in our Documents directory.
236+
/// Files in temporary directories are left for iOS to clean up automatically.
237+
private func cleanupDownloadedFileIfNeeded(at fileURL: URL) {
238+
// Only clean up files in our Documents directory
239+
// Temporary files should be left for iOS to handle
240+
let documentsURLs = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
241+
guard let documentsURL = documentsURLs.first,
242+
fileURL.path.hasPrefix(documentsURL.path),
243+
fileManager.fileExists(atPath: fileURL.path) else {
244+
return
245+
}
246+
247+
try? fileManager.removeItem(at: fileURL)
248+
}
249+
236250
/// Loads POS products for full sync.
237251
///
238252
/// - Parameters:

0 commit comments

Comments
 (0)