-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Team-Wilhelm/remove-image-background
Remove image background + add plant + blob
- Loading branch information
Showing
50 changed files
with
314 additions
and
85 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
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,8 @@ | ||
namespace Core.Options; | ||
|
||
public class AzureBlobStorageOptions | ||
{ | ||
public string ConnectionString { get; set; } = null!; | ||
public string PlantImagesContainer { get; set; } = null!; | ||
public string UserProfileImagesContainer { get; set; } = null!; | ||
} |
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
6 changes: 6 additions & 0 deletions
6
Core/Services/External/BackgroundRemoval/IImageBackgroundRemoverService.cs
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,6 @@ | ||
namespace Core.Services.External; | ||
|
||
public interface IImageBackgroundRemoverService | ||
{ | ||
public Task<byte[]> RemoveBackground(byte[] imageBytes); | ||
} |
34 changes: 34 additions & 0 deletions
34
Core/Services/External/BackgroundRemoval/ImageBackgroundRemoverService.cs
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,34 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using Core.Options; | ||
using Microsoft.Extensions.Options; | ||
using Shared.Exceptions; | ||
|
||
namespace Core.Services.External.BackgroundRemoval; | ||
|
||
public class ImageBackgroundRemoverService(IOptions<AzureVisionOptions> options) : IImageBackgroundRemoverService | ||
{ | ||
public async Task<byte[]> RemoveBackground(byte[] imageBytes) | ||
{ | ||
var request = options.Value.BaseUrl + options.Value.RemoveBackgroundEndpoint; | ||
|
||
var client = new HttpClient(); | ||
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", $"{options.Value.Key}"); | ||
|
||
HttpResponseMessage response; | ||
using (var content = new ByteArrayContent(imageBytes)) | ||
{ | ||
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); | ||
response = await client.PostAsync(request, content); | ||
} | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
{ | ||
throw new AppException("Failed to remove background from image."); | ||
} | ||
|
||
// The response is image/png | ||
var removedBgImageBytes = await response.Content.ReadAsByteArrayAsync(); | ||
return removedBgImageBytes; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Core/Services/External/BackgroundRemoval/MockImageBackgroundRemoverService.cs
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,9 @@ | ||
namespace Core.Services.External; | ||
|
||
public class MockImageBackgroundRemoverService : IImageBackgroundRemoverService | ||
{ | ||
public Task<byte[]> RemoveBackground(byte[] imageBytes) | ||
{ | ||
return Task.FromResult(imageBytes); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Core/Services/External/BlobStorage/BlobStorageServiceService.cs
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 Azure.Storage.Blobs; | ||
using Core.Options; | ||
using Microsoft.Extensions.Options; | ||
using Shared.Exceptions; | ||
|
||
namespace Core.Services.External.BlobStorage; | ||
|
||
public class BlobStorageServiceService(IOptions<AzureBlobStorageOptions> azureBlobStorageOptions) : IBlobStorageService | ||
{ | ||
public async Task<string> SaveImageToBlobStorage(string base64Image, string userEmail, string? blobUrl = null) | ||
{ | ||
var imageBytes = Convert.FromBase64String(base64Image); | ||
blobUrl ??= userEmail + "_" + Guid.NewGuid(); | ||
var blobClient = new BlobClient(azureBlobStorageOptions.Value.ConnectionString, azureBlobStorageOptions.Value.PlantImagesContainer, blobUrl); | ||
var binaryData = new BinaryData(imageBytes); | ||
await blobClient.UploadAsync(binaryData, true); | ||
return blobClient.Uri.ToString(); | ||
} | ||
|
||
public async Task<bool> DeleteImageFromBlobStorage(string imageUrl) | ||
{ | ||
var blobClient = new BlobClient(new Uri(imageUrl)); | ||
return await blobClient.DeleteIfExistsAsync(); | ||
} | ||
|
||
public async Task<string> GetImageFromBlobStorage(string imageUrl) | ||
{ | ||
if (string.IsNullOrEmpty(imageUrl)) return ""; | ||
|
||
var blobClient = new BlobClient(new Uri(imageUrl)); | ||
if (await blobClient.ExistsAsync() == false) throw new NotFoundException("Image not found"); | ||
|
||
using var memoryStream = new MemoryStream(); | ||
await blobClient.DownloadToAsync(memoryStream); | ||
var imageBytes = memoryStream.ToArray(); | ||
return Convert.ToBase64String(imageBytes); | ||
} | ||
} |
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,8 @@ | ||
namespace Core.Services.External.BlobStorage; | ||
|
||
public interface IBlobStorageService | ||
{ | ||
public Task<string> SaveImageToBlobStorage(string base64Image, string userEmail, string? blobUrl = null); | ||
public Task<bool> DeleteImageFromBlobStorage(string imageUrl); | ||
public Task<string> GetImageFromBlobStorage(string imageUrl); | ||
} |
19 changes: 19 additions & 0 deletions
19
Core/Services/External/BlobStorage/MockBlobStorageService.cs
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,19 @@ | ||
namespace Core.Services.External.BlobStorage; | ||
|
||
public class MockBlobStorageService : IBlobStorageService | ||
{ | ||
public Task<string> SaveImageToBlobStorage(string base64Image, string userEmail, string? blobUrl = null) | ||
{ | ||
return Task.FromResult("https://www.example.com"); | ||
} | ||
|
||
public Task<bool> DeleteImageFromBlobStorage(string imageUrl) | ||
{ | ||
return Task.FromResult(true); | ||
} | ||
|
||
public Task<string> GetImageFromBlobStorage(string imageUrl) | ||
{ | ||
return Task.FromResult("base64Image"); | ||
} | ||
} |
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
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,15 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Shared.Dtos.FromClient.Requirements; | ||
using Shared.Models.Information; | ||
|
||
namespace Shared.Dtos.Plant; | ||
namespace Shared.Dtos.FromClient.Plant; | ||
|
||
public class UpdatePlantDto | ||
{ | ||
[Required] public Guid PlantId { get; set; } | ||
public Guid? CollectionId { get; set; } | ||
public string? DeviceId { get; set; } | ||
[MaxLength(50)] public string? Nickname { get; set; } | ||
public string? ImageUrl { get; set; } | ||
public string? Base64Image { get; set; } // should be null if the image should not be updated | ||
public UpdateRequirementDto? UpdateRequirementDto { get; set; } | ||
} |
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
6 changes: 3 additions & 3 deletions
6
Shared/Models/Exceptions/AppException.cs → Shared/Exceptions/AppException.cs
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
2 changes: 1 addition & 1 deletion
2
...Exceptions/InvalidCredentialsException.cs → ...Exceptions/InvalidCredentialsException.cs
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.