-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathFileProxy.cs
More file actions
276 lines (241 loc) · 10.3 KB
/
FileProxy.cs
File metadata and controls
276 lines (241 loc) · 10.3 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
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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.IO;
using System.Text;
using System.Threading.Tasks;
using Dropbox.Api;
using Dropbox.Api.Files;
using Dropbox.Api.Stone;
using ExtCore.FileStorage.Abstractions;
namespace ExtCore.FileStorage.Dropbox;
/// <summary>
/// Implements the <see cref="IDirectoryProxy">IDirectoryProxy</see> interface and represents a file in a Dropbox account.
/// </summary>
public class FileProxy : IFileProxy
{
private readonly string accessToken;
private readonly string rootPath;
private readonly string filepath;
/// <summary>
/// The path of the underlying file relatively to the root one.
/// </summary>
public string RelativePath { get; private set; }
/// <summary>
/// The filename of the underlying file.
/// </summary>
public string Filename { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="FileProxy">FileProxy</see> class.
/// </summary>
/// <param name="accessToken">The Dropbox's account access token.</param>
/// <param name="rootPath">The root path of the underlying file's relative one.</param>
/// <param name="relativePath">The path of the underlying file relatively to the root one.</param>
/// <param name="filename">The filename of the underlying file.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public FileProxy(string accessToken, string rootPath, string relativePath, string filename)
{
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));
if (filename == string.Empty)
throw new ArgumentException($"Value can't be empty. Parameter name: filename.");
if (filename == null)
throw new ArgumentNullException($"Value can't be null. Parameter name: filename.", default(Exception));
this.accessToken = accessToken;
this.rootPath = RelativeUrl.Combine(rootPath);
this.RelativePath = RelativeUrl.Combine(relativePath);
this.Filename = filename;
this.filepath = RelativeUrl.Combine(this.rootPath, this.RelativePath, this.Filename);
}
/// <summary>
/// Checks if the underlying file exists.
/// </summary>
/// <returns>Returns a flag indicating if the underlying file exists.</returns>
public async Task<bool> ExistsAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
{
Metadata metadata = await dropboxClient.Files.GetMetadataAsync(this.filepath);
return metadata.IsFile;
}
}
catch { return false; }
}
/// <summary>
/// Reads content of the underlying file as a byte array.
/// </summary>
/// <returns>Content of the underlying file as a byte array.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<Stream> ReadStreamAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
using (IDownloadResponse<FileMetadata> response = await dropboxClient.Files.DownloadAsync(this.filepath))
return await response.GetContentAsStreamAsync();
}
catch (ApiException<DownloadError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.filepath}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Reads content of the underlying file as a byte array.
/// </summary>
/// <returns>Content of the underlying file as a byte array.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<byte[]> ReadBytesAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
using (IDownloadResponse<FileMetadata> response = await dropboxClient.Files.DownloadAsync(this.filepath))
return await response.GetContentAsByteArrayAsync();
}
catch (ApiException<DownloadError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.filepath}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Reads content of the underlying file as a text string.
/// </summary>
/// <returns>Content of the underlying file as a text string.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<string> ReadTextAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
using (IDownloadResponse<FileMetadata> response = await dropboxClient.Files.DownloadAsync(this.filepath))
return await response.GetContentAsStringAsync();
}
catch (ApiException<DownloadError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.filepath}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a stream.
/// </summary>
/// <param name="inputStream">Content to write to the underlying file as a stream.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteStreamAsync(Stream inputStream)
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
await dropboxClient.Files.UploadAsync(this.filepath, WriteMode.Overwrite.Instance, body: inputStream);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a byte array.
/// </summary>
/// <param name="bytes">Content to write to the underlying file as a byte array.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteBytesAsync(byte[] bytes)
{
try
{
using (MemoryStream inputStream = new MemoryStream(bytes))
await this.WriteStreamAsync(inputStream);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a text string.
/// </summary>
/// <param name="text">Content to write to the underlying file as a text string.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteTextAsync(string text)
{
try
{
await this.WriteBytesAsync(Encoding.UTF8.GetBytes(text));
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Deletes the underlying file.
/// </summary>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task DeleteAsync()
{
try
{
using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
await dropboxClient.Files.DeleteV2Async(this.filepath);
}
catch (ApiException<ListFolderError> e)
{
if (e.ErrorResponse.IsPath)
throw new DirectoryNotFoundException($"Directory not found: \"{this.filepath}\". See inner exception for details.", e);
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
}