Thumbnail

This commit is contained in:
PascalMarchesseault
2024-11-24 14:23:45 -05:00
parent 502ca0313a
commit ebf0c0da96

View File

@@ -14,10 +14,9 @@ public record PostContentRequest(
string Title, string Title,
string Description, string Description,
IFormFileCollection? Files, IFormFileCollection? Files,
string ThumbnailUrl, IFormFile? Thumbnail, // Nouveau champ pour le thumbnail
string[]? ExternalUrls); string[]? ExternalUrls);
[PublicAPI] [PublicAPI]
public sealed class PostContentRequestValidator : Validator<PostContentRequest> public sealed class PostContentRequestValidator : Validator<PostContentRequest>
{ {
@@ -42,8 +41,9 @@ public sealed class PostContentRequestValidator : Validator<PostContentRequest>
RuleForEach(r => r.ExternalUrls) RuleForEach(r => r.ExternalUrls)
.NotEmpty().WithMessage("External URL cannot be empty") .NotEmpty().WithMessage("External URL cannot be empty")
.Must(url => Uri.IsWellFormedUriString(url, UriKind.Absolute)).WithMessage("External URL is not valid"); .Must(url => Uri.IsWellFormedUriString(url, UriKind.Absolute)).WithMessage("External URL is not valid");
RuleFor(r => r.Thumbnail)
.Must(file => file == null || file.Length > 0).WithMessage("Thumbnail file is invalid.");
} }
} }
@@ -64,24 +64,19 @@ public sealed class PostContent(
CancellationToken ct) CancellationToken ct)
{ {
var urls = new ConcurrentBag<string>(); var urls = new ConcurrentBag<string>();
string? thumbnailUrl = null;
// Traitement des fichiers uploadés
if (req.Files is not null) if (req.Files is not null)
{ {
await Parallel.ForEachAsync( await Parallel.ForEachAsync(
req.Files, req.Files,
ct, ct,
async ( async (file, ict) =>
file,
ict) =>
{ {
try try
{ {
var contentUrl = await SaveFileAsync( var contentUrl = await SaveFileAsync(req.CreatorId, req.Id, file, ict);
req.CreatorId,
req.Id,
file,
ict);
urls.Add(contentUrl); urls.Add(contentUrl);
} }
catch (Exception ex) catch (Exception ex)
@@ -90,15 +85,26 @@ public sealed class PostContent(
} }
}); });
} }
if (req.ExternalUrls is not null) // Téléversement du thumbnail
if (req.Thumbnail is not null)
{ {
foreach (var externalUrl in req.ExternalUrls) try
{ {
urls.Add(externalUrl); thumbnailUrl = await SaveFileAsync(
req.CreatorId,
req.Id,
req.Thumbnail,
ct,
isThumbnail: true); // Utilisation d'un chemin spécifique pour le thumbnail
}
catch (Exception ex)
{
Logger.LogError("Error uploading thumbnail: {ErrorMessage}", ex.Message);
} }
} }
// Ajout à la base de données
await context.Contents.AddAsync( await context.Contents.AddAsync(
new Content new Content
{ {
@@ -107,13 +113,13 @@ public sealed class PostContent(
Title = req.Title, Title = req.Title,
Description = req.Description, Description = req.Description,
Urls = urls.IsEmpty ? null : urls.ToArray(), Urls = urls.IsEmpty ? null : urls.ToArray(),
ThumbnailUrl = req.ThumbnailUrl, ThumbnailUrl = thumbnailUrl,
}, },
ct); ct);
await context.SaveChangesAsync(ct); await context.SaveChangesAsync(ct);
// Récupérer le contenu pour le retour
var content = await context var content = await context
.Contents .Contents
.Select(c => new ContentModel .Select(c => new ContentModel
@@ -141,11 +147,18 @@ public sealed class PostContent(
Guid creatorId, Guid creatorId,
Guid contentId, Guid contentId,
IFormFile file, IFormFile file,
CancellationToken ct = default) CancellationToken ct = default,
bool isThumbnail = false) // Nouveau paramètre pour indiquer si c'est un thumbnail
{ {
// Détermine le chemin du blob
var blobName = isThumbnail
? $"{creatorId}/{SubDirectoryNames.Contents}/{contentId}/thumbnail-{file.FileName}" // Chemin pour le thumbnail
: $"{creatorId}/{SubDirectoryNames.Contents}/{contentId}/{file.FileName}"; // Chemin pour les fichiers normaux
// Téléverse le fichier
var url = await blobStorage.UploadFileAsync( var url = await blobStorage.UploadFileAsync(
ContainerNames.Creators, ContainerNames.Creators,
$"{creatorId}/{SubDirectoryNames.Contents}/{contentId}/{file.FileName}", blobName,
file.OpenReadStream(), file.OpenReadStream(),
file.ContentType, file.ContentType,
ct: ct); ct: ct);