using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; namespace back.ApiService.models; public class PhotoBuilder { public static Photo Build(string? title, string? description, List? tags, List? personsIn, IFormFile image) { // Genera un nombre de archivo único var fileName = $"{Guid.NewGuid()}{Path.GetExtension(image.FileName)}"; var photo = new Photo(title, description, tags, personsIn, fileName); // Asegura que los directorios existen Directory.CreateDirectory(Photo.LowResFolder); Directory.CreateDirectory(Photo.MidResFolder); Directory.CreateDirectory(Photo.HighResFolder); // Procesa y guarda las imágenes using var stream = image.OpenReadStream(); using var img = Image.Load(stream); // Baja resolución (480px) img.Mutate(x => x.Resize(new ResizeOptions { Size = new Size(480, 0), Mode = ResizeMode.Max })); img.Save(photo.LowResUrl); // Media resolución (720px) img.Mutate(x => x.Resize(new ResizeOptions { Size = new Size(720, 0), Mode = ResizeMode.Max })); img.Save(photo.MidResUrl); // Original stream.Position = 0; using var original = Image.Load(stream); original.Save(photo.HighResUrl); return photo; } } [Table("Photo")] public class Photo { public const string LowResFolder = "imgs/low"; public const string MidResFolder = "imgs/mid"; public const string HighResFolder = "imgs/high"; [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string? Title { get; set; } public string? Description { get; set; } public string? Ubicación { get; set; } public string? Evento { get; set; } public List? Tags { get; set; } public List? PersonsIn { get; set; } public string? FileName { get; set; } public string LowResUrl { get => Path.Join(LowResFolder, FileName); } public string MidResUrl { get => Path.Join(MidResFolder, FileName); } public string HighResUrl { get => Path.Join(HighResFolder, FileName); } public float? Ranking { get; set; } public DateTime? CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public string? CreatedBy { get; set; } public string? UpdatedBy { get; set; } private Photo() { //Id = Guid.NewGuid(); Tags = []; PersonsIn = []; CreatedAt = DateTime.Now; UpdatedAt = DateTime.Now; CreatedBy = "system"; UpdatedBy = "system"; Ranking = 0.0f; } public Photo(string? title, string? description, List? tags, List? personsIn, string? fileName) : this() { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("FileName cannot be null or empty.", nameof(fileName)); } Title = title; Description = description; Tags = tags ?? []; PersonsIn = personsIn ?? []; FileName = fileName; } }