-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// FileClerk.swift | ||
// | ||
// | ||
// Created by Kamaal M Farah on 06/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct FileClerk { | ||
private static let fileManager = FileManager.default | ||
|
||
private init() { } | ||
|
||
public static let libraryDirectory = fileManager.urls(for: .libraryDirectory, in: .userDomainMask).first | ||
|
||
@available(macOS 13.0, *) | ||
public static func moveFolderContent(from sourceFolder: URL, to destinationFolder: URL) throws { | ||
let sourceFolderContent = try fileManager.contentsOfDirectory( | ||
at: sourceFolder, | ||
includingPropertiesForKeys: nil, | ||
options: [] | ||
) | ||
for content in sourceFolderContent { | ||
let destinationURL = destinationFolder.appending(path: content.lastPathComponent) | ||
guard !fileManager.fileExists(atPath: destinationURL.path) else { continue } | ||
|
||
try fileManager.moveItem(at: content, to: destinationURL) | ||
} | ||
} | ||
|
||
public static func deleteItem(at url: URL) throws { | ||
try fileManager.removeItem(at: url) | ||
} | ||
|
||
public static func getOrCreateDirectory(at url: URL) throws -> URL { | ||
guard !fileManager.fileExists(atPath: url.path) else { return url } | ||
|
||
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) | ||
return url | ||
} | ||
|
||
public static func createFile(with data: Data, at url: URL) { | ||
fileManager.createFile(atPath: url.path, contents: data) | ||
} | ||
|
||
@available(macOS 13.0, *) | ||
public static func getOrCreateSubfolders(atBase baseURL: URL, subFolderPaths: [String]) throws -> URL { | ||
var currentPath = baseURL | ||
for subFolderPath in subFolderPaths { | ||
currentPath = try getOrCreateDirectory(at: currentPath.appending(path: subFolderPath)) | ||
} | ||
return currentPath | ||
} | ||
} |