using Microsoft.AspNetCore.Mvc; using Convert_It_Online.Services; using Microsoft.AspNetCore.Localization; namespace Convert_It_Online.Areas.AudioTools.Controllers { [Area("AudioTools")] [Route("{culture}/[area]/[controller]")] [Route("[area]/[controller]")] // Adicionado para Share Target sem cultura fixa public class SpeechToTextController : Controller { private readonly IAudioTranscriptionService _transcriptionService; private readonly ILogger _logger; public SpeechToTextController(IAudioTranscriptionService transcriptionService, ILogger logger) { _transcriptionService = transcriptionService; _logger = logger; } [HttpGet] public IActionResult Index() { return View(); } [HttpPost] public async Task Transcribe(IFormFile audioFile) { if (audioFile == null || audioFile.Length == 0) { ViewBag.Error = "Por favor, selecione um arquivo de áudio."; return View("Index"); } var culture = HttpContext.Features.Get()?.RequestCulture.UICulture.Name ?? "pt-BR"; var tempPath = Path.GetTempFileName(); try { using (var stream = new FileStream(tempPath, FileMode.Create)) { await audioFile.CopyToAsync(stream); } var transcription = await _transcriptionService.TranscribeAsync(tempPath, culture); ViewBag.Result = transcription; } catch (Exception ex) { _logger.LogError(ex, "Erro no controller ao transcrever."); ViewBag.Error = "Erro ao processar o áudio. Verifique se o formato é suportado."; } finally { if (System.IO.File.Exists(tempPath)) System.IO.File.Delete(tempPath); } return View("Index"); } [HttpPost("HandleShare")] public async Task HandleShare(IFormFile audio) { // O Android via Share Target costuma enviar como 'audio' ou 'file' return await Transcribe(audio); } } }