Files
mmorales.photo/back/models/Photo.cs
2025-08-07 19:19:30 +02:00

97 lines
3.3 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace back.ApiService.models;
public class PhotoBuilder
{
public static Photo Build(string? title, string? description, List<string>? tags, List<string>? personsIn, IFormFile image)
{
// Genera un nombre de archivo único
var id = Guid.NewGuid();
var fileName = $"{id}{Path.GetExtension(image.FileName)}";
var photo = new Photo(title, description, tags, personsIn, fileName)
{
Id = id
};
// Asegura que los directorios existen
Directory.CreateDirectory(Path.Join(Constants.Data, Photo.LowResFolder));
Directory.CreateDirectory(Path.Join(Constants.Data, Photo.MidResFolder));
Directory.CreateDirectory(Path.Join(Constants.Data, 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(Path.Join(Constants.Data, photo.LowResUrl));
// Media resolución (720px)
img.Mutate(x => x.Resize(new ResizeOptions { Size = new Size(720, 0), Mode = ResizeMode.Max }));
img.Save(Path.Join(Constants.Data, photo.MidResUrl));
// Original
stream.Position = 0;
using var original = Image.Load(stream);
original.Save(Path.Join(Constants.Data, 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]
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<string>? Tags { get; set; }
public List<string>? 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<string>? tags, List<string>? 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;
}
}