|
6 | 6 |
|
7 | 7 | // ReSharper disable UseFileSystem
|
8 | 8 |
|
9 |
| -namespace KY.Core.DataAccess |
| 9 | +namespace KY.Core.DataAccess; |
| 10 | + |
| 11 | +internal class FileHelper |
10 | 12 | {
|
11 |
| - internal class FileHelper |
| 13 | + private static readonly Regex sizeRegex = new(@"^(?<size>[0-9.,]+)\s?(?<unit>[A-z]+)$"); |
| 14 | + private readonly PathHelper pathHelper; |
| 15 | + |
| 16 | + public FileHelper(PathHelper pathHelper) |
12 | 17 | {
|
13 |
| - private static readonly Regex sizeRegex = new Regex(@"^(?<size>[0-9.,]+)\s?(?<unit>[A-z]+)$"); |
14 |
| - private readonly PathHelper pathHelper; |
| 18 | + this.pathHelper = pathHelper; |
| 19 | + } |
15 | 20 |
|
16 |
| - public FileHelper(PathHelper pathHelper) |
17 |
| - { |
18 |
| - this.pathHelper = pathHelper; |
19 |
| - } |
| 21 | + public FileInfo GetInfo(string path) |
| 22 | + { |
| 23 | + path = this.pathHelper.ToAbsolute(path); |
| 24 | + return new FileInfo(path); |
| 25 | + } |
20 | 26 |
|
21 |
| - public FileInfo GetInfo(string path) |
22 |
| - { |
23 |
| - path = this.pathHelper.ToAbsolute(path); |
24 |
| - return new FileInfo(path); |
25 |
| - } |
| 27 | + public FileInfo[] GetInfos(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly) |
| 28 | + { |
| 29 | + path = this.pathHelper.ToAbsolute(path); |
| 30 | + DirectoryInfo directory = new(path); |
| 31 | + return searchPattern == null ? directory.GetFiles() : directory.GetFiles(searchPattern, option); |
| 32 | + } |
26 | 33 |
|
27 |
| - public FileInfo[] GetInfos(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly) |
28 |
| - { |
29 |
| - path = this.pathHelper.ToAbsolute(path); |
30 |
| - DirectoryInfo directory = new DirectoryInfo(path); |
31 |
| - return searchPattern == null ? directory.GetFiles() : directory.GetFiles(searchPattern, option); |
32 |
| - } |
| 34 | + public string[] GetFiles(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly) |
| 35 | + { |
| 36 | + path = this.pathHelper.ToAbsolute(path); |
| 37 | + return searchPattern == null ? Directory.GetFiles(path) : Directory.GetFiles(path, searchPattern, option); |
| 38 | + } |
33 | 39 |
|
34 |
| - public string[] GetFiles(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly) |
35 |
| - { |
36 |
| - path = this.pathHelper.ToAbsolute(path); |
37 |
| - return searchPattern == null ? Directory.GetFiles(path) : Directory.GetFiles(path, searchPattern, option); |
38 |
| - } |
| 40 | + public string ReadAllText(string path, Encoding encoding = null) |
| 41 | + { |
| 42 | + path = this.pathHelper.ToAbsolute(path); |
| 43 | + return File.ReadAllText(path, encoding ?? Encoding.UTF8); |
| 44 | + } |
39 | 45 |
|
40 |
| - public string ReadAllText(string path, Encoding encoding = null) |
41 |
| - { |
42 |
| - path = this.pathHelper.ToAbsolute(path); |
43 |
| - return File.ReadAllText(path, encoding ?? Encoding.UTF8); |
44 |
| - } |
| 46 | + public void WriteAllText(string path, string content, Encoding encoding = null) |
| 47 | + { |
| 48 | + path = this.pathHelper.ToAbsolute(path); |
| 49 | + File.WriteAllText(path, content, encoding ?? Encoding.UTF8); |
| 50 | + } |
45 | 51 |
|
46 |
| - public void WriteAllText(string path, string content, Encoding encoding = null) |
47 |
| - { |
48 |
| - path = this.pathHelper.ToAbsolute(path); |
49 |
| - File.WriteAllText(path, content, encoding ?? Encoding.UTF8); |
50 |
| - } |
| 52 | + public void AppendAllText(string path, string content, Encoding encoding = null) |
| 53 | + { |
| 54 | + path = this.pathHelper.ToAbsolute(path); |
| 55 | + File.AppendAllText(path, content, encoding ?? Encoding.UTF8); |
| 56 | + } |
51 | 57 |
|
52 |
| - public void AppendAllText(string path, string content, Encoding encoding = null) |
53 |
| - { |
54 |
| - path = this.pathHelper.ToAbsolute(path); |
55 |
| - File.AppendAllText(path, content, encoding ?? Encoding.UTF8); |
56 |
| - } |
| 58 | + public void WriteAllBytes(string path, byte[] content) |
| 59 | + { |
| 60 | + path = this.pathHelper.ToAbsolute(path); |
| 61 | + File.WriteAllBytes(path, content); |
| 62 | + } |
57 | 63 |
|
58 |
| - public void WriteAllBytes(string path, byte[] content) |
59 |
| - { |
60 |
| - path = this.pathHelper.ToAbsolute(path); |
61 |
| - File.WriteAllBytes(path, content); |
62 |
| - } |
| 64 | + public bool Exists(params string[] pathChunks) |
| 65 | + { |
| 66 | + string path = this.pathHelper.ToAbsolute(pathChunks); |
| 67 | + return File.Exists(path); |
| 68 | + } |
63 | 69 |
|
64 |
| - public bool Exists(params string[] pathChunks) |
65 |
| - { |
66 |
| - string path = this.pathHelper.ToAbsolute(pathChunks); |
67 |
| - return File.Exists(path); |
68 |
| - } |
| 70 | + public void Delete(params string[] pathChunks) |
| 71 | + { |
| 72 | + string path = this.pathHelper.ToAbsolute(pathChunks); |
| 73 | + File.Delete(path); |
| 74 | + } |
69 | 75 |
|
70 |
| - public void Delete(params string[] pathChunks) |
71 |
| - { |
72 |
| - string path = this.pathHelper.ToAbsolute(pathChunks); |
73 |
| - File.Delete(path); |
74 |
| - } |
| 76 | + public string[] ReadAllLines(string path, Encoding encoding = null) |
| 77 | + { |
| 78 | + path = this.pathHelper.ToAbsolute(path); |
| 79 | + return File.ReadAllLines(path, encoding ?? Encoding.UTF8); |
| 80 | + } |
75 | 81 |
|
76 |
| - public string[] ReadAllLines(string path, Encoding encoding = null) |
77 |
| - { |
78 |
| - path = this.pathHelper.ToAbsolute(path); |
79 |
| - return File.ReadAllLines(path, encoding ?? Encoding.UTF8); |
80 |
| - } |
| 82 | + public XElement ReadXml(params string[] pathChunks) |
| 83 | + { |
| 84 | + string path = this.pathHelper.ToAbsolute(pathChunks); |
| 85 | + return XElement.Load(path); |
| 86 | + } |
81 | 87 |
|
82 |
| - public XElement ReadXml(params string[] pathChunks) |
83 |
| - { |
84 |
| - string path = this.pathHelper.ToAbsolute(pathChunks); |
85 |
| - return XElement.Load(path); |
86 |
| - } |
| 88 | + public Stream OpenRead(params string[] pathChunks) |
| 89 | + { |
| 90 | + string path = this.pathHelper.ToAbsolute(pathChunks); |
| 91 | + return File.OpenRead(path); |
| 92 | + } |
87 | 93 |
|
88 |
| - public Stream OpenRead(params string[] pathChunks) |
89 |
| - { |
90 |
| - string path = this.pathHelper.ToAbsolute(pathChunks); |
91 |
| - return File.OpenRead(path); |
92 |
| - } |
| 94 | + public Stream OpenWrite(params string[] pathChunks) |
| 95 | + { |
| 96 | + string path = this.pathHelper.ToAbsolute(pathChunks); |
| 97 | + return File.Open(path, FileMode.OpenOrCreate); |
| 98 | + } |
| 99 | + |
| 100 | + public void Copy(string from, string to, bool overwrite = false) |
| 101 | + { |
| 102 | + from = this.pathHelper.ToAbsolute(from); |
| 103 | + to = this.pathHelper.ToAbsolute(to); |
| 104 | + File.Copy(from, to, overwrite); |
| 105 | + } |
| 106 | + |
| 107 | + public void Move(string path, string oldFileName, string newFileName, bool overwrite = false) |
| 108 | + { |
| 109 | + string from = this.pathHelper.Combine(path, oldFileName); |
| 110 | + string to = this.pathHelper.Combine(path, newFileName); |
| 111 | + this.Move(from, to, overwrite); |
| 112 | + } |
93 | 113 |
|
94 |
| - public Stream OpenWrite(params string[] pathChunks) |
| 114 | + public void Move(string from, string to, bool overwrite = false) |
| 115 | + { |
| 116 | + from = this.pathHelper.ToAbsolute(from); |
| 117 | + to = this.pathHelper.ToAbsolute(to); |
| 118 | + if (overwrite && File.Exists(to)) |
95 | 119 | {
|
96 |
| - string path = this.pathHelper.ToAbsolute(pathChunks); |
97 |
| - return File.OpenWrite(path); |
| 120 | + File.Delete(to); |
98 | 121 | }
|
| 122 | + File.Move(from, to); |
| 123 | + } |
| 124 | + |
| 125 | + public void Write(string path, XElement xml) |
| 126 | + { |
| 127 | + path = this.pathHelper.ToAbsolute(path); |
| 128 | + xml.Save(path); |
| 129 | + } |
99 | 130 |
|
100 |
| - public void Copy(string from, string to, bool overwrite = false) |
| 131 | + public string FormatSize(long size) |
| 132 | + { |
| 133 | + if (size >= 1024L * 1024 * 1024 * 1024 * 1024 * 1024) |
101 | 134 | {
|
102 |
| - from = this.pathHelper.ToAbsolute(from); |
103 |
| - to = this.pathHelper.ToAbsolute(to); |
104 |
| - File.Copy(from, to, overwrite); |
| 135 | + return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024 / 1024:0.#} EB"; |
105 | 136 | }
|
106 |
| - |
107 |
| - public void Move(string path, string oldFileName, string newFileName, bool overwrite = false) |
| 137 | + if (size >= 1024L * 1024 * 1024 * 1024 * 1024) |
108 | 138 | {
|
109 |
| - string from = this.pathHelper.Combine(path, oldFileName); |
110 |
| - string to = this.pathHelper.Combine(path, newFileName); |
111 |
| - this.Move(from, to, overwrite); |
| 139 | + return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024:0.#} PB"; |
112 | 140 | }
|
113 |
| - |
114 |
| - public void Move(string from, string to, bool overwrite = false) |
| 141 | + if (size >= 1024L * 1024 * 1024 * 1024) |
115 | 142 | {
|
116 |
| - from = this.pathHelper.ToAbsolute(from); |
117 |
| - to = this.pathHelper.ToAbsolute(to); |
118 |
| - if (overwrite && File.Exists(to)) |
119 |
| - { |
120 |
| - File.Delete(to); |
121 |
| - } |
122 |
| - File.Move(from, to); |
| 143 | + return $"{size / 1024.0 / 1024 / 1024 / 1024:0.#} TB"; |
123 | 144 | }
|
124 |
| - |
125 |
| - public void Write(string path, XElement xml) |
| 145 | + if (size >= 1024 * 1024 * 1024) |
126 | 146 | {
|
127 |
| - path = this.pathHelper.ToAbsolute(path); |
128 |
| - xml.Save(path); |
| 147 | + return $"{size / 1024.0 / 1024 / 1024:0.#} GB"; |
129 | 148 | }
|
130 |
| - |
131 |
| - public string FormatSize(long size) |
| 149 | + if (size >= 1024 * 1024) |
132 | 150 | {
|
133 |
| - if (size >= 1024L * 1024 * 1024 * 1024 * 1024 * 1024) |
134 |
| - return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024 / 1024:0.#} EB"; |
135 |
| - if (size >= 1024L * 1024 * 1024 * 1024 * 1024) |
136 |
| - return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024:0.#} PB"; |
137 |
| - if (size >= 1024L * 1024 * 1024 * 1024) |
138 |
| - return $"{size / 1024.0 / 1024 / 1024 / 1024:0.#} TB"; |
139 |
| - if (size >= 1024 * 1024 * 1024) |
140 |
| - return $"{size / 1024.0 / 1024 / 1024:0.#} GB"; |
141 |
| - if (size >= 1024 * 1024) |
142 |
| - return $"{size / 1024.0 / 1024:0.#} MB"; |
143 |
| - if (size >= 1024) |
144 |
| - return $"{size / 1024.0:0.#} kB"; |
145 |
| - return $"{size} B"; |
| 151 | + return $"{size / 1024.0 / 1024:0.#} MB"; |
146 | 152 | }
|
147 |
| - |
148 |
| - public long ParseSize(string value) |
| 153 | + if (size >= 1024) |
149 | 154 | {
|
150 |
| - if (string.IsNullOrEmpty(value)) |
151 |
| - return 0; |
152 |
| - Match match = sizeRegex.Match(value); |
153 |
| - if (!match.Success) |
154 |
| - return 0; |
155 |
| - double size = double.Parse(match.Groups["size"].Value); |
156 |
| - switch (match.Groups["unit"].Value.ToUpper()) |
157 |
| - { |
158 |
| - case "EB": |
159 |
| - return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024); |
160 |
| - case "PB": |
161 |
| - return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024); |
162 |
| - case "TB": |
163 |
| - return (long)(size * 1024 * 1024 * 1024 * 1024); |
164 |
| - case "GB": |
165 |
| - return (long)(size * 1024 * 1024 * 1024); |
166 |
| - case "MB": |
167 |
| - return (long)(size * 1024 * 1024); |
168 |
| - case "KB": |
169 |
| - return (long)(size * 1024); |
170 |
| - default: |
171 |
| - return (long)size; |
172 |
| - } |
| 155 | + return $"{size / 1024.0:0.#} kB"; |
173 | 156 | }
|
| 157 | + return $"{size} B"; |
| 158 | + } |
174 | 159 |
|
175 |
| - public string GetName(string path) |
| 160 | + public long ParseSize(string value) |
| 161 | + { |
| 162 | + if (string.IsNullOrEmpty(value)) |
176 | 163 | {
|
177 |
| - return Path.GetFileName(path); |
| 164 | + return 0; |
178 | 165 | }
|
179 |
| - |
180 |
| - public FileSystemWatcher Watch(string path) |
| 166 | + Match match = sizeRegex.Match(value); |
| 167 | + if (!match.Success) |
181 | 168 | {
|
182 |
| - string absolutePath = FileSystem.ToAbsolutePath(path); |
183 |
| - string directory = FileSystem.GetDirectoryName(absolutePath); |
184 |
| - string file = FileSystem.GetFileName(absolutePath); |
185 |
| - FileSystemWatcher watchdog = new (directory); |
186 |
| - watchdog.Filter = file; |
187 |
| - watchdog.EnableRaisingEvents = true; |
188 |
| - return watchdog; |
| 169 | + return 0; |
189 | 170 | }
|
190 |
| - |
191 |
| - public DateTime GetLastWriteTime(params string[] pathChunks) |
| 171 | + double size = double.Parse(match.Groups["size"].Value); |
| 172 | + switch (match.Groups["unit"].Value.ToUpper()) |
192 | 173 | {
|
193 |
| - return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks)); |
| 174 | + case "EB": |
| 175 | + return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024); |
| 176 | + case "PB": |
| 177 | + return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024); |
| 178 | + case "TB": |
| 179 | + return (long)(size * 1024 * 1024 * 1024 * 1024); |
| 180 | + case "GB": |
| 181 | + return (long)(size * 1024 * 1024 * 1024); |
| 182 | + case "MB": |
| 183 | + return (long)(size * 1024 * 1024); |
| 184 | + case "KB": |
| 185 | + return (long)(size * 1024); |
| 186 | + default: |
| 187 | + return (long)size; |
194 | 188 | }
|
195 | 189 | }
|
| 190 | + |
| 191 | + public string GetName(string path) |
| 192 | + { |
| 193 | + return Path.GetFileName(path); |
| 194 | + } |
| 195 | + |
| 196 | + public FileSystemWatcher Watch(string path) |
| 197 | + { |
| 198 | + string absolutePath = FileSystem.ToAbsolutePath(path); |
| 199 | + string directory = FileSystem.GetDirectoryName(absolutePath); |
| 200 | + string file = FileSystem.GetFileName(absolutePath); |
| 201 | + FileSystemWatcher watchdog = new(directory); |
| 202 | + watchdog.Filter = file; |
| 203 | + watchdog.EnableRaisingEvents = true; |
| 204 | + return watchdog; |
| 205 | + } |
| 206 | + |
| 207 | + public DateTime GetLastWriteTime(params string[] pathChunks) |
| 208 | + { |
| 209 | + return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks)); |
| 210 | + } |
196 | 211 | }
|
0 commit comments