added iOS source code
[wl-app.git] / iOS / Pods / MZDownloadManager / MZDownloadManager / Classes / MZUtility.swift
1 //
2 //  MZUtility.swift
3 //  MZDownloadManager
4 //
5 //  Created by Muhammad Zeeshan on 22/10/2014.
6 //  Copyright (c) 2014 ideamakerz. All rights reserved.
7 //
8
9 import UIKit
10
11 open class MZUtility: NSObject {
12     
13     @objc open static let DownloadCompletedNotif: String = {
14         return "com.MZDownloadManager.DownloadCompletedNotif"
15     }()
16     
17     @objc open static let baseFilePath: String = {
18         return (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as String
19     }()
20
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
26         
27         var isUnique            : Bool = false
28         var fileNumber          : Int = 0
29         
30         let fileManger          : FileManager = FileManager.default
31         
32         repeat {
33             var fileDocDirectoryPath : NSString?
34             
35             if fileExtension.length > 0 {
36                 fileDocDirectoryPath = "\(filePath.deletingLastPathComponent)/\(suggestedFileName).\(fileExtension)" as NSString?
37             } else {
38                 fileDocDirectoryPath = "\(filePath.deletingLastPathComponent)/\(suggestedFileName)" as NSString?
39             }
40             
41             let isFileAlreadyExists : Bool = fileManger.fileExists(atPath: fileDocDirectoryPath! as String)
42             
43             if isFileAlreadyExists {
44                 fileNumber += 1
45                 suggestedFileName = "\(fileName)(\(fileNumber))" as NSString
46             } else {
47                 isUnique = true
48                 if fileExtension.length > 0 {
49                     suggestedFileName = "\(suggestedFileName).\(fileExtension)" as NSString
50                 }
51             }
52         
53         } while isUnique == false
54         
55         return suggestedFileName
56     }
57     
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)
66         } else {
67             return Float(dataLength)
68         }
69     }
70     
71     @objc open class func calculateUnit(_ contentLength : Int64) -> NSString {
72         if(contentLength >= (1024*1024*1024)) {
73             return "GB"
74         } else if contentLength >= (1024*1024) {
75             return "MB"
76         } else if contentLength >= 1024 {
77             return "KB"
78         } else {
79             return "Bytes"
80         }
81     }
82     
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) {
87             
88             do {
89                 try (url as NSURL).setResourceValue(NSNumber(value: true as Bool), forKey: URLResourceKey.isExcludedFromBackupKey)
90                 return true
91             } catch let error as NSError {
92                 print("Error excluding \(url.lastPathComponent) from backup \(error)")
93                 return false
94             }
95
96         } else {
97             return false
98         }
99     }
100     
101     @objc open class func getFreeDiskspace() -> NSNumber? {
102         let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
103         let systemAttributes: AnyObject?
104         do {
105             systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: documentDirectoryPath.last!) as AnyObject?
106             let freeSize = systemAttributes?[FileAttributeKey.systemFreeSize] as? NSNumber
107             return freeSize
108         } catch let error as NSError {
109             print("Error Obtaining System Memory Info: Domain = \(error.domain), Code = \(error.code)")
110             return nil;
111         }
112     }
113 }