Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Streetcode/Streetcode.BLL/Interfaces/BlobStorage/IBlobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

public interface IBlobService
{
public string SaveFileInStorage(string base64, string name, string mimeType);
public MemoryStream FindFileInStorageAsMemoryStream(string name);
public string UpdateFileInStorage(
public Task<string> SaveFileInStorage(string base64, string name, string mimeType);
public Task<string> UpdateFileInStorage(
string previousBlobName,
string base64Format,
string newBlobName,
string extension);
public string FindFileInStorageAsBase64(string name);
public void DeleteFileInStorage(string name);
public Task<MemoryStream?> FindFileInStorageAsMemoryStream(string name);
public Task<string?> FindFileInStorageAsBase64(string name);
public Task DeleteFileInStorage(string name);
public Task CleanBlobStorage();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ public async Task<Result<IEnumerable<ArtDTO>>> Handle(GetArtsByStreetcodeIdQuery

foreach (var artDto in artsDto)
{
if (artDto.Image != null && artDto.Image.BlobName != null)
if (artDto.Image?.BlobName == null)
{
artDto.Image.Base64 = _blobService.FindFileInStorageAsBase64(artDto.Image.BlobName);
continue;
}

var imageBase64 = await _blobService.FindFileInStorageAsBase64(artDto.Image.BlobName);
if (imageBase64 is not null)
{
artDto.Image.Base64 = imageBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
artDto.Image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(artsDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CreateAudioHandler(

public async Task<Result<AudioDTO>> Handle(CreateAudioCommand request, CancellationToken cancellationToken)
{
var hashBlobStorageName = _blobService.SaveFileInStorage(
var hashBlobStorageName = await _blobService.SaveFileInStorage(
request.Audio.BaseFormat,
request.Audio.Title,
request.Audio.Extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ public async Task<Result<IEnumerable<AudioDTO>>> Handle(GetAllAudiosQuery reques
var audioDtos = _mapper.Map<IEnumerable<AudioDTO>>(audios);
foreach (var audio in audioDtos)
{
audio.Base64 = _blobService.FindFileInStorageAsBase64(audio.BlobName);
var audioBase64 = await _blobService.FindFileInStorageAsBase64(audio.BlobName);
if (audioBase64 is not null)
{
audio.Base64 = audioBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Audio),
audio.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(audioDtos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ public async Task<Result<MemoryStream>> Handle(GetBaseAudioQuery request, Cancel

if (audio is not null)
{
return _blobStorage.FindFileInStorageAsMemoryStream(audio.BlobName);
var audioMemoryStream = await _blobStorage.FindFileInStorageAsMemoryStream(audio.BlobName);
if (audioMemoryStream is not null)
{
return Result.Ok(audioMemoryStream);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Audio),
audio.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

var errorMsg = Messages.Error_EntityWithIdNotFound.Format(nameof(AudioEntity), request.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ public async Task<Result<AudioDTO>> Handle(GetAudioByIdQuery request, Cancellati

var audioDto = _mapper.Map<AudioDTO>(audio);

audioDto.Base64 = _blobService.FindFileInStorageAsBase64(audioDto.BlobName);
var audioBase64 = await _blobService.FindFileInStorageAsBase64(audioDto.BlobName);

return Result.Ok(audioDto);
if (audioBase64 is not null)
{
audioDto.Base64 = audioBase64;
return Result.Ok(audioDto);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Audio),
audioDto.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ public async Task<Result<AudioDTO>> Handle(GetAudioByStreetcodeIdQuery request,

var audioDto = _mapper.Map<AudioDTO>(audio);

audioDto.Base64 = _blobService.FindFileInStorageAsBase64(audioDto.BlobName);
var audioBase64 = await _blobService.FindFileInStorageAsBase64(audioDto.BlobName);
if (audioBase64 is not null)
{
audioDto.Base64 = audioBase64;
return Result.Ok(audioDto);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Audio),
audioDto.BlobName);

return Result.Ok(audioDto);
_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CreateImageHandler(

public async Task<Result<ImageDTO>> Handle(CreateImageCommand request, CancellationToken cancellationToken)
{
string hashBlobStorageName = _blobService.SaveFileInStorage(
string hashBlobStorageName = await _blobService.SaveFileInStorage(
request.Image.BaseFormat,
request.Image.Title,
request.Image.Extension);
Expand All @@ -42,18 +42,27 @@ public async Task<Result<ImageDTO>> Handle(CreateImageCommand request, Cancellat

await _repositoryWrapper.ImageRepository.CreateAsync(image);
var resultIsSuccess = await _repositoryWrapper.SaveChangesAsync() > 0;
if (!resultIsSuccess)
{
var errorMsg = Messages.Error_FailedToCreateEntity.Format(nameof(DAL.Entities.Media.Images.Image));
_logger.LogError(request, errorMsg);
return Result.Fail(new Error(errorMsg));
}

var createdImage = _mapper.Map<ImageDTO>(image);

createdImage.Base64 = _blobService.FindFileInStorageAsBase64(createdImage.BlobName);

if (resultIsSuccess)
var imageBase64 = await _blobService.FindFileInStorageAsBase64(createdImage.BlobName);
if (imageBase64 is not null)
{
createdImage.Base64 = imageBase64;
return Result.Ok(createdImage);
}

var errorMsg = Messages.Error_FailedToCreateEntity.Format(nameof(DAL.Entities.Media.Images.Image));
_logger.LogError(request, errorMsg);
return Result.Fail(new Error(errorMsg));
var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
createdImage.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ public async Task<Result<IEnumerable<ImageDTO>>> Handle(GetAllImagesQuery reques

foreach (var image in imageDtos)
{
image.Base64 = _blobService.FindFileInStorageAsBase64(image.BlobName);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(image.BlobName);
if (imageBase64 is not null)
{
image.Base64 = imageBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(imageDtos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ public async Task<Result<MemoryStream>> Handle(GetBaseImageQuery request, Cancel

if (image is not null)
{
return _blobStorage.FindFileInStorageAsMemoryStream(image.BlobName);
var imageMemoryStream = await _blobStorage.FindFileInStorageAsMemoryStream(image.BlobName);
if (imageMemoryStream is not null)
{
return Result.Ok(imageMemoryStream);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

var errorMsg = Messages.Error_EntityWithIdNotFound.Format(nameof(DAL.Entities.Media.Images.Image), request.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ public async Task<Result<ImageDTO>> Handle(GetImageByIdQuery request, Cancellati
}

var imageDto = _mapper.Map<ImageDTO>(image);
if (imageDto.BlobName != null)
if (imageDto.BlobName == null)
{
imageDto.Base64 = _blobService.FindFileInStorageAsBase64(image.BlobName);
return Result.Ok(imageDto);
}

return Result.Ok(imageDto);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(imageDto.BlobName);
if (imageBase64 is not null)
{
imageDto.Base64 = imageBase64;
return Result.Ok(imageDto);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
imageDto.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ public async Task<Result<IEnumerable<ImageDTO>>> Handle(GetImageByStreetcodeIdQu

foreach (var image in imageDtos)
{
image.Base64 = _blobService.FindFileInStorageAsBase64(image.BlobName);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(image.BlobName);
if (imageBase64 is not null)
{
image.Base64 = imageBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(imageDtos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ public async Task<Result<IEnumerable<StreetcodeArtDTO>>> Handle(GetStreetcodeArt

foreach (var artDto in artsDto)
{
artDto.Art.Image.Base64 = _blobService.FindFileInStorageAsBase64(artDto.Art.Image.BlobName);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(artDto.Art.Image.BlobName);
if (imageBase64 is not null)
{
artDto.Art.Image.Base64 = imageBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
artDto.Art.Image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(artsDto);
Expand Down
18 changes: 16 additions & 2 deletions Streetcode/Streetcode.BLL/MediatR/News/GetAll/GetAllNewsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ public async Task<Result<IEnumerable<NewsDTO>>> Handle(GetAllNewsQuery request,

foreach (var dto in newsDTOs)
{
if (dto.Image is not null)
if (dto.Image is null)
{
dto.Image.Base64 = _blobService.FindFileInStorageAsBase64(dto.Image.BlobName);
continue;
}

var imageBase64 = await _blobService.FindFileInStorageAsBase64(dto.Image.BlobName);
if (imageBase64 is not null)
{
dto.Image.Base64 = imageBase64;
continue;
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
dto.Image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}

return Result.Ok(newsDTOs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ await _repositoryWrapper.NewsRepository.GetFirstOrDefaultAsync(
return Result.Fail(errorMsg);
}

if (newsDTO.Image is not null)
if (newsDTO.Image is null)
{
newsDTO.Image.Base64 = _blobService.FindFileInStorageAsBase64(newsDTO.Image.BlobName);
return Result.Ok(newsDTO);
}

return Result.Ok(newsDTO);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(newsDTO.Image.BlobName);
if (imageBase64 is not null)
{
newsDTO.Image.Base64 = imageBase64;
return Result.Ok(newsDTO);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
newsDTO.Image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ await _repositoryWrapper.NewsRepository.GetFirstOrDefaultAsync(
return Result.Fail(errorMsg);
}

if (newsDTO.Image is not null)
if (newsDTO.Image is null)
{
newsDTO.Image.Base64 = _blobService.FindFileInStorageAsBase64(newsDTO.Image.BlobName);
return Result.Ok(newsDTO);
}

return Result.Ok(newsDTO);
var imageBase64 = await _blobService.FindFileInStorageAsBase64(newsDTO.Image.BlobName);
if (imageBase64 is not null)
{
newsDTO.Image.Base64 = imageBase64;
return Result.Ok(newsDTO);
}

var errorNotFoundMsg = Messages.Error_MediaBlobNotFound.Format(
nameof(DAL.Entities.Media.Images.Image),
newsDTO.Image.BlobName);

_logger.LogError(request, errorNotFoundMsg);
return Result.Fail(new Error(errorNotFoundMsg));
}
}
}
Loading
Loading