-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.net 9.0 / photo page (part 1) / handle exception review / storage fu…
…nction review / nuget updates
- Loading branch information
Dhiogo Acioli
committed
Nov 24, 2024
1 parent
6d61191
commit 132e1d7
Showing
42 changed files
with
684 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; | ||
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace MM.API.Core | ||
{ | ||
public class ComputerVisionHelper(IConfiguration configuration) | ||
{ | ||
public IConfiguration Configuration { get; } = configuration; | ||
|
||
/// <summary> | ||
/// still using version 3.2 (old version) | ||
/// | ||
/// https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/quickstarts-sdk/image-analysis-client-library?pivots=programming-language-csharp&tabs=windows%2Cvisual-studio | ||
/// | ||
/// update when adult content is available | ||
/// | ||
/// https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/overview-image-analysis?tabs=4-0 | ||
/// </summary> | ||
/// <param name="url"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task<ImageAnalysis> AnalyzeImage(Stream stream, CancellationToken cancellationToken) | ||
{ | ||
var endpoint = Configuration.GetValue<string>("CognitiveEndpoint"); | ||
var key = Configuration.GetValue<string>("CognitiveKey"); | ||
|
||
var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; | ||
|
||
var features = new List<VisualFeatureTypes?>() | ||
{ | ||
VisualFeatureTypes.Adult, VisualFeatureTypes.Faces | ||
}; | ||
|
||
return await client.AnalyzeImageInStreamAsync(stream, visualFeatures: features, cancellationToken: cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,34 @@ | ||
using Azure.Storage.Blobs; | ||
using Azure.Storage.Blobs.Models; | ||
using Microsoft.Extensions.Configuration; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using static VerusDate.Shared.Helper.ImageHelper; | ||
using MM.Shared.Models.Profile; | ||
using static MM.Shared.Core.Helper.ImageHelper; | ||
|
||
namespace MM.API.Core | ||
{ | ||
public class StorageHelper | ||
public class StorageHelper(IConfiguration configuration) | ||
{ | ||
public IConfiguration Configuration { get; } | ||
public IConfiguration Configuration { get; } = configuration; | ||
|
||
public StorageHelper(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public async Task UploadPhoto(PhotoType type, Stream stream, string fileName, CancellationToken cancellationToken) | ||
public async Task UploadPhoto(PhotoType type, Stream stream, string fileName, string userId, CancellationToken cancellationToken) | ||
{ | ||
var container = new BlobContainerClient(Configuration.GetValue<string>("AzureStorage"), GetPhotoContainer(type)); | ||
var client = container.GetBlobClient(fileName); | ||
|
||
var headers = new BlobHttpHeaders { ContentType = "image/jpeg" }; | ||
|
||
await client.UploadAsync(stream, headers, cancellationToken: cancellationToken); | ||
await client.UploadAsync(stream, headers, new Dictionary<string, string>() { { "id", userId } }, cancellationToken: cancellationToken); | ||
} | ||
|
||
public async Task DeletePhoto(PhotoType type, string fileName, CancellationToken cancellationToken) | ||
public async Task DeletePhoto(PhotoType type, string pictureId, CancellationToken cancellationToken) | ||
{ | ||
if (string.IsNullOrEmpty(fileName)) return; | ||
|
||
var container = new BlobContainerClient(Configuration.GetValue<string>("AzureStorage"), GetPhotoContainer(type)); | ||
var blob = container.GetBlobClient(fileName); | ||
var blob = container.GetBlobClient(pictureId); | ||
|
||
if (await blob.ExistsAsync()) | ||
if (await blob.ExistsAsync(cancellationToken)) | ||
{ | ||
await blob.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, cancellationToken: cancellationToken); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.