116 lines
4.1 KiB
C#
116 lines
4.1 KiB
C#
using DeluxeBackend.Models;
|
|
using DeluxeBackend.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using static DeluxeBackend.Enums;
|
|
|
|
namespace DeluxeBackend.Controllers.Api
|
|
{
|
|
[Route("api/images")]
|
|
[ApiController]
|
|
public class ImagesController(IJwtService jwt, ILiteDbService db, ICdnService cdn, DiscordBotService discord) : ControllerBase
|
|
{
|
|
[HttpGet("v2/named")]
|
|
public async Task<IActionResult> Named()
|
|
{
|
|
return Ok(new List<object>());
|
|
}
|
|
[HttpGet("v5/cheered/bulk")]
|
|
public async Task<IActionResult> cheeredBulk()
|
|
{
|
|
return Ok(new List<object>());
|
|
}
|
|
|
|
[HttpGet("v5/player/{accId}")]
|
|
public async Task<IActionResult> FromPlayer(long accId)
|
|
{
|
|
var images = db.SavedImages.Include(x => x.Player).Find(x => x.Player.Id == accId && x.Accessibility == SavedImageAccessibility.Public).OrderByDescending(x => x.Id).Select(image => image.ToDictionary()).ToList();
|
|
return Ok(images);
|
|
}
|
|
|
|
public class SavedImageMetaDTO
|
|
{
|
|
[JsonPropertyName("playerIds")]
|
|
[Required]
|
|
public required List<ulong> PlayerIds { get; set; }
|
|
[JsonPropertyName("savedImageType")]
|
|
[Required]
|
|
public required SavedImageType SavedImageType { get; set; }
|
|
[JsonPropertyName("roomId")]
|
|
[Required]
|
|
public required long RoomId { get; set; }
|
|
[JsonPropertyName("accessibility")]
|
|
[Required]
|
|
public required SavedImageAccessibility Accessibility { get; set; }
|
|
}
|
|
|
|
|
|
[HttpPost("v4/uploadsaved")]
|
|
[Authorize(Roles = "gameClient")]
|
|
//[Consumes("image/jpeg")]
|
|
public async Task<IActionResult> UploadSaved([FromForm(Name = "image"), Required] IFormFile Image, [FromForm(Name = "imgMeta"), Required] string metaJson)
|
|
{
|
|
|
|
Account? account = await jwt.GetLogin(User);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound("User not found in database");
|
|
}
|
|
|
|
SavedImageMetaDTO? Meta;
|
|
try
|
|
{
|
|
Meta = JsonSerializer.Deserialize<SavedImageMetaDTO>(metaJson);
|
|
if (Meta == null) return BadRequest("Invalid metadata format.");
|
|
}
|
|
catch { return BadRequest("Metadata is not valid JSON."); }
|
|
|
|
using var stream = Image.OpenReadStream();
|
|
|
|
string? remotePath = await cdn.UploadFile(
|
|
stream,
|
|
"img"
|
|
);
|
|
|
|
if (remotePath == null)
|
|
{
|
|
return StatusCode(500, "Failed to upload image to the CDN.");
|
|
}
|
|
|
|
SavedImage image = new()
|
|
{
|
|
Player = account,
|
|
PlayerIds = Meta.PlayerIds,
|
|
ImageName = remotePath,
|
|
SavedImageType = Meta.SavedImageType,
|
|
Accessibility = Meta.Accessibility,
|
|
RoomId = Meta.RoomId == -1 ? null : Meta.RoomId
|
|
};
|
|
db.SavedImages.Insert(image);
|
|
|
|
await discord.SendSavedImg(account, image);
|
|
|
|
return Ok(new { ImageName = remotePath });
|
|
}
|
|
|
|
[HttpGet("v6")]
|
|
public async Task<IActionResult> GetByName([FromQuery] string name)
|
|
{
|
|
SavedImage? image = db.SavedImages.Include(x => x.Player).FindOne(x => x.ImageName == name);
|
|
if (image == null) { return NotFound(); }
|
|
return Ok(image.ToDictionary());
|
|
}
|
|
[HttpGet("v4/room/{roomId}")]
|
|
public async Task<IActionResult> GetByRoom([FromQuery] long roomId)
|
|
{
|
|
var images = db.SavedImages.Include(x => x.Player).Find(x => x.RoomId == roomId && x.Accessibility == SavedImageAccessibility.Public).OrderByDescending(x => x.Id).Select(image => image.ToDictionary()).ToList();
|
|
return Ok(images);
|
|
}
|
|
}
|
|
}
|