Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caching image cleanup #3338

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions iOSClient/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
showPrivacyProtectionWindow()
}
}

// Clear older files
let days = NCKeychain().cleanUpDay
let utilityFileSystem = NCUtilityFileSystem()
utilityFileSystem.cleanUp(directory: utilityFileSystem.directoryProviderStorage, days: TimeInterval(days))
}

func sceneDidEnterBackground(_ scene: UIScene) {
Expand Down Expand Up @@ -207,6 +202,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
if NCKeychain().presentPasscode {
showPrivacyProtectionWindow()
}

// Clear older files
let days = NCKeychain().cleanUpDay
let utilityFileSystem = NCUtilityFileSystem()
utilityFileSystem.cleanUp(directory: utilityFileSystem.directoryProviderStorage, days: TimeInterval(days))
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
Expand Down
69 changes: 39 additions & 30 deletions iOSClient/Utility/NCUtilityFileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,37 +615,46 @@ final class NCUtilityFileSystem: NSObject, @unchecked Sendable {
}

func cleanUp(directory: String, days: TimeInterval) {
if days == 0 { return}
let minimumDate = Date().addingTimeInterval(-days * 24 * 60 * 60)
let url = URL(fileURLWithPath: directory)
var offlineDir: [String] = []

if let directories = NCManageDatabase.shared.getTablesDirectory(predicate: NSPredicate(format: "offline == true"), sorted: "serverUrl", ascending: true) {
for directory: tableDirectory in directories {
offlineDir.append(getDirectoryProviderStorageOcId(directory.ocId))
DispatchQueue.global().async {
if days == 0 { return}
let minimumDate = Date().addingTimeInterval(-days * 24 * 60 * 60)
let url = URL(fileURLWithPath: directory)
var offlineDir: [String] = []

if let directories = NCManageDatabase.shared.getTablesDirectory(predicate: NSPredicate(format: "offline == true"), sorted: "serverUrl", ascending: true) {
for directory: tableDirectory in directories {
offlineDir.append(self.getDirectoryProviderStorageOcId(directory.ocId))
}
}
}
let resultsLocalFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "offline == false"), sorted: "lastOpeningDate", ascending: true)

let manager = FileManager.default
if let enumerator = manager.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: []) {
for case let fileURL as URL in enumerator {
if let attributes = try? manager.attributesOfItem(atPath: fileURL.path) {
if attributes[.size] as? Double == 0 { continue }
if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
if fileURL.pathExtension == "ico" { continue }
// check directory offline
let filter = offlineDir.filter({ fileURL.path.hasPrefix($0)})
if !filter.isEmpty { continue }
// -----------------------
let folderURL = fileURL.deletingLastPathComponent()
let ocId = folderURL.lastPathComponent
if let result = resultsLocalFile?.filter({ $0.ocId == ocId }).first, (result.lastOpeningDate as Date) < minimumDate {
do {
try manager.removeItem(atPath: fileURL.path)
} catch { }
manager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
NCManageDatabase.shared.deleteLocalFileOcId(ocId)
let resultsLocalFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "offline == false"), sorted: "lastOpeningDate", ascending: true)

let manager = FileManager.default
if let enumerator = manager.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: []) {
for case let fileURL as URL in enumerator {
if let attributes = try? manager.attributesOfItem(atPath: fileURL.path) {
if attributes[.size] as? Double == 0 { continue }
if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
// check directory offline
let filter = offlineDir.filter({ fileURL.path.hasPrefix($0)})
if !filter.isEmpty { continue }
// -----------------------
if let modificationDate = attributes[.modificationDate] as? Date,
modificationDate < minimumDate {
let fileName = fileURL.lastPathComponent
if fileName.hasSuffix(NCGlobal.shared.previewExt256) || fileName.hasSuffix(NCGlobal.shared.previewExt512) || fileName.hasSuffix(NCGlobal.shared.previewExt1024) {
try? manager.removeItem(atPath: fileURL.path)
}
}
// -----------------------
let folderURL = fileURL.deletingLastPathComponent()
let ocId = folderURL.lastPathComponent
if let result = resultsLocalFile?.filter({ $0.ocId == ocId }).first, (result.lastOpeningDate as Date) < minimumDate {
do {
try manager.removeItem(atPath: fileURL.path)
} catch { }
manager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
NCManageDatabase.shared.deleteLocalFileOcId(ocId)
}
}
}
}
Expand Down
Loading