Skip to content

Commit 2054ce0

Browse files
committed
add virtual path on file and directory models
1 parent dacc03d commit 2054ce0

27 files changed

Lines changed: 604 additions & 545 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<PropertyGroup>
13-
<PackageVersion>1.0.0-beta2</PackageVersion>
13+
<PackageVersion>1.0.0-beta3</PackageVersion>
1414
<Company>SharpGrip</Company>
1515
<Authors>SharpGrip</Authors>
1616
<Copyright>SharpGrip</Copyright>

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public override void Connect()
3333
{
3434
}
3535

36-
public override async Task<IFile> GetFileAsync(string path, CancellationToken cancellationToken = default)
36+
public override async Task<IFile> GetFileAsync(string virtualPath, CancellationToken cancellationToken = default)
3737
{
38-
path = PrependRootPath(path);
38+
var path = GetPath(virtualPath);
3939

4040
try
4141
{
4242
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
4343

44-
return ModelFactory.CreateFile(response);
44+
return ModelFactory.CreateFile(response, virtualPath);
4545
}
4646
catch (AmazonS3Exception exception)
4747
{
@@ -58,9 +58,9 @@ public override async Task<IFile> GetFileAsync(string path, CancellationToken ca
5858
}
5959
}
6060

61-
public override async Task<IDirectory> GetDirectoryAsync(string path, CancellationToken cancellationToken = default)
61+
public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default)
6262
{
63-
path = PrependRootPath(path);
63+
var path = GetPath(virtualPath);
6464

6565
if (!path.EndsWith("/"))
6666
{
@@ -89,7 +89,7 @@ public override async Task<IDirectory> GetDirectoryAsync(string path, Cancellati
8989
{
9090
if (item.Key == path)
9191
{
92-
return ModelFactory.CreateDirectory(item);
92+
return ModelFactory.CreateDirectory(item, virtualPath);
9393
}
9494
}
9595

@@ -101,10 +101,10 @@ public override async Task<IDirectory> GetDirectoryAsync(string path, Cancellati
101101
}
102102
}
103103

104-
public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", CancellationToken cancellationToken = default)
104+
public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath = "", CancellationToken cancellationToken = default)
105105
{
106-
await GetDirectoryAsync(path, cancellationToken);
107-
path = PrependRootPath(path);
106+
await GetDirectoryAsync(virtualPath, cancellationToken);
107+
var path = GetPath(virtualPath);
108108

109109
try
110110
{
@@ -119,7 +119,7 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", C
119119

120120
if (!item.Key.EndsWith("/") && !itemName.Contains('/'))
121121
{
122-
files.Add(ModelFactory.CreateFile(item));
122+
files.Add(ModelFactory.CreateFile(item, GetVirtualPath(item.Key)));
123123
}
124124
}
125125

@@ -131,13 +131,10 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", C
131131
}
132132
}
133133

134-
public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
135-
string path = "",
136-
CancellationToken cancellationToken = default
137-
)
134+
public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string virtualPath = "", CancellationToken cancellationToken = default)
138135
{
139-
await GetDirectoryAsync(path, cancellationToken);
140-
path = PrependRootPath(path);
136+
await GetDirectoryAsync(virtualPath, cancellationToken);
137+
var path = GetPath(virtualPath);
141138

142139
try
143140
{
@@ -152,7 +149,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
152149

153150
if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1)
154151
{
155-
directories.Add(ModelFactory.CreateDirectory(item));
152+
directories.Add(ModelFactory.CreateDirectory(item, GetVirtualPath(item.Key)));
156153
}
157154
}
158155

@@ -164,14 +161,14 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
164161
}
165162
}
166163

167-
public override async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
164+
public override async Task CreateDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default)
168165
{
169-
if (await DirectoryExistsAsync(path, cancellationToken))
166+
if (await DirectoryExistsAsync(virtualPath, cancellationToken))
170167
{
171-
throw new DirectoryExistsException(PrependRootPath(path), Prefix);
168+
throw new DirectoryExistsException(GetPath(virtualPath), Prefix);
172169
}
173170

174-
path = PrependRootPath(path);
171+
var path = GetPath(virtualPath);
175172
path = path.EndsWith("/") ? path : path + "/";
176173

177174
try
@@ -185,11 +182,11 @@ public override async Task CreateDirectoryAsync(string path, CancellationToken c
185182
}
186183
}
187184

188-
public override async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
185+
public override async Task DeleteDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default)
189186
{
190-
await GetDirectoryAsync(path, cancellationToken);
187+
await GetDirectoryAsync(virtualPath, cancellationToken);
191188

192-
path = PrependRootPath(path);
189+
var path = GetPath(virtualPath);
193190
path = path.EndsWith("/") ? path : path + "/";
194191

195192
try
@@ -212,10 +209,10 @@ public override async Task DeleteDirectoryAsync(string path, CancellationToken c
212209
}
213210
}
214211

215-
public override async Task DeleteFileAsync(string path, CancellationToken cancellationToken = default)
212+
public override async Task DeleteFileAsync(string virtualPath, CancellationToken cancellationToken = default)
216213
{
217-
await GetFileAsync(path, cancellationToken);
218-
path = PrependRootPath(path);
214+
await GetFileAsync(virtualPath, cancellationToken);
215+
var path = GetPath(virtualPath);
219216

220217
try
221218
{
@@ -227,10 +224,10 @@ public override async Task DeleteFileAsync(string path, CancellationToken cancel
227224
}
228225
}
229226

230-
public override async Task<byte[]> ReadFileAsync(string path, CancellationToken cancellationToken = default)
227+
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
231228
{
232-
await GetFileAsync(path, cancellationToken);
233-
path = PrependRootPath(path);
229+
await GetFileAsync(virtualPath, cancellationToken);
230+
var path = GetPath(virtualPath);
234231

235232
try
236233
{
@@ -246,10 +243,10 @@ public override async Task<byte[]> ReadFileAsync(string path, CancellationToken
246243
}
247244
}
248245

249-
public override async Task<string> ReadTextFileAsync(string path, CancellationToken cancellationToken = default)
246+
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
250247
{
251-
await GetFileAsync(path, cancellationToken);
252-
path = PrependRootPath(path);
248+
await GetFileAsync(virtualPath, cancellationToken);
249+
var path = GetPath(virtualPath);
253250

254251
try
255252
{
@@ -268,19 +265,14 @@ public override async Task<string> ReadTextFileAsync(string path, CancellationTo
268265
}
269266
}
270267

271-
public override async Task WriteFileAsync(
272-
string path,
273-
byte[] contents,
274-
bool overwrite = false,
275-
CancellationToken cancellationToken = default
276-
)
268+
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
277269
{
278-
if (!overwrite && await FileExistsAsync(path, cancellationToken))
270+
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
279271
{
280-
throw new FileExistsException(PrependRootPath(path), Prefix);
272+
throw new FileExistsException(GetPath(virtualPath), Prefix);
281273
}
282274

283-
path = PrependRootPath(path);
275+
var path = GetPath(virtualPath);
284276

285277
try
286278
{
@@ -300,14 +292,14 @@ public override async Task WriteFileAsync(
300292
}
301293
}
302294

303-
public override async Task AppendFileAsync(string path, byte[] contents, CancellationToken cancellationToken = default)
295+
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
304296
{
305-
await GetFileAsync(path, cancellationToken);
306-
var existingContents = await ReadFileAsync(path, cancellationToken);
297+
await GetFileAsync(virtualPath, cancellationToken);
298+
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
307299
contents = existingContents.Concat(contents).ToArray();
308-
await DeleteFileAsync(path, cancellationToken);
300+
await DeleteFileAsync(virtualPath, cancellationToken);
309301

310-
path = PrependRootPath(path);
302+
var path = GetPath(virtualPath);
311303

312304
try
313305
{

FileSystem.Adapters.AmazonS3/src/ModelFactory.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ namespace SharpGrip.FileSystem.Adapters.AmazonS3
77
{
88
public static class ModelFactory
99
{
10-
public static FileModel CreateFile(GetObjectResponse file)
10+
public static FileModel CreateFile(GetObjectResponse file, string virtualPath)
1111
{
1212
return new FileModel
1313
{
1414
Name = file.Key.Split('/').Last(),
1515
Path = file.Key,
16+
VirtualPath = virtualPath,
1617
Length = file.ContentLength,
17-
LastModifiedDateTime = file.LastModified,
18+
LastModifiedDateTime = file.LastModified
1819
};
1920
}
2021

21-
public static FileModel CreateFile(S3Object file)
22+
public static FileModel CreateFile(S3Object file, string virtualPath)
2223
{
2324
return new FileModel
2425
{
2526
Name = file.Key.Split('/').Last(),
2627
Path = file.Key,
28+
VirtualPath = virtualPath,
2729
Length = file.Size,
2830
LastModifiedDateTime = file.LastModified
2931
};
3032
}
3133

32-
public static DirectoryModel CreateDirectory(S3Object directory)
34+
public static DirectoryModel CreateDirectory(S3Object directory, string virtualPath)
3335
{
3436
var pathParts = directory.Key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
3537
var name = pathParts.Last();
@@ -43,6 +45,7 @@ public static DirectoryModel CreateDirectory(S3Object directory)
4345
{
4446
Name = name.Substring(0, name.Length - 1),
4547
Path = directory.Key.Substring(0, name.Length - 1),
48+
VirtualPath = virtualPath,
4649
LastModifiedDateTime = directory.LastModified
4750
};
4851
}

0 commit comments

Comments
 (0)