Compare commits
No commits in common. "41f8d51f08411c4fd96ea5b3801abf2a05cc313e" and "dd5da6347edf85dad964e88044f3fe97608195f8" have entirely different histories.
41f8d51f08
...
dd5da6347e
@ -110,11 +110,17 @@ namespace qtc_api.Controllers
|
||||
await _chatGWContext.Clients.All.SendAsync("RefreshContactsList");
|
||||
|
||||
// always try to overwrite cache when updating pfp
|
||||
string recordId = $"UserPfp_{userId}";
|
||||
using var stream = file.OpenReadStream();
|
||||
using var ms = new MemoryStream();
|
||||
stream.CopyTo(ms);
|
||||
await _cache.SetImageAsync(recordId, ms.ToArray(), TimeSpan.FromHours(1));
|
||||
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
||||
using(var stream = file.OpenReadStream())
|
||||
{
|
||||
using(var ms = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(ms);
|
||||
await _cache.SetImageAsync(recordId, ms.ToArray());
|
||||
ms.Dispose();
|
||||
}
|
||||
stream.Dispose();
|
||||
}
|
||||
|
||||
return Ok(response);
|
||||
} else
|
||||
@ -131,8 +137,8 @@ namespace qtc_api.Controllers
|
||||
[Authorize]
|
||||
public async Task<ActionResult> GetUserProfilePicture(string userId)
|
||||
{
|
||||
string recordId = $"UserPfp_{userId}";
|
||||
byte[]? pfpBytes = await _cache.GetImageAsync(recordId);
|
||||
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
||||
byte[] pfpBytes = await _cache.GetImageAsync(recordId);
|
||||
|
||||
var result = new ServiceResponse<FileContentResult>();
|
||||
if (pfpBytes == null)
|
||||
@ -141,7 +147,7 @@ namespace qtc_api.Controllers
|
||||
if (result != null && result.Success && result.Data != null)
|
||||
{
|
||||
pfpBytes = result.Data.FileContents;
|
||||
await _cache.SetImageAsync(recordId, pfpBytes, TimeSpan.FromHours(1));
|
||||
await _cache.SetImageAsync(recordId, pfpBytes);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -149,6 +155,7 @@ namespace qtc_api.Controllers
|
||||
// explicitly set from cache
|
||||
result.Success = true;
|
||||
result.Data = new FileContentResult(pfpBytes, "image/jpeg");
|
||||
result.Message = $"{userId}.pfp";
|
||||
}
|
||||
|
||||
if (result != null && result.Success != false)
|
||||
|
||||
@ -5,21 +5,23 @@ namespace qtc_api.Extensions
|
||||
{
|
||||
public static class DistributedCacheExtensions
|
||||
{
|
||||
public static async Task SetRecordAsync<T> (this IDistributedCache cache, string recordId, T data, TimeSpan? absoluteExpireTime = null)
|
||||
public static async Task SetRecordAsync<T> (this IDistributedCache cache, string recordId, T data, TimeSpan? absoluteExpireTime = null, TimeSpan? unusuedExpireTime = null)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions();
|
||||
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromSeconds(15);
|
||||
options.SlidingExpiration = unusuedExpireTime;
|
||||
|
||||
var jsonData = JsonSerializer.Serialize(data);
|
||||
await cache.SetStringAsync(recordId, jsonData, options);
|
||||
}
|
||||
|
||||
public static async Task SetImageAsync(this IDistributedCache cache, string recordId, byte[] data, TimeSpan? absoluteExpireTime = null)
|
||||
public static async Task SetImageAsync(this IDistributedCache cache, string recordId, byte[] data, TimeSpan? absoluteExpireTime = null, TimeSpan? unusuedExpireTime = null)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions();
|
||||
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(30);
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(5);
|
||||
options.SlidingExpiration = unusuedExpireTime;
|
||||
|
||||
await cache.SetAsync(recordId, data, options);
|
||||
}
|
||||
|
||||
@ -91,6 +91,20 @@ namespace qtc_api.Hubs
|
||||
}
|
||||
}
|
||||
|
||||
[HubMethodName("JoinRoomGuest")]
|
||||
public async Task JoinRoomGuestAsync(string roomId, string username)
|
||||
{
|
||||
// here we can just add the client to the room group and call it a day since the user isn't authenticated
|
||||
var room = await _roomService.GetRoom(roomId);
|
||||
|
||||
if(room != null && room.Success && room.Data != null)
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, room.Data.Id);
|
||||
|
||||
await Clients.Group(room.Data.Id).SendAsync("GuestJoin", username);
|
||||
}
|
||||
}
|
||||
|
||||
[HubMethodName("UpdateStatus")]
|
||||
[Authorize]
|
||||
public async Task UpdateStatusAsync(User user, int status)
|
||||
@ -110,6 +124,35 @@ namespace qtc_api.Hubs
|
||||
Log($"Something Went Wrong Setting The Status On User {user.Username}");
|
||||
}
|
||||
|
||||
[HubMethodName("JoinLobby")]
|
||||
[Authorize]
|
||||
public async Task JoinLobbyAsync(User user)
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, "LOBBY");
|
||||
|
||||
await Clients.Group("LOBBY").SendAsync("RoomMessage", $"[SERVER] User {user.Username} Has Joined The Lobby");
|
||||
|
||||
if (!GroupUsers.TryGetValue("LOBBY", out _)) { GroupUsers.Add("LOBBY", new List<User>()); }
|
||||
GroupUsers["LOBBY"].Add(user);
|
||||
|
||||
await Clients.Groups("LOBBY").SendAsync("RoomUserList", GroupUsers["LOBBY"]);
|
||||
Log($"User {user.Username} Has Joined The Lobby");
|
||||
}
|
||||
|
||||
[HubMethodName("LeaveLobby")]
|
||||
[Authorize]
|
||||
public async Task LeaveLobbyAsync(User user)
|
||||
{
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "LOBBY");
|
||||
|
||||
await Clients.Group("LOBBY").SendAsync("RoomMessage", $"[SERVER] User {user.Username} Has Left The Lobby");
|
||||
|
||||
if (GroupUsers.TryGetValue("LOBBY", out _)) GroupUsers["LOBBY"].Remove(GroupUsers["LOBBY"].FirstOrDefault(e => e.Id == user.Id)!);
|
||||
|
||||
await Clients.Client("LOBBY").SendAsync("RoomUserList", GroupUsers["LOBBY"]);
|
||||
Log($"User {user.Username} Has Left The Lobby");
|
||||
}
|
||||
|
||||
[HubMethodName("JoinRoom")]
|
||||
[Authorize]
|
||||
public async Task JoinRoomAsync(User user, Room room)
|
||||
@ -125,20 +168,6 @@ namespace qtc_api.Hubs
|
||||
Log($"User {user.Username} Has Joined {room.Name}");
|
||||
}
|
||||
|
||||
[HubMethodName("JoinRoomGuest")]
|
||||
public async Task JoinRoomGuestAsync(string roomId, string username)
|
||||
{
|
||||
// here we can just add the client to the room group and call it a day since the user isn't authenticated
|
||||
var room = await _roomService.GetRoom(roomId);
|
||||
|
||||
if (room != null && room.Success && room.Data != null)
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, room.Data.Id);
|
||||
|
||||
await Clients.Group(room.Data.Id).SendAsync("GuestJoin", username);
|
||||
}
|
||||
}
|
||||
|
||||
[HubMethodName("LeaveRoom")]
|
||||
[Authorize]
|
||||
public async Task LeaveRoomAsync(User user, Room room)
|
||||
@ -155,9 +184,10 @@ namespace qtc_api.Hubs
|
||||
|
||||
[HubMethodName("SendMessage")]
|
||||
[Authorize]
|
||||
public async Task SendMessageAsync(User user, Message message, Room room)
|
||||
public async Task SendMessageAsync(User user, Message message, bool IsLobbyMsg, Room room = null!)
|
||||
{
|
||||
await Clients.Group(room.Id).SendAsync("RoomMessage", $"{user.Username}: {message.Content}");
|
||||
if(IsLobbyMsg == true) { await Clients.Group("LOBBY").SendAsync("RoomMessage", $"[{user.Username}] {message.Content}"); return; }
|
||||
await Clients.Group(room.Id).SendAsync("RoomMessage", $"[{user.Username}] {message.Content}");
|
||||
}
|
||||
|
||||
[HubMethodName("SendDirectMessage")]
|
||||
|
||||
@ -18,7 +18,6 @@ using qtc_api.Services.CurrencyGamesService;
|
||||
using qtc_api.Services.GameRoomService;
|
||||
using qtc_api.Services.StoreService;
|
||||
using qtc_api.Services.EmailService;
|
||||
using qtc_api.Services.BucketService;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -67,7 +66,6 @@ builder.Services.AddScoped<ITokenService, TokenService>();
|
||||
builder.Services.AddScoped<IRoomService, RoomService>();
|
||||
builder.Services.AddScoped<IContactService, ContactService>();
|
||||
builder.Services.AddScoped<StoreService>();
|
||||
builder.Services.AddScoped<IBucketService, BucketService>();
|
||||
|
||||
builder.Services.AddSingleton<CurrencyGamesService>();
|
||||
builder.Services.AddSingleton<GameRoomService>();
|
||||
|
||||
@ -1,127 +0,0 @@
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace qtc_api.Services.BucketService
|
||||
{
|
||||
public class BucketService : IBucketService
|
||||
{
|
||||
public string ServiceUrl { get; private set; }
|
||||
public string AccessKey { get; private set; }
|
||||
public string SecretKey { get; private set; }
|
||||
public string ProfileImagesBucketName { get; private set; }
|
||||
public string ImagesBucketName { get; private set; }
|
||||
|
||||
private static AmazonS3Client S3Client;
|
||||
private IConfiguration _config;
|
||||
private ILogger<BucketService> _logger;
|
||||
|
||||
private bool Enabled { get; set; }
|
||||
public BucketService(IConfiguration config, ILogger<BucketService> logger)
|
||||
{
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
|
||||
var serviceUrl = _config["S3Config:S3ServiceUrl"];
|
||||
var accessKey = _config["S3Config:S3AccessKey"];
|
||||
var secretKey = _config["S3Config:S3SecretKey"];
|
||||
var profileImagesBucket = _config["S3Config:S3ProfileImagesBucket"];
|
||||
var imagesBucket = _config["S3Config:S3ImagesBucket"];
|
||||
var enabled = _config.GetValue<bool>("S3Config:S3Enabled");
|
||||
|
||||
if (serviceUrl != null) ServiceUrl = serviceUrl;
|
||||
if (accessKey != null) AccessKey = accessKey;
|
||||
if (secretKey != null) SecretKey = secretKey;
|
||||
if (profileImagesBucket != null) ProfileImagesBucketName = profileImagesBucket;
|
||||
if (imagesBucket != null) ImagesBucketName = imagesBucket;
|
||||
|
||||
Enabled = enabled;
|
||||
|
||||
var credentials = new BasicAWSCredentials(AccessKey, SecretKey);
|
||||
S3Client = new AmazonS3Client(credentials, new AmazonS3Config
|
||||
{
|
||||
ServiceURL = ServiceUrl,
|
||||
});
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> PutProfileImage(string userId, string imageName, byte[] imageBytes)
|
||||
{
|
||||
if (!Enabled)
|
||||
{
|
||||
_logger.LogWarning("Not Using S3 Bucket. Performance May Be Degraded.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var stream = new MemoryStream(imageBytes);
|
||||
var request = new PutObjectRequest()
|
||||
{
|
||||
Key = $@"{userId}/{imageName}",
|
||||
BucketName = ProfileImagesBucketName,
|
||||
InputStream = stream,
|
||||
DisablePayloadSigning = true,
|
||||
DisableDefaultChecksumValidation = true
|
||||
};
|
||||
|
||||
var response = await S3Client.PutObjectAsync(request);
|
||||
return response.HttpStatusCode == System.Net.HttpStatusCode.OK || response.HttpStatusCode == System.Net.HttpStatusCode.Accepted;
|
||||
} catch (AmazonS3Exception ex)
|
||||
{
|
||||
_logger.LogError($"S3 Bucket Threw An Exception\n{ex.Message}\n{ex.StackTrace}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<byte[]?> GetProfileImageBytes(string userId, string imageName)
|
||||
{
|
||||
if (!Enabled)
|
||||
{
|
||||
_logger.LogWarning("Not Using S3 Bucket. Performance May Be Degraded.");
|
||||
return default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var response = await S3Client.GetObjectAsync(ProfileImagesBucketName, $@"{userId}/{imageName}");
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
using (var stream = response.ResponseStream)
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(ms);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
else return default;
|
||||
} catch (AmazonS3Exception ex)
|
||||
{
|
||||
_logger.LogError($"S3 Bucket Threw An Exception\n{ex.Message}\n{ex.StackTrace}");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteProfileImage(string userId, string imageName)
|
||||
{
|
||||
if (!Enabled)
|
||||
{
|
||||
_logger.LogWarning("Not Using S3 Bucket. Performance May Be Degraded.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var response = await S3Client.DeleteObjectAsync(ProfileImagesBucketName, $@"{userId}/{imageName}");
|
||||
return response.HttpStatusCode == System.Net.HttpStatusCode.OK || response.HttpStatusCode == System.Net.HttpStatusCode.Accepted;
|
||||
}
|
||||
catch (AmazonS3Exception ex)
|
||||
{
|
||||
_logger.LogError($"S3 Bucket Threw An Exception\n{ex.Message}\n{ex.StackTrace}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
namespace qtc_api.Services.BucketService
|
||||
{
|
||||
public interface IBucketService
|
||||
{
|
||||
public string ServiceUrl { get; }
|
||||
public string AccessKey { get; }
|
||||
public string SecretKey { get; }
|
||||
public string ProfileImagesBucketName { get; }
|
||||
public string ImagesBucketName { get; }
|
||||
public Task<bool> PutProfileImage(string userId, string imageName, byte[] imageBytes);
|
||||
public Task<byte[]?> GetProfileImageBytes(string userId, string imageName);
|
||||
public Task<bool> DeleteProfileImage(string userId, string imageName);
|
||||
}
|
||||
}
|
||||
@ -1,21 +1,18 @@
|
||||
using qtc_api.Services.BucketService;
|
||||
using qtc_api.Services.EmailService;
|
||||
using qtc_api.Services.EmailService;
|
||||
|
||||
namespace qtc_api.Services.UserService
|
||||
{
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IBucketService _bucketService;
|
||||
private readonly DataContext _dataContext;
|
||||
|
||||
private long idMax = 900000000000000000;
|
||||
|
||||
public UserService(IConfiguration configuration, IBucketService bucketService, DataContext dataContext)
|
||||
public UserService(IConfiguration configuration, DataContext dataContext)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_dataContext = dataContext;
|
||||
_bucketService = bucketService;
|
||||
}
|
||||
|
||||
public async Task<ServiceResponse<User>> AddUser(UserDto userReq)
|
||||
@ -262,40 +259,20 @@ namespace qtc_api.Services.UserService
|
||||
if (!Directory.Exists(cdnPath)) Directory.CreateDirectory(cdnPath!);
|
||||
if (!Directory.Exists($"{cdnPath}/{userId}")) Directory.CreateDirectory($"{cdnPath}/{userId}");
|
||||
|
||||
if (userToUpdate.ProfilePicture != null)
|
||||
await _bucketService.DeleteProfileImage(userToUpdate.Id, userToUpdate.ProfilePicture);
|
||||
var fileName = $"{userId}.pfp";
|
||||
var filePath = Path.Combine(cdnPath ?? "./user-content", userId, fileName);
|
||||
|
||||
var fileName = $"{Guid.NewGuid()}.{file.FileName.Split('.')[1]}";
|
||||
using var ms = new MemoryStream();
|
||||
file.CopyTo(ms);
|
||||
var result = await _bucketService.PutProfileImage(userId, fileName, ms.ToArray());
|
||||
if (result)
|
||||
using (var stream = File.Create(filePath))
|
||||
{
|
||||
userToUpdate.ProfilePicture = fileName;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = fileName;
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
// fallback to using local cdn
|
||||
|
||||
var filePath = Path.Combine(cdnPath ?? "./user-content", userId, fileName);
|
||||
userToUpdate.ProfilePicture = fileName;
|
||||
|
||||
using (var stream = File.Create(filePath))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
||||
userToUpdate.ProfilePicture = fileName;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = fileName;
|
||||
}
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = fileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -317,38 +294,26 @@ namespace qtc_api.Services.UserService
|
||||
{
|
||||
if (user.ProfilePicture != null)
|
||||
{
|
||||
var response = await _bucketService.GetProfileImageBytes(user.Id, user.ProfilePicture);
|
||||
if (response != null)
|
||||
if (!Directory.Exists(cdnPath))
|
||||
{
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = user.ProfilePicture;
|
||||
serviceResponse.Data = new FileContentResult(response, "image/jpeg");
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = "User Content Folder Does Not Exist Yet.";
|
||||
return serviceResponse;
|
||||
}
|
||||
else
|
||||
|
||||
var pic = Path.Combine(cdnPath, userId, user.ProfilePicture);
|
||||
|
||||
if (!File.Exists(pic))
|
||||
{
|
||||
// try local cdn
|
||||
|
||||
if (!Directory.Exists(cdnPath))
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = "User Content Folder Does Not Exist Yet.";
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
var pic = Path.Combine(cdnPath, userId, user.ProfilePicture);
|
||||
|
||||
if (!File.Exists(pic))
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = "User Does Not Have A Profile Picture.";
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = user.ProfilePicture;
|
||||
|
||||
serviceResponse.Data = new FileContentResult(File.ReadAllBytes(pic), "image/jpeg");
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = "User Does Not Have A Profile Picture.";
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = user.ProfilePicture;
|
||||
|
||||
serviceResponse.Data = new FileContentResult(File.ReadAllBytes(pic), "image/jpeg");
|
||||
} else
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
|
||||
@ -14,14 +14,6 @@
|
||||
"SMTPPassword": "",
|
||||
"SMTPSenderAddress": ""
|
||||
},
|
||||
"S3Config": {
|
||||
"S3Enabled": false,
|
||||
"S3ServiceUrl": "",
|
||||
"S3AccessKey": "",
|
||||
"S3SecretKey": "",
|
||||
"S3ProfileImagesBucket": "qtcnet-profileimages",
|
||||
"S3ImagesBucket": "qtcnet-images"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.11.2" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="MailKit" Version="4.13.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.7" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user