OSDN Git Service

acdf2791604a81a84f5f4de178ea83c3a6a54689
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
1 //
2 //  ApplicationDirecrories.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/08.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 private func supportDirName() -> String {
12     
13     let main = Bundle.main
14     
15     return main.bundleIdentifier
16         ?? main.infoDictionary?["CFBundleName"] as? String
17         ?? main.infoDictionary?["CFBundleExecutable"] as? String
18         ?? "UnknownAppliation"
19 }
20
21 func localizedAppName() -> String {
22     
23     guard let name = Bundle.main.localizedInfoDictionary?["CFBundleName"] as? String,
24         !name.isEmpty else {
25             
26             return supportDirName()
27     }
28     
29     return name
30 }
31
32 struct ApplicationDirecrories {
33     
34     static let support = searchedURL(for: .applicationSupportDirectory)
35         .appendingPathComponent(supportDirName())
36     
37     static let documents = searchedURL(for: .documentDirectory)
38     
39     static let pictures = searchedURL(for: .picturesDirectory)
40     
41     private static func searchedURL(for directory: FileManager.SearchPathDirectory) -> URL {
42         
43         return FileManager.default.urls(for: directory, in: .userDomainMask)
44             .last
45             ?? URL(fileURLWithPath: NSHomeDirectory())
46     }
47 }
48
49 extension ApplicationDirecrories {
50     
51     static let screenshotSaveDirectoryURL: URL = {
52         
53         let parentURL = URL(fileURLWithPath: AppDelegate.shared.screenShotSaveDirectory)
54         let url = parentURL.appendingPathComponent(localizedAppName())
55         let fm = FileManager.default
56         var isDir: ObjCBool = false
57         
58         do {
59             
60             if !fm.fileExists(atPath: url.path, isDirectory: &isDir) {
61                 
62                 try fm.createDirectory(at: url, withIntermediateDirectories: false)
63                 
64             } else if !isDir.boolValue {
65                 
66                 print("\(url) is regular file, not direcory.")
67                 return parentURL
68             }
69             
70         } catch {
71             
72             print("Can not create screenshot save directory.")
73             return parentURL
74         }
75         
76         return url
77     }()
78 }
79
80
81 func createDirectory(_ url: URL) -> Bool {
82     
83     do {
84         
85         try FileManager.default.createDirectory(at: url,
86                                                 withIntermediateDirectories: false,
87                                                 attributes: nil)
88         
89         return true
90         
91     } catch {
92         
93         return false
94     }
95 }
96
97 func checkDirectory(_ url: URL, create: Bool) -> Bool {
98     
99     do {
100         
101         let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
102         if !resourceValue.isDirectory! {
103             
104             print("Expected a folder to store application data, found a file \(url.path).")
105             
106             return false
107         }
108         
109         return true
110         
111     } catch let error as NSError {
112         
113         if create, error.code == NSFileReadNoSuchFileError {
114             
115             return createDirectory(url)
116         }
117         
118         return false
119     }
120 }