All checks were successful
Deploy ASP.NET MVC to OCI / build-and-deploy (push) Successful in 21m10s
70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
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<SpeechToTextController> _logger;
|
|
|
|
public SpeechToTextController(IAudioTranscriptionService transcriptionService, ILogger<SpeechToTextController> logger)
|
|
{
|
|
_transcriptionService = transcriptionService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> 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<IRequestCultureFeature>()?.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<IActionResult> HandleShare(IFormFile audio)
|
|
{
|
|
// O Android via Share Target costuma enviar como 'audio' ou 'file'
|
|
return await Transcribe(audio);
|
|
}
|
|
}
|
|
}
|