Compare commits
1 Commits
main
...
feature/up
| Author | SHA1 | Date | |
|---|---|---|---|
| 04faddbb9a |
9
src/GameIdeas/GameIdeas.Shared/Enum/BucketType.cs
Normal file
9
src/GameIdeas/GameIdeas.Shared/Enum/BucketType.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace GameIdeas.Shared.Enum;
|
||||||
|
|
||||||
|
public enum BucketType
|
||||||
|
{
|
||||||
|
GameIcon,
|
||||||
|
GameCover,
|
||||||
|
GameMedia,
|
||||||
|
UserIcon
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace GameIdeas.Shared.Exceptions;
|
||||||
|
|
||||||
|
public class BucketTypeMissingException(string message) : Exception(message);
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
namespace GameIdeas.Shared.Exceptions;
|
||||||
|
|
||||||
|
public class EnvironmentVariableMissingException(string valueName) :
|
||||||
|
Exception($"Missing environment variable with key: {valueName}");
|
||||||
19
src/GameIdeas/GameIdeas.Shared/Extensions/BucketExtension.cs
Normal file
19
src/GameIdeas/GameIdeas.Shared/Extensions/BucketExtension.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using GameIdeas.Shared.Enum;
|
||||||
|
using GameIdeas.Shared.Exceptions;
|
||||||
|
|
||||||
|
namespace GameIdeas.Shared.Extensions;
|
||||||
|
|
||||||
|
public static class BucketExtension
|
||||||
|
{
|
||||||
|
public static string GetBucket(this BucketType bucketType)
|
||||||
|
{
|
||||||
|
return bucketType switch
|
||||||
|
{
|
||||||
|
BucketType.GameIcon => "icons",
|
||||||
|
BucketType.GameCover => "covers",
|
||||||
|
BucketType.GameMedia => "medias",
|
||||||
|
BucketType.UserIcon => "users",
|
||||||
|
_ => throw new BucketTypeMissingException($"Bucket type {bucketType} not exist")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using GameIdeas.Shared.Enum;
|
||||||
|
using GameIdeas.WebAPI.Services.Files;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace GameIdeas.WebAPI.Controllers;
|
||||||
|
|
||||||
|
public class FileController(
|
||||||
|
IFileService fileService,
|
||||||
|
ILoggerFactory loggerFactory) : Controller
|
||||||
|
{
|
||||||
|
private readonly ILogger<FileController> Logger = loggerFactory.CreateLogger<FileController>();
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[RequestSizeLimit(100_000_000_000)] // 100 GB
|
||||||
|
public async Task<IActionResult> UploadFile([FromForm] IFormFile file, [FromQuery] BucketType type)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Ok(await fileService.UploadFile(file, type));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.LogError(e, "Failed uploading files");
|
||||||
|
return StatusCode(500, e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Minio" Version="6.0.4" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using Minio;
|
||||||
|
using GameIdeas.Shared.Exceptions;
|
||||||
|
using Minio.DataModel.Args;
|
||||||
|
using GameIdeas.Shared.Enum;
|
||||||
|
using GameIdeas.Shared.Extensions;
|
||||||
|
|
||||||
|
namespace GameIdeas.WebAPI.Services.Files;
|
||||||
|
|
||||||
|
public class FileService : IFileService
|
||||||
|
{
|
||||||
|
private readonly IMinioClient MinioClient;
|
||||||
|
|
||||||
|
public FileService()
|
||||||
|
{
|
||||||
|
var minioEndpoint = Environment.GetEnvironmentVariable("MINIO_ENDPOINT")
|
||||||
|
?? throw new EnvironmentVariableMissingException("MINIO_ENDPOINT");
|
||||||
|
|
||||||
|
var minioAccessKey = Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY")
|
||||||
|
?? throw new EnvironmentVariableMissingException("MINIO_ACCESS_KEY");
|
||||||
|
|
||||||
|
var minioSecretKey= Environment.GetEnvironmentVariable("MINIO_SECRET_KEY")
|
||||||
|
?? throw new EnvironmentVariableMissingException("MINIO_SECRET_KEY");
|
||||||
|
|
||||||
|
MinioClient = new MinioClient()
|
||||||
|
.WithEndpoint(minioEndpoint)
|
||||||
|
.WithCredentials(minioAccessKey, minioSecretKey)
|
||||||
|
.Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> UploadFile(IFormFile file, BucketType bucketType)
|
||||||
|
{
|
||||||
|
var fileName = Path.GetFileName(file.FileName);
|
||||||
|
var bucket = bucketType.GetBucket();
|
||||||
|
|
||||||
|
bool found = await MinioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
await MinioClient.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket));
|
||||||
|
}
|
||||||
|
|
||||||
|
using var stream = file.OpenReadStream();
|
||||||
|
|
||||||
|
var args = new PutObjectArgs()
|
||||||
|
.WithBucket(bucket)
|
||||||
|
.WithObject(fileName)
|
||||||
|
.WithStreamData(stream)
|
||||||
|
.WithObjectSize(file.Length)
|
||||||
|
.WithContentType(file.ContentType);
|
||||||
|
|
||||||
|
await MinioClient.PutObjectAsync(args);
|
||||||
|
|
||||||
|
return $"/{bucket}/{fileName}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using GameIdeas.Shared.Enum;
|
||||||
|
|
||||||
|
namespace GameIdeas.WebAPI.Services.Files;
|
||||||
|
|
||||||
|
public interface IFileService
|
||||||
|
{
|
||||||
|
Task<string> UploadFile(IFormFile file, BucketType bucketType);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user