11using Azure . Storage . Blobs ;
2+ using Azure . Storage . Blobs . Models ;
23using Azure . Storage . Queues ;
34using Microsoft . Azure . Functions . Worker ;
45using Microsoft . EntityFrameworkCore ;
@@ -15,20 +16,19 @@ public class ExtractFunction
1516 private readonly IMeshInboxService _meshInboxService ;
1617 private readonly ServiceLayerDbContext _serviceLayerDbContext ;
1718 private readonly QueueClient _queueClient ;
18- private readonly BlobClient _blobClient ;
19+ private readonly BlobContainerClient _blobContainerClient ;
1920
20-
21- public ExtractFunction ( ILogger logger , IMeshInboxService meshInboxService , ServiceLayerDbContext serviceLayerDbContext , QueueClient queueClient , BlobClient blobClient )
21+ public ExtractFunction ( ILogger logger , IMeshInboxService meshInboxService , ServiceLayerDbContext serviceLayerDbContext , QueueClient queueClient , BlobContainerClient blobClient )
2222 {
2323 _logger = logger ;
2424 _meshInboxService = meshInboxService ;
2525 _serviceLayerDbContext = serviceLayerDbContext ;
2626 _queueClient = queueClient ;
27- _blobClient = blobClient ;
27+ _blobContainerClient = blobClient ;
2828 }
2929
3030 [ Function ( "ExtractFunction" ) ]
31- public async Task Run ( [ QueueTrigger ( "TODO " ) ] ExtractQueueMessage message )
31+ public async Task Run ( [ QueueTrigger ( "my-local-queue " ) ] ExtractQueueMessage message ) // TODO: Queue name
3232 {
3333 _logger . LogInformation ( $ "ExtractFunction started at: { DateTime . Now } ") ;
3434
@@ -59,17 +59,54 @@ public async Task Run([QueueTrigger("TODO")] ExtractQueueMessage message)
5959 await _serviceLayerDbContext . SaveChangesAsync ( ) ;
6060 await transaction . CommitAsync ( ) ;
6161
62- var mailboxId = Environment . GetEnvironmentVariable ( "BSSMailBox " )
63- ?? throw new InvalidOperationException ( $ "Environment variable 'BSSMailBox ' is not set or is empty.") ;
62+ var mailboxId = Environment . GetEnvironmentVariable ( "MeshMailboxId " )
63+ ?? throw new InvalidOperationException ( $ "Environment variable 'MeshMailboxId ' is not set or is empty.") ;
6464
6565 var meshResponse = await _meshInboxService . GetMessageByIdAsync ( mailboxId , file . FileId ) ;
6666 if ( ! meshResponse . IsSuccessful )
6767 {
6868 // TODO - what to do if unsuccessful?
6969 throw new InvalidOperationException ( $ "Mesh extraction failed: { meshResponse . Error } ") ;
7070 }
71+
72+ await UploadFileToBlobStorage ( new BlobFile ( meshResponse . Response . FileAttachment . Content , mailboxId ) ) ;
73+ }
74+
75+ public async Task < bool > UploadFileToBlobStorage ( BlobFile blobFile , bool overwrite = false )
76+ {
77+ var blobClient = _blobContainerClient . GetBlobClient ( blobFile . FileName ) ;
78+
79+ await _blobContainerClient . CreateIfNotExistsAsync ( PublicAccessType . None ) ;
80+
81+ try
82+ {
83+ await blobClient . UploadAsync ( blobFile . Data , overwrite : overwrite ) ;
84+ }
85+ catch ( Exception ex )
86+ {
87+ _logger . LogError ( ex , "There has been a problem while uploading the file: {Message}" , ex . Message ) ;
88+ return false ;
89+ }
90+
91+ return true ;
92+ }
93+ }
94+
95+ public class BlobFile
96+ {
97+ public BlobFile ( byte [ ] bytes , string fileName )
98+ {
99+ Data = new MemoryStream ( bytes ) ;
100+ FileName = fileName ;
101+ }
102+ public BlobFile ( Stream stream , string fileName )
103+ {
104+ Data = stream ;
105+ FileName = fileName ;
71106 }
72107
108+ public Stream Data { get ; set ; }
109+ public string FileName { get ; set ; }
73110}
74111
75112public class ExtractQueueMessage
0 commit comments