front y back minimos
This commit is contained in:
13
back/.config/dotnet-tools.json
Normal file
13
back/.config/dotnet-tools.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": 1,
|
||||
"isRoot": true,
|
||||
"tools": {
|
||||
"dotnet-ef": {
|
||||
"version": "9.0.8",
|
||||
"commands": [
|
||||
"dotnet-ef"
|
||||
],
|
||||
"rollForward": false
|
||||
}
|
||||
}
|
||||
}
|
4
back/Constants.cs
Normal file
4
back/Constants.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
public static class Constants
|
||||
{
|
||||
public const string Data = "data";
|
||||
}
|
12
back/DTO/PhotoFormModel.cs
Normal file
12
back/DTO/PhotoFormModel.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace back.DTO;
|
||||
|
||||
public class PhotoFormModel
|
||||
{
|
||||
public required string Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Tags { get; set; }
|
||||
public string? People { get; set; }
|
||||
public IFormFile? Image { get; set; }
|
||||
public string? Ubicacion { get; set; }
|
||||
public string? Evento { get; set; }
|
||||
}
|
@@ -9,13 +9,22 @@ public class Program
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
Directory.CreateDirectory(Constants.Data);
|
||||
// Add services to the container.
|
||||
builder.Services.AddDbContext<PhotoContext>(options =>options.UseSqlite("Data Source=photos.db"));
|
||||
builder.Services.AddDbContext<PhotoContext>(options => options.UseSqlite($"Data Source={Constants.Data}/photos.db"));
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll",
|
||||
builder => builder.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader());
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -29,6 +38,7 @@ public class Program
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
|
@@ -1,24 +1,14 @@
|
||||
using back.ApiService.context;
|
||||
using back.ApiService.models;
|
||||
using back.DTO;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace back.controllers;
|
||||
|
||||
public class PhotoFormModel
|
||||
{
|
||||
public required string Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Tags { get; set; }
|
||||
public string? People { get; set; }
|
||||
public IFormFile? Image { get; set; }
|
||||
public string? Ubicacion { get; set; }
|
||||
public string? Evento { get; set; }
|
||||
}
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PhotoController(PhotoContext photoContext) : ControllerBase
|
||||
public class PhotosController(PhotoContext photoContext) : ControllerBase
|
||||
{
|
||||
private readonly PhotoContext _photoContext = photoContext;
|
||||
|
||||
@@ -37,18 +27,33 @@ public class PhotoController(PhotoContext photoContext) : ControllerBase
|
||||
.ToListAsync();
|
||||
|
||||
Response.Headers.Append("X-Total-Count", totalItems.ToString());
|
||||
|
||||
|
||||
return Ok(photos);
|
||||
}
|
||||
|
||||
// GET api/<PhotoController>/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Photo>> Get(Guid id)
|
||||
[HttpGet("{id}/{res}")]
|
||||
public async Task<IActionResult> Get(Guid id, string res = "low")
|
||||
{
|
||||
var photo = await _photoContext.Photos.FindAsync(id);
|
||||
if (photo == null)
|
||||
return NotFound();
|
||||
return photo;
|
||||
|
||||
string? filePath = res.ToLower() switch
|
||||
{
|
||||
"low" => photo.LowResUrl,
|
||||
"mid" => photo.MidResUrl,
|
||||
"high" => photo.HighResUrl,
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (filePath == null || !System.IO.File.Exists(Path.Combine(Constants.Data, filePath)))
|
||||
return NotFound();
|
||||
|
||||
var fileBytes = await System.IO.File.ReadAllBytesAsync(Path.Combine(Constants.Data, filePath));
|
||||
var contentType = "image/jpeg"; // Cambia si usas otro formato
|
||||
|
||||
return File(fileBytes, contentType);
|
||||
}
|
||||
|
||||
// POST api/<PhotoController>
|
@@ -1,5 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
@@ -10,29 +11,33 @@ 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);
|
||||
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(Photo.LowResFolder);
|
||||
Directory.CreateDirectory(Photo.MidResFolder);
|
||||
Directory.CreateDirectory(Photo.HighResFolder);
|
||||
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(photo.LowResUrl);
|
||||
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(photo.MidResUrl);
|
||||
img.Save(Path.Join(Constants.Data, photo.MidResUrl));
|
||||
|
||||
// Original
|
||||
stream.Position = 0;
|
||||
using var original = Image.Load(stream);
|
||||
original.Save(photo.HighResUrl);
|
||||
original.Save(Path.Join(Constants.Data, photo.HighResUrl));
|
||||
|
||||
return photo;
|
||||
}
|
||||
@@ -45,7 +50,7 @@ public class Photo
|
||||
public const string MidResFolder = "imgs/mid";
|
||||
public const string HighResFolder = "imgs/high";
|
||||
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
[Key]
|
||||
public Guid Id { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
|
BIN
back/photos.db-shm
Normal file
BIN
back/photos.db-shm
Normal file
Binary file not shown.
0
back/photos.db-wal
Normal file
0
back/photos.db-wal
Normal file
Reference in New Issue
Block a user