Skip to content

Instantly share code, notes, and snippets.

@saroar
Last active February 20, 2019 09:05
Show Gist options
  • Select an option

  • Save saroar/6aa5404815409e209df689c1c2f7af8d to your computer and use it in GitHub Desktop.

Select an option

Save saroar/6aa5404815409e209df689c1c2f7af8d to your computer and use it in GitHub Desktop.
enum AppDirectories: String {
case documents = "Documents"
case index = "Index"
case libary = "Library"
case temp = "Temp"
}
protocol AppDirectoryNames{
func documentsDirectoryURL() -> URL
func indexDirectoryURL() -> URL
func libraryDirectoryURL() -> URL
func tempDirectoryURL() -> URL
func getURL(for directory: AppDirectories) -> URL
func buildFullPath(forFileName name: String, isDirectory directory: AppDirectories) -> URL
}
extension AppDirectoryNames{
func documentsDirectoryURL() -> URL{
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
func indexDirectoryURL() -> URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(AppDirectories.index.rawValue)
}
func libraryDirectoryURL() -> URL {
return FileManager.default.urls(for: FileManager.SearchPathDirectory.libraryDirectory, in: .userDomainMask).first!
}
func tempDirectoryURL() -> URL {
return FileManager.default.temporaryDirectory
}
func getURL(for directory: AppDirectories) -> URL {
switch directory {
case .documents:
return documentsDirectoryURL()
case .index:
return indexDirectoryURL()
case .libary:
return libraryDirectoryURL()
case .temp:
return tempDirectoryURL()
}
}
func buildFullPath(forFileName name: String, isDirectory directory: AppDirectories) -> URL {
return getURL(for: directory).appendingPathComponent(name)
}
}
protocol AppFileStatusChecking {
func isWritable(file at: URL) -> Bool
func isReadable(file at: URL) -> Bool
func exists(file at: URL) -> Bool
}
extension AppFileStatusChecking {
func isWritable(file at: URL) -> Bool {
if FileManager.default.isWritableFile(atPath: at.path) {
print(at.path)
return true
} else {
print(at.path)
return false
}
}
func isReadable(file at: URL) -> Bool {
if FileManager.default.isReadableFile(atPath: at.path) {
print(at.path)
return true
} else {
print(at.path)
return false
}
}
func exists(file at: URL) -> Bool {
if FileManager.default.fileExists(atPath: at.path) {
print(at.path)
return true
} else {
print(at.path)
return false
}
}
}
protocol AppFileSystemMetaData {
func list(directory at: URL) -> Bool
func attributes(ofFile atFullPath: URL) -> [FileAttributeKey: Any]
}
extension AppFileSystemMetaData {
func list(directory at: URL) -> Bool {
guard let listing = FileManager.default.contents(atPath: at.path) else {
return false
}
if listing.count > 0 {
print("\n----------------------------")
print("LISTING: \(at.path)")
print("")
for file in listing {
print("File: \(file.description)")
}
print("")
print("----------------------------\n")
return true
} else {
return false
}
}
func attributes(ofFile atFullPath: URL) -> [FileAttributeKey: Any] {
return try! FileManager.default.attributesOfItem(atPath: atFullPath.path)
}
}
protocol AppFileManipulation: AppDirectoryNames {
func writeFile(containing: String, to path: AppDirectories, withName name: String) -> Bool
func readFile(at path: AppDirectories, withName name: String) -> String
func deleteFile(at path: AppDirectories, withName name: String) -> Bool
func renameFile(at path: AppDirectories, withName name: String, oldName: String, newName: String) -> Bool
func moveFile(withName name: String, inDirectory: AppDirectories, toDirectory: AppDirectories) -> Bool
func copyFile(withName name: String, inDirectory: AppDirectories, toDirectory: AppDirectories) -> Bool
func changeFileExtension(withName name: String, inDirectory: AppDirectories, toNewExtension newExtension: String) -> Bool
}
extension AppFileManipulation {
func writeFile(containing: String, to path: AppDirectories, withName name: String) -> Bool {
let filePath = getURL(for: path).path + "/" + name
let rawData: Data? = containing.data(using: .utf8)
return FileManager.default.createFile(atPath: filePath, contents: rawData, attributes: nil)
}
func readFile(at path: AppDirectories, withName name: String) -> String {
let filePath = getURL(for: path).path + "/" + name
let fileContents = FileManager.default.contents(atPath: filePath)
let fileContentsAsString = String(bytes: fileContents!, encoding: .utf8)
print(fileContentsAsString!)
return fileContentsAsString!
}
func deleteFile(at path: AppDirectories, withName name: String) -> Bool {
let filePath = buildFullPath(forFileName: name, isDirectory: path)
try! FileManager.default.removeItem(at: filePath)
return true
}
func renameFile(at path: AppDirectories, withName name: String, oldName: String, newName: String) -> Bool {
let getOldPath = getURL(for: path).appendingPathComponent(oldName)
let getNewPath = getURL(for: path).appendingPathComponent(newName)
try! FileManager.default.moveItem(at: getOldPath, to: getNewPath)
return true
}
func moveFile(withName name: String, inDirectory: AppDirectories, toDirectory: AppDirectories) -> Bool {
let originalURL = buildFullPath(forFileName: name, isDirectory: inDirectory)
let destinationURL = buildFullPath(forFileName: name, isDirectory: toDirectory)
try! FileManager.default.moveItem(at: originalURL, to: destinationURL)
return true
}
func copyFile(withName name: String, inDirectory: AppDirectories, toDirectory: AppDirectories) -> Bool {
let originalURL = buildFullPath(forFileName: name, isDirectory: inDirectory)
let destinationURL = buildFullPath(forFileName: name+"1", isDirectory: toDirectory)
try! FileManager.default.moveItem(at: originalURL, to: destinationURL)
return true
}
func changeFileExtension(withName name: String, inDirectory: AppDirectories, toNewExtension newExtension: String) -> Bool {
var newFileName = NSString(string: name)
newFileName = newFileName.deletingPathExtension as NSString
newFileName = newFileName.appendingPathExtension(newExtension) as! NSString
let finalFileName: String = String(newFileName)
let originalURL = buildFullPath(forFileName: name, isDirectory: inDirectory)
let destinationURL = buildFullPath(forFileName: finalFileName, isDirectory: inDirectory)
try! FileManager.default.moveItem(at: originalURL, to: destinationURL)
return true
}
}
// use case
struct AppFile: AppFileManipulation, AppFileStatusChecking, AppFileSystemMetaData {
let fileName: String
func moveToDocument() {
_ = moveFile(withName: fileName, inDirectory: .index, toDirectory: .documents)
}
func deleteTempFile() {
_ = deleteFile(at: .temp, withName: fileName)
}
func write() -> Bool {
writeFile(containing: "We were talking\nAbout the space\nBetween us all", to: .documents, withName: "karma.txt")
writeFile(containing: "And the people\nWho hide themselves\nBehind a wall", to: .documents, withName: "dharma.txt")
return true
}
func list() -> Bool {
let getUrl = getURL(for: .documents)
return list(directory: getUrl)
}
func getAttributes() {
let attribs = attributes(ofFile: buildFullPath(forFileName: "karma.txt", isDirectory: .documents))
for (key, value) in attribs {
print("\(key) value is \(value)")
}
}
}
let file = AppFile(fileName: "crosswalk-40.png")
file.moveToDocument()
let fileT = AppFile(fileName: "iOS file management in Swift 4.txt")
fileT.deleteTempFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment