From 107e47170ad296db7f4b8b5ba1669b6a65b079ef Mon Sep 17 00:00:00 2001 From: Marten Rebane <54431068+martenrebane@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:24:08 +0300 Subject: [PATCH 1/2] Move files from Documents to Cache folder --- MoppApp/MoppApp/FileManager.swift | 28 ++++++++++++++++++++++++++++ MoppApp/MoppApp/MoppApp.swift | 11 +++++++++++ 2 files changed, 39 insertions(+) diff --git a/MoppApp/MoppApp/FileManager.swift b/MoppApp/MoppApp/FileManager.swift index b003f767..2c9e476c 100644 --- a/MoppApp/MoppApp/FileManager.swift +++ b/MoppApp/MoppApp/FileManager.swift @@ -36,6 +36,15 @@ class MoppFileManager { } } + static var documentsDirectory: URL { + let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) + if #available(iOS 16.0, *) { + return URL(filePath: paths.first ?? "") + } else { + return URL(fileURLWithPath: paths.first ?? "", isDirectory: true) + } + } + func logsDirectory() -> URL { return MoppFileManager.cacheDirectory.appendingPathComponent("logs") } @@ -352,6 +361,25 @@ class MoppFileManager { return true } + func moveContentsOfDirectory(from sourceURL: URL, to destinationURL: URL) throws { + if !fileManager.fileExists(atPath: destinationURL.path) { + try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil) + } + + let files = try fileManager.contentsOfDirectory(atPath: sourceURL.path) + + for file in files { + let sourceItemURL = sourceURL.appendingPathComponent(file) + let destinationItemURL = destinationURL.appendingPathComponent(file) + + if fileManager.fileExists(atPath: destinationItemURL.path) { + try fileManager.removeItem(at: destinationItemURL) + } + + try fileManager.moveItem(at: sourceItemURL, to: destinationItemURL) + } + } + func renameFile(withPath sourcePath: URL, toPath destinationPath: URL) -> Bool { do { guard let newUrl: URL = try fileManager.replaceItemAt(sourcePath, withItemAt: destinationPath), fileExists(newUrl.path) else { diff --git a/MoppApp/MoppApp/MoppApp.swift b/MoppApp/MoppApp/MoppApp.swift index 0432d915..7097b2cb 100644 --- a/MoppApp/MoppApp/MoppApp.swift +++ b/MoppApp/MoppApp/MoppApp.swift @@ -148,6 +148,8 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate { FileLogUtil.setupAppLogging() + moveRecentFilesToCacheFolder() + // Get remote configuration SettingsConfiguration().getCentralConfiguration() @@ -542,6 +544,15 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate { } topViewController.infoAlert(message: message, dismissCallback: nil) } + + private func moveRecentFilesToCacheFolder() { + do { + try MoppFileManager.shared.moveContentsOfDirectory(from: MoppFileManager.documentsDirectory, to: MoppFileManager.cacheDirectory) + } catch (let error) { + printLog("Unable to copy files from Documents folder to Library/Cache folder: \(error.localizedDescription)") + return + } + } } extension MoppApp { From 3c9612274e660a5f11221ce9a9e34e13466f60c2 Mon Sep 17 00:00:00 2001 From: Marten Rebane <54431068+martenrebane@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:01:52 +0300 Subject: [PATCH 2/2] Folder content moving changes --- MoppApp/MoppApp/FileManager.swift | 71 +++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/MoppApp/MoppApp/FileManager.swift b/MoppApp/MoppApp/FileManager.swift index 2c9e476c..25d2dd37 100644 --- a/MoppApp/MoppApp/FileManager.swift +++ b/MoppApp/MoppApp/FileManager.swift @@ -362,21 +362,74 @@ class MoppFileManager { } func moveContentsOfDirectory(from sourceURL: URL, to destinationURL: URL) throws { + let fileManager = FileManager.default + if !fileManager.fileExists(atPath: destinationURL.path) { try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil) } - - let files = try fileManager.contentsOfDirectory(atPath: sourceURL.path) - for file in files { - let sourceItemURL = sourceURL.appendingPathComponent(file) - let destinationItemURL = destinationURL.appendingPathComponent(file) - - if fileManager.fileExists(atPath: destinationItemURL.path) { - try fileManager.removeItem(at: destinationItemURL) + let items = try fileManager.contentsOfDirectory(atPath: sourceURL.path) + + for item in items { + let sourceItemURL = sourceURL.appendingPathComponent(item) + let destinationItemURL = destinationURL.appendingPathComponent(item) + + sourceURL.startAccessingSecurityScopedResource() + destinationURL.startAccessingSecurityScopedResource() + + if item == "Inbox" { + printLog("Deleting contents from 'Inbox' folder") + try deleteContentsOfDirectory(at: sourceItemURL) + continue } - try fileManager.moveItem(at: sourceItemURL, to: destinationItemURL) + var isDirectory: ObjCBool = false + + if fileManager.fileExists(atPath: sourceItemURL.path, isDirectory: &isDirectory), isDirectory.boolValue { + do { + try moveContentsOfDirectory(from: sourceItemURL, to: destinationItemURL) + } catch { + printLog("Unable to copy directory contents \(sourceItemURL.lastPathComponent): \(error.localizedDescription)") + } + + do { + try fileManager.removeItem(at: sourceItemURL) + } catch { + printLog("Unable to remove directory content \(sourceItemURL.lastPathComponent): \(error.localizedDescription)") + } + } else { + do { + if fileManager.fileExists(atPath: destinationItemURL.path) { + try fileManager.removeItem(at: destinationItemURL) + } + try fileManager.copyItem(at: sourceItemURL, to: destinationItemURL) + } catch { + printLog("Unable to copy file \(sourceItemURL.lastPathComponent): \(error.localizedDescription)") + } + + do { + try fileManager.removeItem(at: sourceItemURL) + } catch { + printLog("Unable to remove file \(sourceItemURL.lastPathComponent): \(error.localizedDescription)") + } + } + + sourceURL.stopAccessingSecurityScopedResource() + destinationURL.stopAccessingSecurityScopedResource() + } + } + + func deleteContentsOfDirectory(at directoryURL: URL) throws { + let items = try fileManager.contentsOfDirectory(atPath: directoryURL.path) + + for item in items { + let itemURL = directoryURL.appendingPathComponent(item) + do { + try fileManager.removeItem(at: itemURL) + printLog("Deleted \(item) from \(directoryURL.lastPathComponent)") + } catch { + printLog("Unable to delete \(item) from \(directoryURL.lastPathComponent): \(error.localizedDescription)") + } } }