marinofaggiana před 3 roky
rodič
revize
9455d9cf7f

+ 1 - 0
iOSClient/Data/NCDatabase.swift

@@ -307,6 +307,7 @@ class tableGPS: Object {
 class tableLocalFile: Object {
     
     @objc dynamic var account = ""
+    @objc dynamic var date: NSDate? = nil
     @objc dynamic var etag = ""
     @objc dynamic var exifDate: NSDate? = nil
     @objc dynamic var exifLatitude = ""

+ 15 - 0
iOSClient/Data/NCManageDatabase.swift

@@ -1696,6 +1696,7 @@ class NCManageDatabase: NSObject {
                 let addObject = tableLocalFile()
                 
                 addObject.account = metadata.account
+                addObject.date = NSDate()
                 addObject.etag = metadata.etag
                 addObject.exifDate = NSDate()
                 addObject.exifLatitude = "-1"
@@ -1796,6 +1797,20 @@ class NCManageDatabase: NSObject {
         }
     }
     
+    @objc func setLocalFileDate(ocId: String) {
+        
+        let realm = try! Realm()
+        
+        do {
+            try realm.safeWrite {
+                let result = realm.objects(tableLocalFile.self).filter("ocId == %@", ocId).first
+                result?.date = NSDate()
+            }
+        } catch let error {
+            NCCommunicationCommon.shared.writeLog("Could not write to database: \(error)")
+        }
+    }
+    
     //MARK: -
     //MARK: Table Metadata
     

+ 1 - 1
iOSClient/NCGlobal.swift

@@ -79,7 +79,7 @@ class NCGlobal: NSObject {
     // Database Realm
     //
     let databaseDefault                             = "nextcloud.realm"
-    let databaseSchemaVersion: UInt64               = 178
+    let databaseSchemaVersion: UInt64               = 179
     
     // Intro selector
     //

+ 31 - 0
iOSClient/Utility/NCUtilityFileSystem.swift

@@ -239,5 +239,36 @@ class NCUtilityFileSystem: NSObject {
         
         return resultFileName
     }
+    
+    func cleanUp(directory: String, days: TimeInterval) {
+        
+        let minimumDate = Date().addingTimeInterval(-days*24*60*60)
+        let url = URL(fileURLWithPath: directory)
+
+        
+        func meetsRequirement(date: Date) -> Bool {
+            return date < minimumDate
+        }
+
+        do {
+            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 let creationDate = attributes[.creationDate] as? Date {
+                            if attributes[.size] as? Double == 0 { continue }
+                            if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
+                            if meetsRequirement(date: creationDate) {
+                                try manager.removeItem(atPath: fileURL.path)
+                            }
+                        }
+                    }
+                    print(fileURL.path)
+                }
+            }
+        } catch {
+            print("Cannot cleanup the old files: \(error)")
+        }
+    }
 }