Skip to content

Instantly share code, notes, and snippets.

@Ilesh
Last active September 14, 2020 13:15
Show Gist options
  • Select an option

  • Save Ilesh/1d6167f6d61ab035f4e39ff02dbf2a2a to your computer and use it in GitHub Desktop.

Select an option

Save Ilesh/1d6167f6d61ab035f4e39ff02dbf2a2a to your computer and use it in GitHub Desktop.
//
// IPDirectory.swift
//
//
// Created by Ilesh's 2018 on 13/06/19.
// Copyright © 2019 Ilesh's mac. All rights reserved.
//
import Foundation
import UIKit
class IPDirectory {
class func getDocumentPath() -> String {
return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}
private class func getDocument() -> URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
class func saveImage(to path:String = "", image: UIImage , fileName: String) {
var fileURL : URL!
if path == "" {
fileURL = getDocument().appendingPathComponent(fileName)
}else{
fileURL = getDocument().appendingPathComponent(path).appendingPathComponent(fileName)
}
if let data = image.pngData(),!FileManager.default.fileExists(atPath: fileURL.path){
do {
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}
class func delete(directoryFile:String) -> Bool?{
let fullPath = getDocument().appendingPathComponent(directoryFile)
if FileManager.default.fileExists(atPath: fullPath.path) {
do {
try FileManager.default.removeItem(atPath: fullPath.path)
print("Local path removed successfully")
return true
} catch let error as NSError {
print("------Error",error.debugDescription)
return false
}
}else{
return nil
}
}
class func loadFile(on path: String = "",fileName:String) -> String {
var fileURL : URL!
if path == "" {
fileURL = getDocument().appendingPathComponent(fileName)
}else{
fileURL = getDocument().appendingPathComponent(path).appendingPathComponent(fileName)
}
return fileURL.path
}
class func loadImage(on path:String = "",imageName:String) -> UIImage? {
var fileURL : URL!
if path == "" {
fileURL = getDocument().appendingPathComponent(imageName)
}else{
fileURL = getDocument().appendingPathComponent(path).appendingPathComponent(imageName)
}
if FileManager.default.fileExists(atPath: fileURL.path){
let image = UIImage(contentsOfFile: fileURL.path)
return image!
}
return nil
}
/// Returns the files name list which contails in the documents folder.
///
///
/// - Parameters:
/// - Extension: Get only the specific extension list. Ex: .pdf/.mp3/.mkv Types list
class func loadAllDocumentsFiles(ofExtension:String = "") -> [String] {
var arrFiles : [String] = []
let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let arrFileList = try? FileManager.default.contentsOfDirectory(atPath: directory)
if ofExtension == "" {
return arrFileList ?? arrFiles
}else{
if let data = arrFileList, data.count > 0 {
arrFiles = data.filter({$0.lowercased().contain(ofExtension)})
}
return arrFiles
}
}
class func DeleteAllDocumentFiles(ofExtension:String){
let arrAllFiles = self.loadAllDocumentsFiles(ofExtension: ofExtension)
if arrAllFiles.count > 0 {
arrAllFiles.forEach { (fileName) in
if let delete = self.delete(directoryFile:fileName), delete {
print("successfully Deleted \(fileName)")
}else{
print("Error on deleted this file \(fileName) !")
}
}
}
}
}
extension String {
/**
Generates a unique string that can be used as a filename for storing data objects that need to ensure they have a unique filename. It is guranteed to be unique.
https://stackoverflow.com/a/39904016/2910061
- parameter prefix: The prefix of the filename that will be added to every generated string.
- returns: A string that will consists of a prefix (if available) and a generated unique string.
*/
static func uniqueFilename(withPrefix prefix: String? = nil) -> String {
let uniqueString = ProcessInfo.processInfo.globallyUniqueString
if prefix != nil {
return "\(prefix!)-\(uniqueString)"
}
return uniqueString
}
static func randomString(prefix:String = "", length: Int,strExtension:String = ".png") -> String {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var randomString = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
var nextChar = letters.character(at: Int(rand))
randomString += NSString(characters: &nextChar, length: 1) as String
}
return ((prefix == "") ? (randomString + strExtension):"\(prefix + randomString + strExtension)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment