Compare commits
2 Commits
feature/up
...
65b4ea9476
| Author | SHA1 | Date | |
|---|---|---|---|
| 65b4ea9476 | |||
| 163a251579 |
@@ -1,9 +0,0 @@
|
|||||||
namespace GameIdeas.Shared.Enum;
|
|
||||||
|
|
||||||
public enum BucketType
|
|
||||||
{
|
|
||||||
GameIcon,
|
|
||||||
GameCover,
|
|
||||||
GameMedia,
|
|
||||||
UserIcon
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
namespace GameIdeas.Shared.Exceptions;
|
|
||||||
|
|
||||||
public class BucketTypeMissingException(string message) : Exception(message);
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
namespace GameIdeas.Shared.Exceptions;
|
|
||||||
|
|
||||||
public class EnvironmentVariableMissingException(string valueName) :
|
|
||||||
Exception($"Missing environment variable with key: {valueName}");
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
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")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
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,7 +20,6 @@
|
|||||||
<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>
|
||||||
|
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
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}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
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