-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathDirectoryProxy.cs
More file actions
243 lines (205 loc) · 9.03 KB
/
DirectoryProxy.cs
File metadata and controls
243 lines (205 loc) · 9.03 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
// 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.Linq;
using System.Threading.Tasks;
using Dropbox.Api;
using Dropbox.Api.Files;
using ExtCore.FileStorage.Abstractions;
namespace ExtCore.FileStorage.Dropbox;
/// <summary>
/// Implements the <see cref="IDirectoryProxy">IDirectoryProxy</see> interface and represents a directory in a Dropbox account.
/// </summary>
public class DirectoryProxy : IDirectoryProxy
{
private readonly string accessToken;
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="accessToken">The Dropbox's account access token.</param>
/// <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>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public DirectoryProxy(string accessToken, string rootPath, string relativePath)
{
if (accessToken == string.Empty)
throw new ArgumentException($"Value can't be empty. Parameter name: accessToken.");
if (accessToken == null)
throw new ArgumentNullException($"Value can't be null. Parameter name: accessToken.", default(Exception));
this.accessToken = accessToken;
this.rootPath = RelativeUrl.Combine(rootPath);
this.RelativePath = RelativeUrl.Combine(relativePath);
this.path = RelativeUrl.Combine(this.rootPath, this.RelativePath);
if (string.Equals(this.path, "/"))
this.path = string.Empty;
}
/// <summary>
/// Checks if the underlying directory exists.
/// </summary>
/// <returns>Returns a flag indicating if the underlying directory exists.</returns>
public async Task<bool> ExistsAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
{
Metadata metadata = await dropboxClient.Files.GetMetadataAsync(this.path);
return metadata.IsFolder;
}
}
catch { return false; }
}
/// <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()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
await dropboxClient.Files.CreateFolderV2Async(this.path);
}
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));
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
await dropboxClient.Files.MoveV2Async(this.path, RelativeUrl.Combine(this.rootPath, destinationRelativePath));
}
catch (ApiException<RelocationError> e)
{
if (e.ErrorResponse.IsFromLookup)
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{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)
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
await dropboxClient.Files.DeleteV2Async(this.path);
}
catch (ApiException<DeleteError> e)
{
if (e.ErrorResponse.IsPathLookup)
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{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()
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
{
IList<IDirectoryProxy> directoryProxies = new List<IDirectoryProxy>();
try
{
foreach (Metadata metadata in (await dropboxClient.Files.ListFolderAsync(this.path)).Entries.Where(m => m.IsFolder))
directoryProxies.Add(new DirectoryProxy(this.accessToken, this.rootPath, metadata.PathDisplay.Substring(this.rootPath.Length)));
}
catch (ApiException<ListFolderError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{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);
}
return directoryProxies;
}
}
/// <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()
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
{
IList<IFileProxy> fileProxies = new List<IFileProxy>();
try
{
foreach (Metadata metadata in (await dropboxClient.Files.ListFolderAsync(this.path)).Entries.Where(m => m.IsFile))
{
string relativePath = metadata.PathDisplay;
relativePath = relativePath.Substring(this.rootPath.Length);
if (relativePath.Contains("/"))
relativePath = relativePath.Remove(relativePath.LastIndexOf("/"));
fileProxies.Add(new FileProxy(this.accessToken, this.rootPath, relativePath, metadata.Name));
}
}
catch (ApiException<ListFolderError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.path}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{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);
}
return fileProxies;
}
}
}