JJ Colocate

This commit is contained in:
2025-06-30 14:23:20 -04:00
parent 360fa53439
commit 24abf5607f
77 changed files with 5700 additions and 5700 deletions

View File

@@ -1,19 +1,19 @@
using Microsoft.AspNetCore.Mvc;
namespace AobaServer.Controllers.Api;
[Route("/api/auth")]
public class AuthApi : ControllerBase
{
[HttpGet("login")]
public Task<IActionResult> LoginAsync()
{
throw new NotImplementedException();
}
[HttpGet("register")]
public Task<IActionResult> RegisterAsync()
{
throw new NotImplementedException();
}
}
using Microsoft.AspNetCore.Mvc;
namespace AobaServer.Controllers.Api;
[Route("/api/auth")]
public class AuthApi : ControllerBase
{
[HttpGet("login")]
public Task<IActionResult> LoginAsync()
{
throw new NotImplementedException();
}
[HttpGet("register")]
public Task<IActionResult> RegisterAsync()
{
throw new NotImplementedException();
}
}

View File

@@ -1,38 +1,38 @@
using AobaCore.Models;
using AobaCore.Services;
using AobaServer.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
namespace AobaServer.Controllers.Api;
[ApiController, Authorize]
[Route("/api/media")]
public class MediaApi(AobaService aoba) : ControllerBase
{
[HttpPost("upload")]
public async Task<IActionResult> UploadAsync([FromForm] IFormFile file, CancellationToken cancellationToken)
{
var media = await aoba.UploadFileAsync(file.OpenReadStream(), file.FileName, User.GetId(), cancellationToken);
if (media.HasError)
return Problem(detail: media.Error.Message, statusCode: StatusCodes.Status400BadRequest);
return Ok(new
{
media = media.Value,
url = media.Value.GetMediaUrl()
});
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(ObjectId id, CancellationToken cancellationToken)
{
await aoba.DeleteFileAsync(id, cancellationToken);
return Ok();
}
}
using AobaCore.Models;
using AobaCore.Services;
using AobaServer.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
namespace AobaServer.Controllers.Api;
[ApiController, Authorize]
[Route("/api/media")]
public class MediaApi(AobaService aoba) : ControllerBase
{
[HttpPost("upload")]
public async Task<IActionResult> UploadAsync([FromForm] IFormFile file, CancellationToken cancellationToken)
{
var media = await aoba.UploadFileAsync(file.OpenReadStream(), file.FileName, User.GetId(), cancellationToken);
if (media.HasError)
return Problem(detail: media.Error.Message, statusCode: StatusCodes.Status400BadRequest);
return Ok(new
{
media = media.Value,
url = media.Value.GetMediaUrl()
});
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(ObjectId id, CancellationToken cancellationToken)
{
await aoba.DeleteFileAsync(id, cancellationToken);
return Ok();
}
}

View File

@@ -1,37 +1,37 @@
using AobaCore.Services;
using AobaServer.Models;
using AobaServer.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace AobaServer.Controllers;
//allow login via http during debug testing
#if DEBUG
[AllowAnonymous]
[Route("auth")]
public class AuthController(AccountsService accountsService, AuthInfo authInfo) : Controller
{
[HttpPost("login")]
public async Task<IActionResult> Login([FromForm] string username, [FromForm] string password, CancellationToken cancellationToken)
{
var user = await accountsService.VerifyLoginAsync(username, password, cancellationToken);
if (user == null)
return Problem("Invalid login Credentials", statusCode: StatusCodes.Status400BadRequest);
Response.Cookies.Append("token", user.GetToken(authInfo), new CookieOptions
{
IsEssential = true,
SameSite = SameSiteMode.Strict,
Secure = true,
});
return Ok();
}
}
using AobaCore.Services;
using AobaServer.Models;
using AobaServer.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace AobaServer.Controllers;
//allow login via http during debug testing
#if DEBUG
[AllowAnonymous]
[Route("auth")]
public class AuthController(AccountsService accountsService, AuthInfo authInfo) : Controller
{
[HttpPost("login")]
public async Task<IActionResult> Login([FromForm] string username, [FromForm] string password, CancellationToken cancellationToken)
{
var user = await accountsService.VerifyLoginAsync(username, password, cancellationToken);
if (user == null)
return Problem("Invalid login Credentials", statusCode: StatusCodes.Status400BadRequest);
Response.Cookies.Append("token", user.GetToken(authInfo), new CookieOptions
{
IsEssential = true,
SameSite = SameSiteMode.Strict,
Secure = true,
});
return Ok();
}
}
#endif

View File

@@ -1,65 +1,65 @@
using AobaCore.Models;
using AobaCore.Services;
using HeyRed.Mime;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
namespace AobaServer.Controllers;
[Route("/m")]
public class MediaController(AobaService aobaService, ILogger<MediaController> logger) : Controller
{
[HttpGet("{id}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken)
{
var file = await aobaService.GetFileStreamAsync(id, cancellationToken: cancellationToken);
if (file.HasError)
{
logger.LogError(file.Error.Exception, "Failed to load media stream");
return NotFound();
}
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
_ = aobaService.IncrementFileViewCountAsync(id, cancellationToken);
return File(file, mime, true);
}
/// <summary>
/// Redirect legacy media urls to the new url
/// </summary>
/// <param name="id"></param>
/// <param name="rest"></param>
/// <param name="aoba"></param>
/// <returns></returns>
[HttpGet("/i/{id}/{*rest}")]
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, CancellationToken cancellationToken)
{
var media = await aobaService.GetMediaAsync(id, cancellationToken);
if (media == null)
return NotFound();
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
}
[HttpGet("thumb/{id}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> ThumbAsync(ObjectId id, [FromServices] ThumbnailService thumbnailService, [FromQuery] ThumbnailSize size = ThumbnailSize.Medium, CancellationToken cancellationToken = default)
{
var thumb = await thumbnailService.GetOrCreateThumbnailAsync(id, size, cancellationToken);
if (thumb.HasError)
{
logger.LogError("Failed to generate thumbnail: {}", thumb.Error);
return DefaultThumbnailAsync();
}
return File(thumb, "image/webp", true);
}
[NonAction]
private IActionResult DefaultThumbnailAsync()
{
return NoContent();
}
}
using AobaCore.Models;
using AobaCore.Services;
using HeyRed.Mime;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
namespace AobaServer.Controllers;
[Route("/m")]
public class MediaController(AobaService aobaService, ILogger<MediaController> logger) : Controller
{
[HttpGet("{id}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken)
{
var file = await aobaService.GetFileStreamAsync(id, cancellationToken: cancellationToken);
if (file.HasError)
{
logger.LogError(file.Error.Exception, "Failed to load media stream");
return NotFound();
}
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
_ = aobaService.IncrementFileViewCountAsync(id, cancellationToken);
return File(file, mime, true);
}
/// <summary>
/// Redirect legacy media urls to the new url
/// </summary>
/// <param name="id"></param>
/// <param name="rest"></param>
/// <param name="aoba"></param>
/// <returns></returns>
[HttpGet("/i/{id}/{*rest}")]
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, CancellationToken cancellationToken)
{
var media = await aobaService.GetMediaAsync(id, cancellationToken);
if (media == null)
return NotFound();
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
}
[HttpGet("thumb/{id}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> ThumbAsync(ObjectId id, [FromServices] ThumbnailService thumbnailService, [FromQuery] ThumbnailSize size = ThumbnailSize.Medium, CancellationToken cancellationToken = default)
{
var thumb = await thumbnailService.GetOrCreateThumbnailAsync(id, size, cancellationToken);
if (thumb.HasError)
{
logger.LogError("Failed to generate thumbnail: {}", thumb.Error);
return DefaultThumbnailAsync();
}
return File(thumb, "image/webp", true);
}
[NonAction]
private IActionResult DefaultThumbnailAsync()
{
return NoContent();
}
}