Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit e70e497

Browse files
feat: Implemented FileExtract Function
1 parent 4f64ffd commit e70e497

File tree

3 files changed

+47
-55
lines changed

3 files changed

+47
-55
lines changed
Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Azure.Storage.Blobs.Models;
21
using Microsoft.Azure.Functions.Worker;
32
using Microsoft.EntityFrameworkCore;
43
using Microsoft.Extensions.Logging;
@@ -15,6 +14,7 @@ public class FileExtractFunction(
1514
IMeshInboxService meshInboxService,
1615
ServiceLayerDbContext serviceLayerDbContext,
1716
IFileTransformQueueClient fileTransformQueueClient,
17+
IFileExtractQueueClient fileExtractQueueClient,
1818
IMeshFilesBlobStore meshFileBlobStore)
1919
{
2020
[Function("FileExtractFunction")]
@@ -51,57 +51,41 @@ public async Task Run([QueueTrigger("file-extract")] FileExtractQueueMessage mes
5151
await serviceLayerDbContext.SaveChangesAsync();
5252
await transaction.CommitAsync();
5353

54-
// TODO - approx after this point we'll need to wrap everything in a try-catch. On failure we should
55-
// update the meshfile to FailedExtract, and move the message to the poison queue
56-
57-
var mailboxId = Environment.GetEnvironmentVariable("NBSSMailBoxId")
58-
?? throw new InvalidOperationException($"Environment variable 'NBSSMailBoxId' is not set or is empty.");
59-
60-
var meshResponse = await meshInboxService.GetMessageByIdAsync(mailboxId, file.FileId);
61-
if (!meshResponse.IsSuccessful)
54+
try
6255
{
63-
// TODO - what to do if unsuccessful?
64-
throw new InvalidOperationException($"Mesh extraction failed: {meshResponse.Error}");
56+
var mailboxId = Environment.GetEnvironmentVariable("NBSSMailBoxId")
57+
?? throw new InvalidOperationException($"Environment variable 'NBSSMailBoxId' is not set or is empty.");
58+
59+
var meshResponse = await meshInboxService.GetMessageByIdAsync(mailboxId, file.FileId);
60+
if (!meshResponse.IsSuccessful)
61+
{
62+
// TODO - what to do if unsuccessful?
63+
throw new InvalidOperationException($"Mesh extraction failed: {meshResponse.Error}");
64+
}
65+
66+
var blobPath = await meshFileBlobStore.UploadAsync(file, meshResponse.Response.FileAttachment.Content);
67+
68+
var meshAcknowledgementResponse = await meshInboxService.AcknowledgeMessageByIdAsync(mailboxId, message.FileId);
69+
if (!meshResponse.IsSuccessful)
70+
{
71+
// TODO - what to do if unsuccessful?
72+
throw new InvalidOperationException($"Mesh acknowledgement failed: {meshResponse.Error}");
73+
}
74+
75+
file.Status = MeshFileStatus.Extracted;
76+
file.LastUpdatedUtc = DateTime.UtcNow;
77+
file.BlobPath = blobPath;
78+
await serviceLayerDbContext.SaveChangesAsync();
79+
80+
await fileTransformQueueClient.EnqueueFileTransformAsync(file);
81+
}
82+
catch (Exception ex)
83+
{
84+
logger.LogError(ex, "");
85+
file.Status = MeshFileStatus.FailedExtract;
86+
file.LastUpdatedUtc = DateTime.UtcNow;
87+
await serviceLayerDbContext.SaveChangesAsync();
88+
await fileExtractQueueClient.SendToPoisonQueueAsync(message);
6589
}
66-
67-
await meshFileBlobStore.UploadAsync(file, meshResponse.Response.FileAttachment.Content);
6890
}
69-
70-
// public async Task<bool> UploadFileToBlobStorage(BlobFile blobFile, bool overwrite = false)
71-
// {
72-
// var blobClient = blobContainerClient.GetBlobClient(blobFile.FileName);
73-
//
74-
// await blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.None);
75-
//
76-
// try
77-
// {
78-
// await blobClient.UploadAsync(blobFile.Data, overwrite: overwrite);
79-
// }
80-
// catch (Exception ex)
81-
// {
82-
// logger.LogError(ex, "There has been a problem while uploading the file: {Message}", ex.Message);
83-
// return false;
84-
// }
85-
//
86-
// return true;
87-
// }
8891
}
89-
90-
// public class BlobFile
91-
// {
92-
// public BlobFile(byte[] bytes, string fileName)
93-
// {
94-
// Data = new MemoryStream(bytes);
95-
// FileName = fileName;
96-
// }
97-
// public BlobFile(Stream stream, string fileName)
98-
// {
99-
// Data = stream;
100-
// FileName = fileName;
101-
// }
102-
//
103-
// public Stream Data { get; set; }
104-
// public string FileName { get; set; }
105-
// }
106-
107-

src/ServiceLayer.Mesh/Storage/IMeshFilesBlobStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public interface IMeshFilesBlobStore
88
public Task<Stream> DownloadAsync(MeshFile file);
99

1010
// Mesh client gives us a byte array, hence this not taking a stream.
11-
public Task UploadAsync(MeshFile file, byte[] data);
11+
public Task<string> UploadAsync(MeshFile file, byte[] data);
1212
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
using Azure.Storage.Blobs;
12
using ServiceLayer.Mesh.Models;
23

34
namespace ServiceLayer.Mesh.Storage;
45

5-
public class MeshFilesBlobStore : IMeshFilesBlobStore
6+
public class MeshFilesBlobStore(BlobContainerClient blobContainerClient) : IMeshFilesBlobStore
67
{
78
public Task<Stream> DownloadAsync(MeshFile file)
89
{
910
throw new NotImplementedException();
1011
}
1112

12-
public Task UploadAsync(MeshFile file, byte[] data)
13+
public async Task<string> UploadAsync(MeshFile file, byte[] data)
1314
{
14-
throw new NotImplementedException();
15+
var blobPath = $"{file.FileType}/{file.FileId}";
16+
var blobClient = blobContainerClient.GetBlobClient(blobPath);
17+
18+
var dataStream = new MemoryStream(data);
19+
20+
await blobClient.UploadAsync(dataStream, overwrite: true);
21+
22+
return blobPath;
1523
}
1624
}

0 commit comments

Comments
 (0)