2024-09-26 22:26:40 +02:00
|
|
|
using Coravel.Invocable;
|
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
|
|
|
using Foxnouns.Backend.Extensions;
|
|
|
|
using Foxnouns.Backend.Services;
|
2024-12-09 14:52:31 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-09-26 22:26:40 +02:00
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Jobs;
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public class CreateFlagInvocable(
|
|
|
|
DatabaseContext db,
|
|
|
|
ObjectStorageService objectStorageService,
|
|
|
|
ILogger logger
|
|
|
|
) : IInvocable, IInvocableWithPayload<CreateFlagPayload>
|
2024-09-26 22:26:40 +02:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger = logger.ForContext<CreateFlagInvocable>();
|
|
|
|
public required CreateFlagPayload Payload { get; set; }
|
|
|
|
|
|
|
|
public async Task Invoke()
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Information(
|
|
|
|
"Creating flag {FlagId} for user {UserId} with image data length {DataLength}",
|
|
|
|
Payload.Id,
|
|
|
|
Payload.UserId,
|
|
|
|
Payload.ImageData.Length
|
|
|
|
);
|
2024-09-26 22:26:40 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-12-09 14:52:31 +01:00
|
|
|
PrideFlag? flag = await db.PrideFlags.FirstOrDefaultAsync(f =>
|
|
|
|
f.Id == Payload.Id && f.UserId == Payload.UserId
|
|
|
|
);
|
|
|
|
if (flag == null)
|
|
|
|
{
|
|
|
|
_logger.Warning(
|
|
|
|
"Got a flag create job for {FlagId} but it doesn't exist, aborting",
|
|
|
|
Payload.Id
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
(string? hash, Stream? image) = await ImageObjectExtensions.ConvertBase64UriToImage(
|
2024-11-23 20:41:41 +01:00
|
|
|
Payload.ImageData,
|
2024-12-08 15:07:25 +01:00
|
|
|
256,
|
|
|
|
false
|
2024-10-02 00:28:07 +02:00
|
|
|
);
|
2024-09-26 22:26:40 +02:00
|
|
|
await objectStorageService.PutObjectAsync(Path(hash), image, "image/webp");
|
|
|
|
|
2024-12-09 14:52:31 +01:00
|
|
|
flag.Hash = hash;
|
|
|
|
db.Update(flag);
|
2024-09-26 22:26:40 +02:00
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
_logger.Information("Uploaded flag {FlagId} with hash {Hash}", flag.Id, flag.Hash);
|
|
|
|
}
|
|
|
|
catch (ArgumentException ae)
|
|
|
|
{
|
|
|
|
_logger.Warning("Invalid data URI for flag {FlagId}: {Reason}", Payload.Id, ae.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2024-09-26 23:03:50 +02:00
|
|
|
public static string Path(string hash) => $"flags/{hash}.webp";
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|