5 // Created by Muhammad Zeeshan on 22/10/2014.
6 // Copyright (c) 2014 ideamakerz. All rights reserved.
11 open class MZUtility: NSObject {
13 @objc open static let DownloadCompletedNotif: String = {
14 return "com.MZDownloadManager.DownloadCompletedNotif"
17 @objc open static let baseFilePath: String = {
18 return (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as String
21 @objc open class func getUniqueFileNameWithPath(_ filePath : NSString) -> NSString {
22 let fullFileName : NSString = filePath.lastPathComponent as NSString
23 let fileName : NSString = fullFileName.deletingPathExtension as NSString
24 let fileExtension : NSString = fullFileName.pathExtension as NSString
25 var suggestedFileName : NSString = fileName
27 var isUnique : Bool = false
28 var fileNumber : Int = 0
30 let fileManger : FileManager = FileManager.default
33 var fileDocDirectoryPath : NSString?
35 if fileExtension.length > 0 {
36 fileDocDirectoryPath = "\(filePath.deletingLastPathComponent)/\(suggestedFileName).\(fileExtension)" as NSString?
38 fileDocDirectoryPath = "\(filePath.deletingLastPathComponent)/\(suggestedFileName)" as NSString?
41 let isFileAlreadyExists : Bool = fileManger.fileExists(atPath: fileDocDirectoryPath! as String)
43 if isFileAlreadyExists {
45 suggestedFileName = "\(fileName)(\(fileNumber))" as NSString
48 if fileExtension.length > 0 {
49 suggestedFileName = "\(suggestedFileName).\(fileExtension)" as NSString
53 } while isUnique == false
55 return suggestedFileName
58 @objc open class func calculateFileSizeInUnit(_ contentLength : Int64) -> Float {
59 let dataLength : Float64 = Float64(contentLength)
60 if dataLength >= (1024.0*1024.0*1024.0) {
61 return Float(dataLength/(1024.0*1024.0*1024.0))
62 } else if dataLength >= 1024.0*1024.0 {
63 return Float(dataLength/(1024.0*1024.0))
64 } else if dataLength >= 1024.0 {
65 return Float(dataLength/1024.0)
67 return Float(dataLength)
71 @objc open class func calculateUnit(_ contentLength : Int64) -> NSString {
72 if(contentLength >= (1024*1024*1024)) {
74 } else if contentLength >= (1024*1024) {
76 } else if contentLength >= 1024 {
83 @objc open class func addSkipBackupAttributeToItemAtURL(_ docDirectoryPath : NSString) -> Bool {
84 let url : URL = URL(fileURLWithPath: docDirectoryPath as String)
85 let fileManager = FileManager.default
86 if fileManager.fileExists(atPath: url.path) {
89 try (url as NSURL).setResourceValue(NSNumber(value: true as Bool), forKey: URLResourceKey.isExcludedFromBackupKey)
91 } catch let error as NSError {
92 print("Error excluding \(url.lastPathComponent) from backup \(error)")
101 @objc open class func getFreeDiskspace() -> NSNumber? {
102 let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
103 let systemAttributes: AnyObject?
105 systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: documentDirectoryPath.last!) as AnyObject?
106 let freeSize = systemAttributes?[FileAttributeKey.systemFreeSize] as? NSNumber
108 } catch let error as NSError {
109 print("Error Obtaining System Memory Info: Domain = \(error.domain), Code = \(error.code)")