Skip to content

Commit 6aab5b9

Browse files
Merge pull request #18 from CapstoneProjectCMC/feature/Organization-Service
fix some error about minio
2 parents 18b8bd0 + ead67af commit 6aab5b9

File tree

6 files changed

+66
-25
lines changed

6 files changed

+66
-25
lines changed

FileService/FileService.Api/Controllers/FileDocumentController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
using FileService.Service.ApiModels.FileDocumentModels;
33
using FileService.Service.Dtos.FileDocumentDtos;
44
using FileService.Service.Interfaces;
5+
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67

78
namespace FileService.Api.Controllers
89
{
10+
// [Authorize]
11+
// [Authorize(Policy = "Permission")]
12+
// [Authorize(Roles = "ADMIN")]
913
[Route("file/api/[controller]")]
1014
[ApiController]
1115
public class FileDocumentController : BaseApiController

FileService/FileService.Api/Middlewares/AuthenMiddleware.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using FileService.Core.ApiModels;
2+
using System.Security.Claims;
23

34
namespace FileService.Api.Middlewares
45
{
56
public class AuthenMiddleware
67
{
78
private readonly RequestDelegate _next;
9+
10+
811
public AuthenMiddleware(RequestDelegate next)
912
{
1013
_next = next;
1114
}
1215
public async Task InvokeAsync(HttpContext httpContext)
1316
{
17+
//lấy userId và sessionId từ claims
1418
var userId = httpContext.User.Claims.FirstOrDefault(c => c.Type == "userId")?.Value;
1519
var sessionId = httpContext.User.Claims.FirstOrDefault(c => c.Type == "sessionId")?.Value;
1620

FileService/FileService.Api/Program.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using FileService.Service.Implementation;
1919
using FileService.Service.Interfaces;
2020
using Microsoft.Extensions.FileProviders;
21+
using System.Security.Claims;
2122

2223
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
2324

@@ -39,7 +40,7 @@
3940

4041
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
4142
Console.WriteLine($"ASPNETCORE_ENVIRONMENT: {env}");
42-
builder.Services.Configure<MinioConfig>(builder.Configuration.GetSection("MinioConfig"));
43+
//builder.Services.Configure<MinioConfig>(builder.Configuration.GetSection("MinioConfig"));
4344

4445
builder.Services.Configure<MongoDbSettings>(builder.Configuration.GetSection("MongoDbSettings"));
4546
builder.Services.Configure<FfmpegSettings>(builder.Configuration.GetSection("FfmpegSettings"));
@@ -98,7 +99,11 @@
9899
ValidateIssuerSigningKey = true,
99100
ValidIssuer = appSettings.Jwt.Issuer,
100101
ValidAudience = appSettings.Jwt.Audience,
101-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Jwt.Key))
102+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Jwt.Key)),
103+
104+
// Cấu hình claim để nhận Role
105+
RoleClaimType = ClaimTypes.Role
106+
102107
};
103108
});
104109

@@ -173,12 +178,6 @@
173178
app.UseSwaggerUI();
174179
}
175180

176-
using (var scope = app.Services.CreateScope())
177-
{
178-
var minioService = scope.ServiceProvider.GetRequiredService<IMinioService>();
179-
await minioService.EnsureBucketExistsAsync();
180-
}
181-
182181
//kích hoạt CORS policy
183182
app.UseCors(MyAllowSpecificOrigins);
184183

@@ -192,6 +191,5 @@
192191

193192
app.MapControllers();
194193

195-
196-
await app.RunAsync();
194+
app.Run();
197195

FileService/FileService.Api/appsettings.Development.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"OtpHoursAvailable": 24
2222
},
2323
"Jwt": {
24-
"Key": "uifhjcnsqbhwbAMKXjdt123gdritocet",
25-
"Issuer": "http://localhost:38946",
26-
"Audience": "http://localhost:38946",
24+
"Key": "1TjXchw5FloESb63Kc+DFhTARvpWL4jUGCwfGWxuG5SIf/1y/LgJxHnMqaF6A/ij",
25+
"Issuer": "Code Campus",
26+
"Audience": "Code Campus",
2727
"AccessTokenExpiresTime": 60,
28-
"RefreshTokenExpiresTime": 3
28+
"RefreshTokenExpiresTime": 300
2929
},
3030
"Admin": {
3131
"OtpMaxAttempted": 3,

FileService/FileService.Api/appsettings.Staging.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"OtpHoursAvailable": 24
2222
},
2323
"Jwt": {
24-
"Key": "uifhjcnsqbhwbAMKXjdt123gdritocet",
25-
"Issuer": "http://localhost:38946",
26-
"Audience": "http://localhost:38946",
24+
"Key": "1TjXchw5FloESb63Kc+DFhTARvpWL4jUGCwfGWxuGSSIf/1y/LgJxHnMqaF6A/ij",
25+
"Issuer": "Code Campus",
26+
"Audience": "Code Campus",
2727
"AccessTokenExpiresTime": 60,
2828
"RefreshTokenExpiresTime": 3
2929
},

FileService/FileService.Service/Implementation/MinioService.cs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,60 @@ public class MinioService : BaseService, IMinioService
2222
private readonly IMinioClient _minioClient;
2323
private readonly MinioConfig _config;
2424

25-
public MinioService(AppSettings appSettings, UserContext userContext) : base(appSettings, userContext)
25+
public MinioService(IOptions<MinioConfig> options, AppSettings appSettings, UserContext userContext) : base(appSettings, userContext)
2626
{
27-
_config = appSettings.MinioConfig ?? throw new ArgumentNullException(nameof(appSettings.MinioConfig));
27+
_config = options.Value ?? throw new ArgumentNullException(nameof(options.Value));
2828

29-
_minioClient = new MinioClient()
30-
.WithEndpoint(_config.Endpoint, _config.Port)
31-
.WithCredentials(_config.AccessKey, _config.SecretKey)
32-
.WithSSL(_config.Secure)
33-
.Build();
29+
Console.WriteLine($"MinioConfig: Endpoint={_config.Endpoint}, Port={_config.Port}, AccessKey length={_config.AccessKey?.Length ?? 0}, Secure={_config.Secure}");
30+
31+
if (string.IsNullOrWhiteSpace(_config.Endpoint) || _config.Port <= 0 || string.IsNullOrWhiteSpace(_config.AccessKey) || string.IsNullOrWhiteSpace(_config.SecretKey))
32+
{
33+
throw new InvalidOperationException("Minio configuration is invalid or incomplete.");
34+
}
35+
try
36+
{
37+
Console.WriteLine("Attempting to build MinioClient...");
38+
_minioClient = new MinioClient()
39+
.WithEndpoint(_config.Endpoint, _config.Port)
40+
.WithCredentials(_config.AccessKey, _config.SecretKey)
41+
.WithSSL(_config.Secure)
42+
.Build();
43+
if (_minioClient == null)
44+
{
45+
throw new InvalidOperationException("Failed to initialize MinioClient.");
46+
}
47+
// Kiểm tra kết nối thực tế
48+
Console.WriteLine("Testing MinioClient connection...");
49+
var buckets = _minioClient.ListBucketsAsync().GetAwaiter().GetResult(); // Kiểm tra nhanh
50+
Console.WriteLine("MinioClient initialized and connection tested successfully.");
51+
}
52+
catch (MinioException ex)
53+
{
54+
Console.WriteLine($"MinioException: {ex.Message}");
55+
throw new InvalidOperationException($"Minio initialization failed: {ex.Message}", ex);
56+
}
57+
catch (Exception ex)
58+
{
59+
Console.WriteLine($"Unexpected error: {ex.Message}");
60+
throw new InvalidOperationException($"Failed to initialize MinioClient: {ex.Message}", ex);
61+
}
3462
}
3563

3664
public async Task EnsureBucketExistsAsync()
3765
{
38-
var bucketExistsArgs = new BucketExistsArgs().WithBucket(_config.BucketName);
66+
if (_minioClient == null)
67+
{
68+
throw new InvalidOperationException("MinioClient is not initialized.");
69+
}
70+
3971
if (string.IsNullOrWhiteSpace(_config.BucketName))
4072
{
4173
throw new InvalidOperationException("Bucket name is not configured.");
4274
}
75+
var bucketExistsArgs = new BucketExistsArgs().WithBucket(_config.BucketName);
76+
4377
bool found = await _minioClient.BucketExistsAsync(bucketExistsArgs);
78+
4479
if (!found)
4580
{
4681
var makeBucketArgs = new MakeBucketArgs().WithBucket(_config.BucketName);

0 commit comments

Comments
 (0)