-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathDirectoryProxy.cs
More file actions
263 lines (229 loc) · 9.15 KB
/
DirectoryProxy.cs
File metadata and controls
263 lines (229 loc) · 9.15 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright © 2018 Dmitry Sikorsky. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ExtCore.FileStorage.Abstractions;
namespace ExtCore.FileStorage.FileSystem;
/// <summary>
/// Implements the <see cref="IDirectoryProxy">IDirectoryProxy</see> interface and represents a directory in a file system.
/// </summary>
public class DirectoryProxy : IDirectoryProxy
{
private readonly string rootPath;
private readonly string path;
/// <summary>
/// The path of the underlying directory relatively to the root one.
/// </summary>
public string RelativePath { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DirectoryProxy">DirectoryProxy</see> class.
/// </summary>
/// <param name="rootPath">The root path of the underlying directory's relative one.</param>
/// <param name="relativePath">The path of the underlying directory relatively to the root one.</param>
public DirectoryProxy(string rootPath, string relativePath)
{
this.rootPath = AbsolutePath.Combine(rootPath);
this.RelativePath = AbsolutePath.Combine(relativePath);
this.path = AbsolutePath.Combine(this.rootPath, this.RelativePath);
}
/// <summary>
/// Checks if the underlying directory exists.
/// </summary>
/// <returns>Returns a flag indicating if the underlying directory exists.</returns>
public async Task<bool> ExistsAsync()
{
return await Task<bool>.Factory.StartNew(() => Directory.Exists(this.path));
}
/// <summary>
/// Creates the underlying directory.
/// </summary>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task CreateAsync()
{
await Task.Factory.StartNew(() =>
{
try
{
Directory.CreateDirectory(this.path);
}
catch (UnauthorizedAccessException e)
{
throw new AccessDeniedException($"Access denied: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.DirectoryNotFoundException e)
{
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.PathTooLongException e)
{
throw new PathTooLongException($"Path too long: \"{this.path}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
});
}
/// <summary>
/// Moves the underlying directory.
/// </summary>
/// <param name="destinationRelativePath"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task MoveAsync(string destinationRelativePath)
{
if (destinationRelativePath == string.Empty)
throw new ArgumentException($"Value can't be empty. Parameter name: destinationRelativePath.");
if (destinationRelativePath == null)
throw new ArgumentNullException($"Value can't be null. Parameter name: destinationRelativePath.", default(Exception));
await Task.Factory.StartNew(() =>
{
try
{
Directory.Move(this.path, this.rootPath + destinationRelativePath);
}
catch (UnauthorizedAccessException e)
{
throw new AccessDeniedException($"Access denied: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.DirectoryNotFoundException e)
{
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.PathTooLongException e)
{
throw new PathTooLongException($"Path too long: \"{this.path}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
});
}
/// <summary>
/// Deletes the underlying directory.
/// </summary>
/// <param name="recursive">Pass true to remove all the underlying directory content recursively; otherwise false.</param>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task DeleteAsync(bool recursive)
{
await Task.Factory.StartNew(() =>
{
try
{
Directory.Delete(this.path, recursive);
}
catch (UnauthorizedAccessException e)
{
throw new AccessDeniedException($"Access denied: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.DirectoryNotFoundException e)
{
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.PathTooLongException e)
{
throw new PathTooLongException($"Path too long: \"{this.path}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
});
}
/// <summary>
/// Gets the directory proxies for the directories inside the underlying one.
/// </summary>
/// <returns>The directory proxies for the directories inside the underlying one</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<IEnumerable<IDirectoryProxy>> GetDirectoryProxiesAsync()
{
return await Task<IEnumerable<IDirectoryProxy>>.Factory.StartNew(
() =>
{
try
{
return Directory.GetDirectories(this.path).Select(
d => new DirectoryProxy(this.rootPath, d.Substring(this.rootPath.Length))
);
}
catch (UnauthorizedAccessException e)
{
throw new AccessDeniedException($"Access denied: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.DirectoryNotFoundException e)
{
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.PathTooLongException e)
{
throw new PathTooLongException($"Path too long: \"{this.path}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
}
);
}
/// <summary>
/// Gets the file proxies for the files inside the underlying one.
/// </summary>
/// <returns>The file proxies for the files inside the underlying directory.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<IEnumerable<IFileProxy>> GetFileProxiesAsync()
{
return await Task<IEnumerable<IFileProxy>>.Factory.StartNew(
() =>
{
try
{
return Directory.GetFiles(this.path).Select(
f =>
{
string relativePath = f;
relativePath = relativePath.Substring(this.rootPath.Length);
relativePath = relativePath.Remove(relativePath.LastIndexOf(Path.DirectorySeparatorChar));
return new FileProxy(this.rootPath, relativePath, f.Substring(f.LastIndexOf(Path.DirectorySeparatorChar) + 1));
}
);
}
catch (UnauthorizedAccessException e)
{
throw new AccessDeniedException($"Access denied: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.DirectoryNotFoundException e)
{
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
}
catch (System.IO.PathTooLongException e)
{
throw new PathTooLongException($"Path too long: \"{this.path}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
}
);
}
}