Skip to content

Commit

Permalink
IHS-82 Minio paths structure (#83)
Browse files Browse the repository at this point in the history
* change image location in minio storage
change new image message contract

* fix ci action
  • Loading branch information
adedw authored Sep 24, 2024
1 parent a1c06a5 commit 971a998
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
cache: true
cache-dependency-path: '**/packages.lock.json'

- name: Restore dependencies
working-directory: ./tests/ImageHosting.Storage.UnitTests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
file: ./src/ImageHosting.Storage.WebApi/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
6 changes: 5 additions & 1 deletion src/ImageHosting.Storage.Application/DTOs/RemoveFileDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace ImageHosting.Storage.Application.DTOs;

public class RemoveFileDTO(string userId, string imageId)
{
public string UserId { get; } = userId;
public string Bucket { get; } = userId;
public string ImageId { get; } = imageId;

public string Prefix => ImageId.ToString();
public string ImageName => "original.jpg";
public string FullImageName => $"{Prefix}/{ImageName}";
}
8 changes: 6 additions & 2 deletions src/ImageHosting.Storage.Application/DTOs/WriteFileDTO.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace ImageHosting.Storage.Application.DTOs;

public class WriteFileDTO(string userId, string imageId, long length, string contentType, Stream stream)
public class WriteFileDTO(string bucket, string imageId, long length, string contentType, Stream stream)
{
public string UserId { get; } = userId;
public string Bucket { get; } = bucket;
public string ImageId { get; } = imageId;
public long Length { get; } = length;
public string ContentType { get; } = contentType;
public Stream Stream { get; } = stream;

public string Prefix => ImageId.ToString();
public string ImageName => "original.jpg";
public string FullImageName => $"{Prefix}/{ImageName}";
}
9 changes: 6 additions & 3 deletions src/ImageHosting.Storage.Domain/Messages/NewImage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Text.Json.Serialization;
using ImageHosting.Storage.Domain.ValueTypes;
using System.Text.Json.Serialization;

namespace ImageHosting.Storage.Domain.Messages;

public class NewImage
{
[JsonPropertyName("bucketId")] public required UserId BucketId { get; init; }
[JsonPropertyName("imageId")] public required ImageId ImageId { get; init; }
[JsonPropertyName("bucket")] public UserId Bucket { get; set; }
[JsonIgnore] public ImageId ImageId { get; set; }

[JsonPropertyName("prefix")] public string Prefix => ImageId.ToString();
[JsonPropertyName("image")] public string ImageName => "original.jpg";
}
25 changes: 13 additions & 12 deletions src/ImageHosting.Storage.Infrastructure/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@ public class FileService(IMinioClient minioClient, ILogger<FileService> logger)
public async Task<UploadFileDTO> WriteFileAsync(WriteFileDTO writeFileDto,
CancellationToken cancellationToken = default)
{
var bucketExistsArgs = new BucketExistsArgs().WithBucket(writeFileDto.UserId);
var bucketExistsArgs = new BucketExistsArgs().WithBucket(writeFileDto.Bucket);
var foundBucket =
await minioClient.BucketExistsAsync(bucketExistsArgs, cancellationToken).ConfigureAwait(false);
if (!foundBucket)
{
throw new UserBucketDoesNotExistsException(writeFileDto.UserId);
throw new UserBucketDoesNotExistsException(writeFileDto.Bucket);
}

var isObjectExists = await IsObjectExistsAsync(writeFileDto.UserId, writeFileDto.ImageId, cancellationToken);
var isObjectExists = await IsObjectExistsAsync(writeFileDto.Bucket, writeFileDto.FullImageName, cancellationToken)
.ConfigureAwait(false);
if (isObjectExists)
{
throw new ImageObjectAlreadyExistsException(writeFileDto.UserId, writeFileDto.ImageId);
throw new ImageObjectAlreadyExistsException(writeFileDto.Bucket, writeFileDto.ImageId);
}

var putObjectArgs = new PutObjectArgs()
.WithBucket(writeFileDto.UserId)
.WithBucket(writeFileDto.Bucket)
.WithObjectSize(writeFileDto.Length)
.WithContentType(writeFileDto.ContentType)
.WithStreamData(writeFileDto.Stream)
.WithObject(writeFileDto.ImageId);
.WithObject(writeFileDto.FullImageName);

var putObjectResponse =
await minioClient.PutObjectAsync(putObjectArgs, cancellationToken).ConfigureAwait(false);

logger.LogFileWritten(writeFileDto.ImageId, writeFileDto.UserId);
logger.LogFileWritten(writeFileDto.ImageId, writeFileDto.Bucket);
return putObjectResponse.ToDTO();
}

Expand All @@ -63,19 +64,19 @@ private async Task<bool> IsObjectExistsAsync(string bucketId, string objectName,

public async Task RemoveFileAsync(RemoveFileDTO removeFileDto, CancellationToken cancellationToken = default)
{
var bucketExistsArgs = new BucketExistsArgs().WithBucket(removeFileDto.UserId);
var bucketExistsArgs = new BucketExistsArgs().WithBucket(removeFileDto.Bucket);
var foundBucket =
await minioClient.BucketExistsAsync(bucketExistsArgs, cancellationToken).ConfigureAwait(false);
if (!foundBucket)
{
throw new UserBucketDoesNotExistsException(removeFileDto.UserId);
throw new UserBucketDoesNotExistsException(removeFileDto.Bucket);
}

var removeObjectArgs = new RemoveObjectArgs()
.WithBucket(removeFileDto.UserId)
.WithObject(removeFileDto.ImageId);
.WithBucket(removeFileDto.Bucket)
.WithObject(removeFileDto.FullImageName);
await minioClient.RemoveObjectAsync(removeObjectArgs, cancellationToken).ConfigureAwait(false);

logger.LogFileRemoved(removeFileDto.ImageId, removeFileDto.UserId);
logger.LogFileRemoved(removeFileDto.ImageId, removeFileDto.Bucket);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class NewImageProducer(ITopicProducer<NewImage> messageProducer, ILogger<
public async Task SendAsync(NewImage newImage, CancellationToken cancellationToken = default)
{
await messageProducer.Produce(newImage, cancellationToken);
logger.LogMessagePublished(newImage.ImageId, newImage.BucketId);
logger.LogMessagePublished(newImage.ImageId, newImage.Bucket);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PublishNewMessageCommand(INewImageProducer newImageProducer, UserId
{
public Task ExecuteAsync(CancellationToken cancellationToken = default)
{
return newImageProducer.SendAsync(new NewImage { BucketId = userId, ImageId = imageId }, cancellationToken);
return newImageProducer.SendAsync(new NewImage { Bucket = userId, ImageId = imageId }, cancellationToken);
}

public Task RollbackAsync(CancellationToken cancellationToken = default)
Expand Down

0 comments on commit 971a998

Please sign in to comment.