-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlwaysOnSizeApp.swift
158 lines (131 loc) · 4.46 KB
/
AlwaysOnSizeApp.swift
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
//
// AlwaysOnSizeApp.swift
// AlwaysOnSize
//
// Created by 최영우 on 2024. 2. 14..
//
import SwiftUI
import CoreServices
@main
struct AlwaysOnSizeApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
func getFormatedNumberString(size: Double) -> String {
var varsize : Double = size
if (varsize) / 1024 < 1 {
return String(format: "%.1fByte", varsize)
}
varsize /= 1024
if varsize / 1024 < 1 {
return String(format: "%.1fKB", varsize)
}
varsize /= 1024
if varsize / 1024 < 1 {
return String(format: "%.1fMB", varsize)
}
varsize /= 1024
return String(format: "%.1fGB", varsize)
}
func getFreeSizeAsString() -> String {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
var freeSize: Double? // Byte단위
do {
let attributes = try FileManager.default.attributesOfFileSystem(forPath: documentDirectoryPath.last! as String)
freeSize = attributes[FileAttributeKey.systemFreeSize] as? Double
} catch {
print(error)
}
var freeSizeAsDouble: Double = freeSize as? Double ?? 0
return getFormatedNumberString(size: freeSize!)
}
func findFileSize() -> [String] {
let filePath = try? FileManager.default.contentsOfDirectory(at: .downloadsDirectory, includingPropertiesForKeys: [.fileSizeKey])
var fileSize : Double = 0
var dic : [String:Double] = [:]
var dicarr : [String] = []
do {
//return [FileAttributeKey : Any]
for file in filePath! {
let attr = try FileManager.default.attributesOfItem(atPath: file.path)
fileSize = attr[FileAttributeKey.size] as! Double
//if you convert to NSDictionary, you can get file size old way as well.
let dict = attr as NSDictionary
fileSize = Double(dict.fileSize())
dic.updateValue(fileSize, forKey: file.path)
}
} catch {
print("Error: \(error)")
}
var sortdic = dic.sorted { (first, second) in
return first.value > second.value
}
for i in sortdic {
dicarr.append(String(i.value))
print(i.value)
}
return dicarr
}
struct FileInformation {
var fileName: String
var fileSize: Double
var path: String
var isChecked: Bool = false // Checkbox의 상태를 나타내는 속성
}
func findFileInformations() -> [FileInformation] {
var fileInformations: [FileInformation] = []
if let filePaths = try? FileManager.default.contentsOfDirectory(at: .downloadsDirectory, includingPropertiesForKeys: [.fileSizeKey]) {
for filePath in filePaths {
do {
let attr = try FileManager.default.attributesOfItem(atPath: filePath.path)
let fileSize = attr[FileAttributeKey.size] as? Double ?? 0
let fileName = filePath.lastPathComponent // 옵셔널 체이닝을 사용하여 안전하게 파일 이름을 가져옵니다.
let path = filePath.path
fileInformations.append(FileInformation(fileName: fileName, fileSize: fileSize, path: path))
} catch {
print("Error: \(error)")
}
}
} else {
print("Failed to get contents of directory.")
}
return fileInformations
}
func totalCheckedFileSize(fileInformations: [FileInformation]) -> Double {
var totalSize: Double = 0
for fileInfo in fileInformations {
if fileInfo.isChecked {
totalSize += fileInfo.fileSize
}
}
return totalSize
}
func deleteFile(atPath path: String, filename: String) -> Bool {
let fileManager = FileManager.default
let filePath = path
do {
// 파일이 존재하는지 확인합니다.
if fileManager.fileExists(atPath: filePath) {
// 파일을 삭제합니다.
try fileManager.removeItem(atPath: filePath)
print("File deleted successfully at path: \(filePath)")
return true
} else {
print("File does not exist at path: \(filePath)")
return false
}
} catch {
print("Error deleting file: \(error)")
return false
}
}
func deleteFiles(fileInformations: [FileInformation]) {
for fileInfo in fileInformations {
if fileInfo.isChecked {
_ = deleteFile(atPath: fileInfo.path, filename: fileInfo.fileName)
}
}
}