CNLiveFileManager.swift
5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// CNLiveFileManager.swift
// metaCode
//
// Created by zx on 2023/12/27.
//
import Foundation
class CNLiveFileManager:NSObject{
//单例类
static let shared = CNLiveFileManager()
// MARK: - 属性
/// 用户id 切换用户时会把当前用户存储路径指空
var _userId:String?
var userId:String?{
get{
_userId
}
set{
if _userId == newValue{
self.useDSFileDir = ""
}
_userId = newValue
}
}
// MARK: - 储存路径
/// 存储路径 ~/Document/DS
var _dsFileDir:String?
var dsFileDir:String?{
get{
if _dsFileDir == nil{
let path = String(format: "%@/Documents/metaCode/SearchGoods", NSHomeDirectory())
let fileManager = FileManager.default
var isDir : ObjCBool = false
let exist = fileManager.fileExists(atPath: path, isDirectory: &isDir)
if !(isDir.boolValue && exist) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
} catch {
debugPrint("文件目录创建失败")
return ""
}
}
_dsFileDir = path
}
return _dsFileDir
}
}
/// 当前用户储存路径
/// 没userId ~/Document/DS/Common/
/// 有userId ~/Document/DS/userId_XXX/
// MARK: - 网络缓存
private lazy var useDSFileDir: String = {
let userFileDir = NSString()
var path: String = ""
path = "\(dsFileDir!)/userId_\(DSUserInfoManager.shared.dsUserInfo.uid!)/"
let fileManager = FileManager.default
var isDir: ObjCBool = false
let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
if !(isDir.boolValue && exist) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
} catch {
debugPrint("文件目录创建失败")
return ""
}
}
return path as String
}()
var _path:String?
var path:String?{
get{
String(format: "%@/storage.plist", self.useDSFileDir )
}
}
// MARK: - 存储、读取、删除
/// write file 储存
/// @param object 存储的对象
/// @param key 存储对象的key
func saveData(object:Array<Any>,key:String){
var dic = NSMutableDictionary(contentsOfFile: self.path!)
if dic == nil{
dic = NSMutableDictionary()
}
dic?.setObject(object, forKey: key as NSCopying)
dic?.write(toFile: self.path!, atomically: true)
}
/// write file 读取
/// @param key 存储对象的key
func takeDataKey(key:String) -> Array<Any>?{
var dic = NSMutableDictionary(contentsOfFile: self.path!)
if dic == nil{
return nil
}
return dic![key] as? Array<Any>
}
/// 获取存储的所有数据
func takeAllSaveData() -> NSDictionary?{
var dic = NSMutableDictionary(contentsOfFile: self.path!)
if dic == nil{
return nil
}
return dic!
}
/// 删除存储中所有的key
func resetAllSaveData(){
var dic = NSMutableDictionary(contentsOfFile: self.path!)
if dic != nil{
dic!.removeAllObjects()
dic!.write(toFile: self.path!, atomically: true)
}
}
/// 删除存储中对应的key
/// @param resetKey 删除的key
func deleteKey(resetKey:String){
var dic = NSMutableDictionary(contentsOfFile: self.path!)
if dic != nil{
dic!.removeObject(forKey: resetKey)
dic!.write(toFile: self.path!, atomically: true)
}
}
/// 计算储存路径目录下所有文件大小 单位字节b
func calcDSFileDirCacheSize() -> CGFloat {
let fileManager = FileManager.default
var size: UInt64 = 0
var isDir: ObjCBool = false
do {
let directoryAttributes = try fileManager.attributesOfItem(atPath: self.dsFileDir!)
isDir = directoryAttributes[FileAttributeKey.type] as? ObjCBool ?? false
} catch {
print("Error getting attributes of directory: \(error)")
}
if isDir.boolValue {
let fileList = try? fileManager.contentsOfDirectory(atPath: self.dsFileDir!)
for filePath in fileList ?? [] {
do {
let fileAttributes = try fileManager.attributesOfItem(atPath: (self.dsFileDir! as NSString).appendingPathComponent(filePath))
size += fileAttributes[.size] as? UInt64 ?? 0
} catch {
print("Error getting attributes of file: \(error)")
}
}
} else {
print("The path is not a directory.")
}
return CGFloat(size)
}
/// 清空储存路径缓存
/// @param completion 清空完成回调
func clearDSFileDirCacheCompletion(_ completion: @escaping () -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
let fileManager = FileManager.default
let files = try? fileManager.contentsOfDirectory(atPath: self.dsFileDir!)
for file in files! {
let path = (self.dsFileDir! as NSString).appendingPathComponent(file)
do {
if FileManager.default.fileExists(atPath: path) {
try FileManager.default.removeItem(atPath: path)
}
} catch {
print("Error removing item at path: \(path), error: \(error)")
}
}
DispatchQueue.main.async {
completion()
}
}
}
}