Skip to content

Commit 98a6d8c

Browse files
committed
Added new endpoint to get files with a given extension
1 parent a27f74b commit 98a6d8c

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

src/Pixel.Persistence.Respository/FilesRepository.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,41 @@ public async IAsyncEnumerable<ProjectDataFile> GetFilesAsync(string projectId, s
158158
}
159159
}
160160

161+
/// <inheritdoc/>
162+
public async IAsyncEnumerable<ProjectDataFile> GetFilesOfTypeAsync(string projectId, string projectVersion, string fileExtension)
163+
{
164+
Guard.Argument(projectId, nameof(projectId)).NotNull().NotEmpty();
165+
Guard.Argument(projectVersion, nameof(projectVersion)).NotNull().NotEmpty();
166+
Guard.Argument(fileExtension, nameof(fileExtension)).NotNull().NotEmpty();
167+
168+
var filter = Builders<GridFSFileInfo>.Filter.Eq(x => x.Metadata[this.IdField], projectId) &
169+
Builders<GridFSFileInfo>.Filter.Eq(x => x.Metadata[this.versionField], projectVersion) &
170+
Builders<GridFSFileInfo>.Filter.Regex(x => x.Filename, $"({fileExtension})$");
171+
172+
var sort = Builders<GridFSFileInfo>.Sort.Descending(x => x.UploadDateTime);
173+
var options = new GridFSFindOptions
174+
{
175+
Sort = sort
176+
};
177+
178+
using (var cursor = await bucket.FindAsync(filter, options))
179+
{
180+
foreach (var file in (await cursor.ToListAsync()))
181+
{
182+
var fileData = await bucket.DownloadAsBytesAsync(file.Id);
183+
yield return new ProjectDataFile()
184+
{
185+
ProjectId = projectId,
186+
ProjectVersion = projectVersion,
187+
FileName = file.Filename,
188+
FilePath = file.Metadata[this.filePathField].AsString,
189+
Tag = file.Metadata[this.tagField].AsString,
190+
Bytes = fileData
191+
};
192+
}
193+
}
194+
}
195+
161196
/// <inheritdoc/>
162197
public async Task AddOrUpdateFileAsync(string projectId, string projectVersion, ProjectDataFile dataFile)
163198
{

src/Pixel.Persistence.Respository/Interfaces/IFilesRepository.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@ public interface IFilesRepository
2525
/// <returns></returns>
2626
IAsyncEnumerable<ProjectDataFile> GetFilesAsync(string projectId, string projectVersion, string[] tags);
2727

28+
/// <summary>
29+
/// Get all the files with a matching extension
30+
/// </summary>
31+
/// <param name="projectId"></param>
32+
/// <param name="projectVersion"></param>
33+
/// <param name="fileExtension"></param>
34+
/// <returns></returns>
35+
IAsyncEnumerable<ProjectDataFile> GetFilesOfTypeAsync(string projectId, string projectVersion, string fileExtension);
36+
2837
/// <summary>
2938
/// Get a file by name for a given version of project
3039
/// </summary>
3140
/// <param name="projectId"></param>
3241
/// <param name="projectVersion"></param>
3342
/// <param name="fileName"></param>
3443
/// <returns></returns>
35-
Task<ProjectDataFile> GetFileAsync(string projectId, string projectVersion, string fileName);
44+
Task<ProjectDataFile> GetFileAsync(string projectId, string projectVersion, string fileName);
3645

3746
/// <summary>
3847
/// Add a new file to a given version of project

src/Pixel.Persistence.Services.Api/Controllers/FilesController.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,37 @@ public async Task<ActionResult> GetFilesWithTag(string projectId, string project
117117
}
118118
}
119119

120+
[HttpGet("{projectId}/{projectVersion}/type/{fileExtension}")]
121+
public async Task<ActionResult> GetFilesOfType(string projectId, string projectVersion, string fileExtension)
122+
{
123+
try
124+
{
125+
using (var stream = new MemoryStream())
126+
{
127+
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
128+
{
129+
await foreach (var file in this.filesRepository.GetFilesOfTypeAsync(projectId, projectVersion, fileExtension))
130+
{
131+
var zipEntry = zipArchive.CreateEntry(Path.Combine(file.FilePath));
132+
using (var zipEntryStream = zipEntry.Open())
133+
{
134+
zipEntryStream.Write(file.Bytes);
135+
}
136+
}
137+
}
138+
return File(stream.ToArray(), "application/zip", $"{projectId}.zip");
139+
140+
}
141+
142+
}
143+
catch (Exception ex)
144+
{
145+
logger.LogError(ex, ex.Message);
146+
return Problem(ex.Message, statusCode: StatusCodes.Status500InternalServerError);
147+
}
148+
}
149+
150+
120151
/// <summary>
121152
/// Add a new file to a given version of project
122153
/// </summary>

src/Pixel.Persistence.Services.Client/FilesRepositoryClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public async Task<byte[]> DownloadProjectDataFilesWithTags(string projectId, str
7979
return result.RawBytes;
8080
}
8181

82+
/// <inheritdoc/>
83+
public async Task<byte[]> DownloadProjectDataFilesOfType(string projectId, string projectVersion, string fileExtension)
84+
{
85+
Guard.Argument(projectId, nameof(projectId)).NotNull().NotEmpty();
86+
Guard.Argument(projectVersion, nameof(projectVersion)).NotNull().NotEmpty();
87+
Guard.Argument(fileExtension, nameof(fileExtension)).NotNull().NotEmpty();
88+
89+
RestRequest restRequest = new RestRequest($"{baseUrl}/{projectId}/{projectVersion}/type/{fileExtension}");
90+
var client = this.clientFactory.GetOrCreateClient();
91+
var result = await client.ExecuteGetAsync(restRequest);
92+
result.EnsureSuccess();
93+
return result.RawBytes;
94+
}
95+
8296
/// <inheritdoc/>
8397
public async Task<byte[]> DownProjectDataFile(string projectId, string projectVersion, string fileName)
8498
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public interface IFilesRepositoryClient
4242
/// <returns></returns>
4343
Task<byte[]> DownloadProjectDataFilesWithTags(string projectId, string projectVersion, string[] tags);
4444

45+
/// <summary>
46+
/// Download data files with matching extension
47+
/// </summary>
48+
/// <param name="projectId"></param>
49+
/// <param name="projectVersion"></param>
50+
/// <param name="fileExtension"></param>
51+
/// <returns></returns>
52+
Task<byte[]> DownloadProjectDataFilesOfType(string projectId, string projectVersion, string fileExtension);
53+
4554
/// <summary>
4655
/// Add a project data file for a given version of project
4756
/// </summary>

0 commit comments

Comments
 (0)