Skip to content

Commit a64c958

Browse files
author
Frank Li (Wicresoft North America Ltd)
committed
refine the code based on comments.
1 parent f9b8fdf commit a64c958

File tree

16 files changed

+178
-1235
lines changed

16 files changed

+178
-1235
lines changed

audio/src/ContentReactor.Audio/ContentReactor.Audio.Api/ApiFunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using ContentReactor.Audio.Services.Models.Requests;
88
using ContentReactor.Audio.Services.Models.Results;
99
using ContentReactor.Shared;
10-
using ContentReactor.Shared.BlobRepository;
10+
using ContentReactor.Shared.BlobHelper;
1111
using ContentReactor.Shared.UserAuthentication;
1212
using Microsoft.AspNetCore.Mvc;
1313
using Microsoft.Azure.WebJobs;
@@ -21,7 +21,7 @@ namespace ContentReactor.Audio.Api
2121
public static class ApiFunctions
2222
{
2323
private const string JsonContentType = "application/json";
24-
public static IAudioService AudioService = new AudioService(new BlobRepository(), new AudioTranscriptionService(), new EventGridPublisherService());
24+
public static IAudioService AudioService = new AudioService(new BlobHelper(), new AudioTranscriptionService(), new EventGridPublisherService());
2525
public static IUserAuthenticationService UserAuthenticationService = new QueryStringUserAuthenticationService();
2626

2727
[FunctionName("BeginCreateAudio")]

audio/src/ContentReactor.Audio/ContentReactor.Audio.Services.Tests/AudioServiceTests.cs

Lines changed: 0 additions & 407 deletions
This file was deleted.

audio/src/ContentReactor.Audio/ContentReactor.Audio.Services.Tests/ContentReactor.Audio.Services.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj" />
1817
<ProjectReference Include="..\ContentReactor.Audio.Services\ContentReactor.Audio.Services.csproj" />
1918
</ItemGroup>
2019

audio/src/ContentReactor.Audio/ContentReactor.Audio.Services/AudioService.cs

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using Azure.Storage.Sas;
2-
using ContentReactor.Audio.Services.Models.Responses;
3-
using ContentReactor.Audio.Services.Models.Results;
4-
using ContentReactor.Shared;
5-
using ContentReactor.Shared.BlobRepository;
6-
using ContentReactor.Shared.EventSchemas.Audio;
7-
using System;
1+
using System;
82
using System.Collections.Generic;
93
using System.IO;
104
using System.Linq;
115
using System.Threading.Tasks;
6+
using Azure;
7+
using Azure.Storage.Blobs.Models;
8+
using Azure.Storage.Blobs.Specialized;
9+
using Azure.Storage.Sas;
10+
using ContentReactor.Audio.Services.Models.Responses;
11+
using ContentReactor.Audio.Services.Models.Results;
12+
using ContentReactor.Shared;
13+
using ContentReactor.Shared.BlobHelper;
14+
using ContentReactor.Shared.EventSchemas.Audio;
1215

1316
namespace ContentReactor.Audio.Services
1417
{
@@ -24,7 +27,7 @@ public interface IAudioService
2427

2528
public class AudioService : IAudioService
2629
{
27-
protected IBlobRepository BlobRepository;
30+
protected BlobHelper BlobHelper;
2831
protected IAudioTranscriptionService AudioTranscriptionService;
2932
protected IEventGridPublisherService EventGridPublisherService;
3033

@@ -34,59 +37,51 @@ public class AudioService : IAudioService
3437
protected internal const string UserIdMetadataName = "userId";
3538
protected internal const int TranscriptPreviewLength = 100;
3639

37-
public AudioService(IBlobRepository blobRepository, IAudioTranscriptionService audioTranscriptionService, IEventGridPublisherService eventGridPublisherService)
40+
public AudioService(BlobHelper blobHelper, IAudioTranscriptionService audioTranscriptionService, IEventGridPublisherService eventGridPublisherService)
3841
{
39-
BlobRepository = blobRepository;
42+
BlobHelper = blobHelper;
4043
AudioTranscriptionService = audioTranscriptionService;
4144
EventGridPublisherService = eventGridPublisherService;
4245
}
4346

4447
public (string id, string url) BeginAddAudioNote(string userId)
4548
{
4649
// generate an ID for this image note
47-
var audioId = Guid.NewGuid().ToString();
50+
string audioId = Guid.NewGuid().ToString();
4851

4952
// create a blob placeholder (which will not have any contents yet)
50-
var blob = BlobRepository.CreatePlaceholderBlob(AudioBlobContainerName, audioId);
51-
52-
// get a SAS token to allow the client to write the blob
53-
BlobSasBuilder sasBuilder = new BlobSasBuilder
54-
{
55-
StartsOn = DateTime.UtcNow.AddMinutes(-5),
56-
ExpiresOn = DateTime.UtcNow.AddHours(24),
57-
};
53+
BlockBlobClient blob = BlobHelper.GetBlobClient(AudioBlobContainerName, audioId);
5854

59-
sasBuilder.SetPermissions(BlobSasPermissions.Create);
60-
sasBuilder.SetPermissions(BlobSasPermissions.Write);
6155

62-
var url = BlobRepository.GetSasTokenForBlob(blob, sasBuilder);
56+
string urlAndSas = BlobHelper.GetSasUriForBlob(blob, BlobSasPermissions.Create | BlobSasPermissions.Write);
6357

64-
return (audioId, url);
58+
return (audioId, urlAndSas);
6559
}
6660

6761
public async Task<CompleteAddAudioNoteResult> CompleteAddAudioNoteAsync(string audioId, string userId, string categoryId)
6862
{
69-
var imageBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, audioId);
70-
if (imageBlob == null || !await BlobRepository.BlobExistsAsync(imageBlob))
63+
BlockBlobClient imageBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, audioId);
64+
if (imageBlob == null || !await imageBlob.ExistsAsync())
7165
{
7266
// the blob hasn't actually been uploaded yet, so we can't process it
7367
return CompleteAddAudioNoteResult.AudioNotUploaded;
7468
}
7569

76-
var response = await imageBlob.GetPropertiesAsync();
70+
// if the blob already contains metadata then that means it has already been added
71+
Response<BlobProperties> response = await imageBlob.GetPropertiesAsync();
7772
if (response.Value.Metadata.ContainsKey(CategoryIdMetadataName))
7873
{
7974
return CompleteAddAudioNoteResult.AudioAlreadyCreated;
8075
}
8176

8277
// set the blob metadata
83-
var metaData = new Dictionary<string, string>
78+
var metadata = new Dictionary<string, string>
8479
{
8580
{ CategoryIdMetadataName, categoryId },
8681
{ UserIdMetadataName, userId }
8782
};
8883

89-
await BlobRepository.UpdateBlobMetadataAsync(imageBlob, metaData);
84+
await imageBlob.SetMetadataAsync(metadata);
9085

9186
// publish an event into the Event Grid topic
9287
var subject = $"{userId}/{audioId}";
@@ -98,37 +93,29 @@ public async Task<CompleteAddAudioNoteResult> CompleteAddAudioNoteAsync(string a
9893
public async Task<AudioNoteDetails> GetAudioNoteAsync(string id, string userId)
9994
{
10095
// get the blob, if it exists
101-
var audioBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, id);
96+
BlockBlobClient audioBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
10297
if (audioBlob == null)
10398
{
10499
return null;
105100
}
106101

107-
// get a SAS token for the blob
108-
BlobSasBuilder sasBuilder = new BlobSasBuilder
109-
{
110-
StartsOn = DateTime.UtcNow.AddMinutes(-5),
111-
ExpiresOn = DateTime.UtcNow.AddHours(24),
112-
};
113-
114-
sasBuilder.SetPermissions(BlobSasPermissions.Read);
115-
var audioUrl = BlobRepository.GetSasTokenForBlob(audioBlob, sasBuilder);
102+
string audioUrlAndSas = BlobHelper.GetSasUriForBlob(audioBlob, BlobSasPermissions.Read);
116103

117104
// get the transcript out of the blob metadata
118-
var response = await audioBlob.GetPropertiesAsync();
105+
Response<BlobProperties> response = await audioBlob.GetPropertiesAsync();
119106
response.Value.Metadata.TryGetValue(TranscriptMetadataName, out var transcript);
120107

121108
return new AudioNoteDetails
122109
{
123110
Id = id,
124-
AudioUrl = audioUrl,
111+
AudioUrl = audioUrlAndSas,
125112
Transcript = transcript
126113
};
127114
}
128115

129116
public async Task<AudioNoteSummaries> ListAudioNotesAsync(string userId)
130117
{
131-
var blobs = await BlobRepository.ListBlobsAsync(AudioBlobContainerName);
118+
IList<BlobItem> blobs = await BlobHelper.ListBlobsAsync(AudioBlobContainerName);
132119
var blobSummaries = blobs
133120
.Select(b => new AudioNoteSummary
134121
{
@@ -146,7 +133,8 @@ public async Task<AudioNoteSummaries> ListAudioNotesAsync(string userId)
146133
public async Task DeleteAudioNoteAsync(string id, string userId)
147134
{
148135
// delete the blog
149-
await BlobRepository.DeleteBlobAsync(AudioBlobContainerName, id);
136+
BlockBlobClient blob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
137+
await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
150138

151139
// fire an event into the Event Grid topic
152140
var subject = $"{userId}/{id}";
@@ -156,7 +144,7 @@ public async Task DeleteAudioNoteAsync(string id, string userId)
156144
public async Task<string> UpdateAudioTranscriptAsync(string id, string userId)
157145
{
158146
// get the blob
159-
var audioBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, id);
147+
BlockBlobClient audioBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
160148
if (audioBlob == null)
161149
{
162150
return null;
@@ -166,21 +154,21 @@ public async Task<string> UpdateAudioTranscriptAsync(string id, string userId)
166154
string transcript;
167155
using (var audioBlobStream = new MemoryStream())
168156
{
169-
await BlobRepository.DownloadBlobAsync(audioBlob, audioBlobStream);
157+
await audioBlob.DownloadToAsync(audioBlobStream);
170158

171159
// send to Cognitive Services and get back a transcript
172160
transcript = await AudioTranscriptionService.GetAudioTranscriptFromCognitiveServicesAsync(audioBlobStream);
173161
}
174162

175163
// update the blob's metadata
176-
var metaData = new Dictionary<string, string>
164+
var metadata = new Dictionary<string, string>
177165
{
178166
{ TranscriptMetadataName, transcript }
179167
};
180168

181-
await audioBlob.SetMetadataAsync(metaData);
169+
await audioBlob.SetMetadataAsync(metadata);
182170
// create a preview form of the transcript
183-
var transcriptPreview = transcript.Truncate(TranscriptPreviewLength);
171+
string transcriptPreview = transcript.Truncate(TranscriptPreviewLength);
184172

185173
// fire an event into the Event Grid topic
186174
var subject = $"{userId}/{id}";

audio/src/ContentReactor.Audio/ContentReactor.Audio.WorkerApi/WorkerApiFunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
using Microsoft.Azure.WebJobs.Host;
99
using ContentReactor.Audio.Services;
1010
using ContentReactor.Shared;
11-
using ContentReactor.Shared.BlobRepository;
11+
using ContentReactor.Shared.BlobHelper;
1212

1313
namespace ContentReactor.Audio.WorkerApi
1414
{
1515
public static class WorkerApiFunctions
1616
{
1717
private static readonly IEventGridSubscriberService EventGridSubscriberService = new EventGridSubscriberService();
18-
private static readonly IAudioService AudioService = new AudioService(new BlobRepository(), new AudioTranscriptionService(), new EventGridPublisherService());
18+
private static readonly IAudioService AudioService = new AudioService(new BlobHelper(), new AudioTranscriptionService(), new EventGridPublisherService());
1919

2020
[FunctionName("UpdateAudioTranscript")]
2121
public static async Task<IActionResult> UpdateAudioTranscript(

audio/src/ContentReactor.Audio/ContentReactor.Audio.sln

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2024
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30503.244
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Shared", "..\..\..\shared\src\ContentReactor.Shared\ContentReactor.Shared.csproj", "{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Tests.FakeBlobRepository", "..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj", "{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}"
9-
EndProject
108
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Audio.Api", "ContentReactor.Audio.Api\ContentReactor.Audio.Api.csproj", "{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Audio.Services", "ContentReactor.Audio.Services\ContentReactor.Audio.Services.csproj", "{60520F7F-2ED7-47CE-BB70-0877CB535DAA}"
@@ -27,10 +25,6 @@ Global
2725
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
2826
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
2927
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Release|Any CPU.Build.0 = Release|Any CPU
30-
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31-
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
32-
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
33-
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Release|Any CPU.Build.0 = Release|Any CPU
3428
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3529
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
3630
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Release|Any CPU.ActiveCfg = Release|Any CPU

images/src/ContentReactor.Images/ContentReactor.Images.Api/ApiFunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Microsoft.Azure.WebJobs.Extensions.Http;
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.Azure.WebJobs.Host;
12-
using ContentReactor.Shared.BlobRepository;
12+
using ContentReactor.Shared.BlobHelper;
1313
using System.IO;
1414
using System.Net.Http;
1515
using ContentReactor.Images.Services.Converters;
@@ -22,7 +22,7 @@ namespace ContentReactor.Images.Api
2222
public static class ApiFunctions
2323
{
2424
private const string JsonContentType = "application/json";
25-
public static IImagesService ImagesService = new ImagesService(new BlobRepository(), new ImageValidatorService(), new ImagePreviewService(), new ImageCaptionService(new HttpClient()), new EventGridPublisherService());
25+
public static IImagesService ImagesService = new ImagesService(new BlobHelper(), new ImageValidatorService(), new ImagePreviewService(), new ImageCaptionService(new HttpClient()), new EventGridPublisherService());
2626
public static IUserAuthenticationService UserAuthenticationService = new QueryStringUserAuthenticationService();
2727

2828
[FunctionName("BeginCreateImage")]

images/src/ContentReactor.Images/ContentReactor.Images.Services.Tests/ContentReactor.Images.Services.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
<ItemGroup>
3030
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Shared\ContentReactor.Shared.csproj" />
31-
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj" />
3231
<ProjectReference Include="..\ContentReactor.Images.Services\ContentReactor.Images.Services.csproj" />
3332
</ItemGroup>
3433

0 commit comments

Comments
 (0)