Skip to content

Commit

Permalink
Merge pull request #22 from wantedfast/Dev-updateSDK
Browse files Browse the repository at this point in the history
Update Storage Blob SDK to Track2.
  • Loading branch information
jongio authored Oct 19, 2020
2 parents da129fc + 1b0c043 commit 079593f
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 1,227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using ContentReactor.Audio.Services.Models.Requests;
using ContentReactor.Audio.Services.Models.Results;
using ContentReactor.Shared;
using ContentReactor.Shared.BlobRepository;
using ContentReactor.Shared.BlobHelper;
using ContentReactor.Shared.UserAuthentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
Expand All @@ -21,7 +21,7 @@ namespace ContentReactor.Audio.Api
public static class ApiFunctions
{
private const string JsonContentType = "application/json";
public static IAudioService AudioService = new AudioService(new BlobRepository(), new AudioTranscriptionService(), new EventGridPublisherService());
public static IAudioService AudioService = new AudioService(new BlobHelper(), new AudioTranscriptionService(), new EventGridPublisherService());
public static IUserAuthenticationService UserAuthenticationService = new QueryStringUserAuthenticationService();

[FunctionName("BeginCreateAudio")]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj" />
<ProjectReference Include="..\ContentReactor.Audio.Services\ContentReactor.Audio.Services.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Sas;
using ContentReactor.Audio.Services.Models.Responses;
using ContentReactor.Audio.Services.Models.Results;
using ContentReactor.Shared;
using ContentReactor.Shared.BlobRepository;
using ContentReactor.Shared.BlobHelper;
using ContentReactor.Shared.EventSchemas.Audio;
using Microsoft.WindowsAzure.Storage.Blob;

namespace ContentReactor.Audio.Services
{
Expand All @@ -23,7 +27,7 @@ public interface IAudioService

public class AudioService : IAudioService
{
protected IBlobRepository BlobRepository;
protected BlobHelper BlobHelper;
protected IAudioTranscriptionService AudioTranscriptionService;
protected IEventGridPublisherService EventGridPublisherService;

Expand All @@ -32,53 +36,52 @@ public class AudioService : IAudioService
protected internal const string CategoryIdMetadataName = "categoryId";
protected internal const string UserIdMetadataName = "userId";
protected internal const int TranscriptPreviewLength = 100;
public AudioService(IBlobRepository blobRepository, IAudioTranscriptionService audioTranscriptionService, IEventGridPublisherService eventGridPublisherService)

public AudioService(BlobHelper blobHelper, IAudioTranscriptionService audioTranscriptionService, IEventGridPublisherService eventGridPublisherService)
{
BlobRepository = blobRepository;
BlobHelper = blobHelper;
AudioTranscriptionService = audioTranscriptionService;
EventGridPublisherService = eventGridPublisherService;
}

public (string id, string url) BeginAddAudioNote(string userId)
{
// generate an ID for this image note
var audioId = Guid.NewGuid().ToString();
string audioId = Guid.NewGuid().ToString();

// create a blob placeholder (which will not have any contents yet)
var blob = BlobRepository.CreatePlaceholderBlob(AudioBlobContainerName, userId, audioId);
BlockBlobClient blob = BlobHelper.GetBlobClient(AudioBlobContainerName, audioId);

// get a SAS token to allow the client to write the blob
var writePolicy = new SharedAccessBlobPolicy
{
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5), // to allow for clock skew
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
Permissions = SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Write
};
var url = BlobRepository.GetSasTokenForBlob(blob, writePolicy);

return (audioId, url);
string urlAndSas = BlobHelper.GetSasUriForBlob(blob, BlobSasPermissions.Create | BlobSasPermissions.Write);

return (audioId, urlAndSas);
}

public async Task<CompleteAddAudioNoteResult> CompleteAddAudioNoteAsync(string audioId, string userId, string categoryId)
{
var imageBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, userId, audioId, true);
if (imageBlob == null || !await BlobRepository.BlobExistsAsync(imageBlob))
BlockBlobClient imageBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, audioId);
if (imageBlob == null || !await imageBlob.ExistsAsync())
{
// the blob hasn't actually been uploaded yet, so we can't process it
return CompleteAddAudioNoteResult.AudioNotUploaded;
}

// if the blob already contains metadata then that means it has already been added
if (imageBlob.Metadata.ContainsKey(CategoryIdMetadataName))
Response<BlobProperties> response = await imageBlob.GetPropertiesAsync();
if (response.Value.Metadata.ContainsKey(CategoryIdMetadataName))
{
return CompleteAddAudioNoteResult.AudioAlreadyCreated;
}

// set the blob metadata
imageBlob.Metadata.Add(CategoryIdMetadataName, categoryId);
imageBlob.Metadata.Add(UserIdMetadataName, userId);
await BlobRepository.UpdateBlobMetadataAsync(imageBlob);
var metadata = new Dictionary<string, string>
{
{ CategoryIdMetadataName, categoryId },
{ UserIdMetadataName, userId }
};

await imageBlob.SetMetadataAsync(metadata);

// publish an event into the Event Grid topic
var subject = $"{userId}/{audioId}";
Expand All @@ -90,39 +93,33 @@ public async Task<CompleteAddAudioNoteResult> CompleteAddAudioNoteAsync(string a
public async Task<AudioNoteDetails> GetAudioNoteAsync(string id, string userId)
{
// get the blob, if it exists
var audioBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, userId, id, true);
BlockBlobClient audioBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
if (audioBlob == null)
{
return null;
}

// get a SAS token for the blob
var readPolicy = new SharedAccessBlobPolicy
{
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5), // to allow for clock skew
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
Permissions = SharedAccessBlobPermissions.Read
};
var audioUrl = BlobRepository.GetSasTokenForBlob(audioBlob, readPolicy);
string audioUrlAndSas = BlobHelper.GetSasUriForBlob(audioBlob, BlobSasPermissions.Read);

// get the transcript out of the blob metadata
audioBlob.Metadata.TryGetValue(TranscriptMetadataName, out var transcript);

return new AudioNoteDetails
Response<BlobProperties> response = await audioBlob.GetPropertiesAsync();
response.Value.Metadata.TryGetValue(TranscriptMetadataName, out var transcript);

return new AudioNoteDetails
{
Id = id,
AudioUrl = audioUrl,
Id = id,
AudioUrl = audioUrlAndSas,
Transcript = transcript
};
}

public async Task<AudioNoteSummaries> ListAudioNotesAsync(string userId)
{
var blobs = await BlobRepository.ListBlobsInFolderAsync(AudioBlobContainerName, userId);
IList<BlobItem> blobs = await BlobHelper.ListBlobsAsync(AudioBlobContainerName);
var blobSummaries = blobs
.Select(b => new AudioNoteSummary
{
Id = b.Name.Split('/')[1],
Id = b.Name.Split('/')[1],
Preview = b.Metadata.ContainsKey(TranscriptMetadataName) ? b.Metadata[TranscriptMetadataName].Truncate(TranscriptPreviewLength) : string.Empty
})
.ToList();
Expand All @@ -136,7 +133,8 @@ public async Task<AudioNoteSummaries> ListAudioNotesAsync(string userId)
public async Task DeleteAudioNoteAsync(string id, string userId)
{
// delete the blog
await BlobRepository.DeleteBlobAsync(AudioBlobContainerName, userId, id);
BlockBlobClient blob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);

// fire an event into the Event Grid topic
var subject = $"{userId}/{id}";
Expand All @@ -146,7 +144,7 @@ public async Task DeleteAudioNoteAsync(string id, string userId)
public async Task<string> UpdateAudioTranscriptAsync(string id, string userId)
{
// get the blob
var audioBlob = await BlobRepository.GetBlobAsync(AudioBlobContainerName, userId, id, true);
BlockBlobClient audioBlob = BlobHelper.GetBlobClient(AudioBlobContainerName, id);
if (audioBlob == null)
{
return null;
Expand All @@ -156,18 +154,21 @@ public async Task<string> UpdateAudioTranscriptAsync(string id, string userId)
string transcript;
using (var audioBlobStream = new MemoryStream())
{
await BlobRepository.DownloadBlobAsync(audioBlob, audioBlobStream);
await audioBlob.DownloadToAsync(audioBlobStream);

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

// update the blob's metadata
audioBlob.Metadata[TranscriptMetadataName] = transcript;
await BlobRepository.UpdateBlobMetadataAsync(audioBlob);
var metadata = new Dictionary<string, string>
{
{ TranscriptMetadataName, transcript }
};

await audioBlob.SetMetadataAsync(metadata);
// create a preview form of the transcript
var transcriptPreview = transcript.Truncate(TranscriptPreviewLength);
string transcriptPreview = transcript.Truncate(TranscriptPreviewLength);

// fire an event into the Event Grid topic
var subject = $"{userId}/{id}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
using Microsoft.Azure.WebJobs.Host;
using ContentReactor.Audio.Services;
using ContentReactor.Shared;
using ContentReactor.Shared.BlobRepository;
using ContentReactor.Shared.BlobHelper;

namespace ContentReactor.Audio.WorkerApi
{
public static class WorkerApiFunctions
{
private static readonly IEventGridSubscriberService EventGridSubscriberService = new EventGridSubscriberService();
private static readonly IAudioService AudioService = new AudioService(new BlobRepository(), new AudioTranscriptionService(), new EventGridPublisherService());
private static readonly IAudioService AudioService = new AudioService(new BlobHelper(), new AudioTranscriptionService(), new EventGridPublisherService());

[FunctionName("UpdateAudioTranscript")]
public static async Task<IActionResult> UpdateAudioTranscript(
Expand Down
10 changes: 2 additions & 8 deletions audio/src/ContentReactor.Audio/ContentReactor.Audio.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2024
# Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Shared", "..\..\..\shared\src\ContentReactor.Shared\ContentReactor.Shared.csproj", "{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Tests.FakeBlobRepository", "..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj", "{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Audio.Api", "ContentReactor.Audio.Api\ContentReactor.Audio.Api.csproj", "{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentReactor.Audio.Services", "ContentReactor.Audio.Services\ContentReactor.Audio.Services.csproj", "{60520F7F-2ED7-47CE-BB70-0877CB535DAA}"
Expand All @@ -27,10 +25,6 @@ Global
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2B84E89-AA59-4497-81E7-E0CCB661DFB6}.Release|Any CPU.Build.0 = Release|Any CPU
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C08827E-88BA-4E08-A214-E1ADCBAD7CF6}.Release|Any CPU.Build.0 = Release|Any CPU
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E230A66-5FC0-4D63-9943-DF8D7D2DE5E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using ContentReactor.Shared.BlobRepository;
using ContentReactor.Shared.BlobHelper;
using System.IO;
using System.Net.Http;
using ContentReactor.Images.Services.Converters;
Expand All @@ -22,7 +22,7 @@ namespace ContentReactor.Images.Api
public static class ApiFunctions
{
private const string JsonContentType = "application/json";
public static IImagesService ImagesService = new ImagesService(new BlobRepository(), new ImageValidatorService(), new ImagePreviewService(), new ImageCaptionService(new HttpClient()), new EventGridPublisherService());
public static IImagesService ImagesService = new ImagesService(new BlobHelper(), new ImageValidatorService(), new ImagePreviewService(), new ImageCaptionService(new HttpClient()), new EventGridPublisherService());
public static IUserAuthenticationService UserAuthenticationService = new QueryStringUserAuthenticationService();

[FunctionName("BeginCreateImage")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

<ItemGroup>
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Shared\ContentReactor.Shared.csproj" />
<ProjectReference Include="..\..\..\..\shared\src\ContentReactor.Tests.FakeBlobRepository\ContentReactor.Tests.FakeBlobRepository.csproj" />
<ProjectReference Include="..\ContentReactor.Images.Services\ContentReactor.Images.Services.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 079593f

Please sign in to comment.