90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
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<string>? tags, List<string>? 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 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;
|
|
}
|
|
}
|