marinofaggiana 4 年 前
コミット
e15cbc3077

+ 5 - 0
iOSClient/AppDelegate.swift

@@ -182,6 +182,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             }
         }
         
+        // Clear file
+        if let directory = CCUtility.getDirectoryProviderStorage() {
+            NCUtilityFileSystem.shared.getCleanUp(directory: directory, days: 1)
+        }
+        
         // Passcode
         DispatchQueue.main.async {
             self.passcodeWithAutomaticallyPromptForBiometricValidation(true)

+ 0 - 1
iOSClient/Data/NCDatabase.swift

@@ -307,7 +307,6 @@ 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 = ""

+ 0 - 15
iOSClient/Data/NCManageDatabase.swift

@@ -1696,7 +1696,6 @@ class NCManageDatabase: NSObject {
                 let addObject = tableLocalFile()
                 
                 addObject.account = metadata.account
-                addObject.date = NSDate()
                 addObject.etag = metadata.etag
                 addObject.exifDate = NSDate()
                 addObject.exifLatitude = "-1"
@@ -1797,20 +1796,6 @@ 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               = 179
+    let databaseSchemaVersion: UInt64               = 180
     
     // Intro selector
     //

+ 1 - 0
iOSClient/Utility/CCUtility.h

@@ -264,5 +264,6 @@
 + (NSString *)getExtension:(NSString*)fileName;
 + (NSDate *)datetimeWithOutTime:(NSDate *)datDate;
 + (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems;
++ (NSDate *)getATime:(const char *)path;
 
 @end

+ 10 - 0
iOSClient/Utility/CCUtility.m

@@ -26,6 +26,8 @@
 #import "NSNotificationCenter+MainThread.h"
 #import <OpenSSL/OpenSSL.h>
 #import <CoreLocation/CoreLocation.h>
+#include <sys/stat.h>
+
 
 #define INTRO_MessageType       @"MessageType_"
 
@@ -1831,4 +1833,12 @@
     return queryItem.value;
 }
 
++ (NSDate *)getATime:(const char *)path
+{
+    struct stat st;
+    stat(path, &st);
+    time_t accessed = st.st_atime;
+    NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
+    return date;
+}
 @end

+ 19 - 16
iOSClient/Utility/NCUtilityFileSystem.swift

@@ -244,31 +244,34 @@ class NCUtilityFileSystem: NSObject {
         
         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)
+        
+        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 date = CCUtility.getATime(fileURL.path) {
+                        
+                        let folderURL = fileURL.deletingLastPathComponent()
+                        let ocId = folderURL.lastPathComponent
+                        
+                        if attributes[.size] as? Double == 0 { continue }
+                        if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
+                        if meetsRequirement(date: date) {
+                            do {
+                                try manager.removeItem(atPath: folderURL.path)
+                                NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))
+                            } catch {
+                                print("catch")
                             }
                         }
                     }
-                    print(fileURL.path)
                 }
             }
-        } catch {
-            print("Cannot cleanup the old files: \(error)")
-        }
+        }        
     }
 }