using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using MediatR; using Host.Models; using Knot.Shared.Kernel; using Knot.Modules.Chats.Application.TelegramImport; using System.Threading; using System.Threading.Tasks; namespace Host.Controllers; [Authorize] [ApiController] [Route("api/import/telegram")] public sealed class TelegramImportController : ControllerBase { private readonly ISender _sender; private readonly IUserContext _userContext; public TelegramImportController(ISender sender, IUserContext userContext) { _sender = sender; _userContext = userContext; } [HttpPost("analyze")] [DisableRequestSizeLimit] [RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024 * 1024 * 1024)] // 10GB for big exports public async Task Analyze(IFormFile file, CancellationToken ct) { if (file == null) { return BadRequest("No file uploaded."); } using var stream = file.OpenReadStream(); var command = new AnalyzeImportCommand(stream, file.FileName); var result = await _sender.Send(command, ct); if (result.IsFailure) { return BadRequest(result.Error.Description ?? result.Error.Code); } return Ok(result.Value); } [HttpPost("execute")] public async Task Execute([FromBody] ExecuteImportRequest req, CancellationToken ct) { var command = new ExecuteImportCommand(_userContext.UserId, req.Token, req.Mapping, req.GroupName); var result = await _sender.Send(command, ct); if (result.IsFailure) { return BadRequest(result.Error.Description ?? result.Error.Code); } return Ok(result.Value); } }