Files
2026-07-23 18:21:43 -07:00

71 lines
2.0 KiB
C#

using DeluxeBackend.Models;
using DeluxeBackend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic.FileIO;
using System.ComponentModel.DataAnnotations;
using static DeluxeBackend.Enums;
namespace DeluxeBackend.Controllers
{
[Route("Storage")]
[ApiController]
public class StorageController(IJwtService jwt, ILiteDbService db, ICdnService cdn) : ControllerBase
{
[HttpPost("upload")]
[Authorize(Roles = "gameClient")]
public async Task<IActionResult> Upload([FromForm(Name = "FileType"), Required] FileType fileType, [FromForm(Name = "File"), Required] IFormFile file)
{
Account? account = await jwt.GetLogin(User);
if (account == null)
{
return Unauthorized();
}
string path = "fuck";
switch (fileType)
{
case FileType.Image:
path = "img";
break;
case FileType.Holotar:
path = "data";
break;
case FileType.SubRoomSave:
path = "room";
break;
case FileType.RoomMetadata:
path = "room";
break;
case FileType.Invention:
path = "invention";
break;
default:
return BadRequest();
}
using var stream = file.OpenReadStream();
var (remotePath, proof) = await cdn.UploadFile(
stream,
path,
account
);
if (remotePath == null)
{
return StatusCode(500, $"Failed to upload {fileType} to the CDN.");
}
return Ok(new
{
Filename = remotePath,
Hash = "meow",
OwnershipProof = proof
});
}
}
}