Skip to content

Commit

Permalink
Merge pull request #509 from martenrebane/MOPPIOS-1443
Browse files Browse the repository at this point in the history
Move files from Documents to Cache folder
  • Loading branch information
Counter178 authored Sep 18, 2024
2 parents d4ca766 + 3c96122 commit 1a7264e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
81 changes: 81 additions & 0 deletions MoppApp/MoppApp/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -355,6 +364,78 @@ class MoppFileManager {
return true
}

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 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
}

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)")
}
}
}

func renameFile(withPath sourcePath: URL, toPath destinationPath: URL) -> Bool {
do {
guard let newUrl: URL = try fileManager.replaceItemAt(sourcePath, withItemAt: destinationPath), fileExists(newUrl.path) else {
Expand Down
11 changes: 11 additions & 0 deletions MoppApp/MoppApp/MoppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate {

FileLogUtil.setupAppLogging()

moveRecentFilesToCacheFolder()

// Get remote configuration
SettingsConfiguration().getCentralConfiguration()

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1a7264e

Please sign in to comment.