From 96f43895fbed856cc9a2aefa3b869ade09ec41e7 Mon Sep 17 00:00:00 2001 From: itzmarkoni Date: Mon, 16 Jun 2025 18:52:35 -0400 Subject: [PATCH] Added TTS --- Astrocore.Api/.dockerignore | 30 +++++++++ Astrocore.Api/Astrocore.Api.csproj | 3 + .../Controllers/TextToSpeechController.cs | 67 +++++++++++++++++++ Astrocore.Api/Dockerfile | 17 ++++- Astrocore.Api/Program.cs | 6 +- docker-compose.dcproj | 19 ++++++ docker-compose.override.yml | 12 ++++ docker-compose.yml | 6 ++ launchSettings.json | 11 +++ 9 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 Astrocore.Api/.dockerignore create mode 100644 Astrocore.Api/Controllers/TextToSpeechController.cs create mode 100644 docker-compose.dcproj create mode 100644 docker-compose.override.yml create mode 100644 docker-compose.yml create mode 100644 launchSettings.json diff --git a/Astrocore.Api/.dockerignore b/Astrocore.Api/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/Astrocore.Api/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/Astrocore.Api/Astrocore.Api.csproj b/Astrocore.Api/Astrocore.Api.csproj index 958df04..df2ef5c 100644 --- a/Astrocore.Api/Astrocore.Api.csproj +++ b/Astrocore.Api/Astrocore.Api.csproj @@ -6,9 +6,12 @@ enable 916bd77d-132b-42cb-93e0-55d05b0d4f4b Linux + . + ..\docker-compose.dcproj + diff --git a/Astrocore.Api/Controllers/TextToSpeechController.cs b/Astrocore.Api/Controllers/TextToSpeechController.cs new file mode 100644 index 0000000..d81ef2a --- /dev/null +++ b/Astrocore.Api/Controllers/TextToSpeechController.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; +using System.Net.Http; +using System.Text; +using System.Text.Json; + +namespace Astrocore.Api.Controllers +{ + + + [Route("api/[controller]")] + [ApiController] + public class TextToSpeechController : ControllerBase + { + private readonly IHttpClientFactory _httpClientFactory; + public TextToSpeechController(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + [HttpGet] + public async Task Index(string message) + { + var client = _httpClientFactory.CreateClient(); + var body = $"msg={Uri.EscapeDataString(message)}&lang=Gwyneth&source=ttsmp3"; + var request = new HttpRequestMessage(HttpMethod.Post, "https://ttsmp3.com/makemp3_new.php") + { + Content = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded") + }; + + var response = await client.SendAsync(request); + var json = await response.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(json); + var mp3File = doc.RootElement.GetProperty("URL").GetString(); + var mp3Bytes = await client.GetByteArrayAsync(mp3File); + var tempDir = Path.GetTempPath(); + var mp3Path = Path.Combine(tempDir, "tts.mp3"); + var dfpwmPath = Path.Combine(tempDir, "tts.dfpwm"); + + await System.IO.File.WriteAllBytesAsync(mp3Path, mp3Bytes); + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "ffmpeg", + Arguments = $"-i \"{mp3Path}\" -ac 1 -c:a dfpwm -ar 48k \"{dfpwmPath}\"", + RedirectStandardError = true, + UseShellExecute = false + } + }; + + process.Start(); + string errorOutput = await process.StandardError.ReadToEndAsync(); + process.WaitForExit(); + + if (!System.IO.File.Exists(dfpwmPath)) + { + return StatusCode(500, $"FFmpeg failed: {errorOutput}"); + + } + + var dfpwmBytes = await System.IO.File.ReadAllBytesAsync(dfpwmPath); + return File(dfpwmBytes, "application/octet-stream", "tts.dfpwm"); + } + } +} diff --git a/Astrocore.Api/Dockerfile b/Astrocore.Api/Dockerfile index 3743b21..4f1a543 100644 --- a/Astrocore.Api/Dockerfile +++ b/Astrocore.Api/Dockerfile @@ -2,8 +2,15 @@ # This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base -USER $APP_UID WORKDIR /app + +# Install ffmpeg before switching user +RUN apt-get update && \ + apt-get install -y ffmpeg && \ + rm -rf /var/lib/apt/lists/* + +USER $APP_UID + EXPOSE 8080 EXPOSE 8081 @@ -26,5 +33,11 @@ RUN dotnet publish "./Astrocore.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publ # This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration) FROM base AS final WORKDIR /app + +# Install ffmpeg +RUN apt-get update && \ + apt-get install -y ffmpeg && \ + rm -rf /var/lib/apt/lists/* + COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "Astrocore.Api.dll"] \ No newline at end of file +ENTRYPOINT ["dotnet", "Astrocore.Api.dll"] diff --git a/Astrocore.Api/Program.cs b/Astrocore.Api/Program.cs index e93868b..7cf3432 100644 --- a/Astrocore.Api/Program.cs +++ b/Astrocore.Api/Program.cs @@ -8,7 +8,7 @@ namespace Astrocore.Api var builder = WebApplication.CreateBuilder(args); // Add services to the container. - + builder.Services.AddHttpClient(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -23,8 +23,8 @@ namespace Astrocore.Api app.UseSwaggerUI(); } - //app.UseHttpsRedirection(); - //app.UseAuthorization(); + app.UseHttpsRedirection(); + app.UseAuthorization(); app.MapControllers(); diff --git a/docker-compose.dcproj b/docker-compose.dcproj new file mode 100644 index 0000000..0ab0d6c --- /dev/null +++ b/docker-compose.dcproj @@ -0,0 +1,19 @@ + + + + 2.1 + Linux + False + a88d4e72-07da-43d3-b2a5-a71f5278b0e1 + LaunchBrowser + {Scheme}://localhost:{ServicePort}/swagger + astrocore.api + + + + docker-compose.yml + + + + + \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..7b15dbb --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,12 @@ +services: + astrocore.api: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_HTTP_PORTS=8080 + - ASPNETCORE_HTTPS_PORTS=8081 + ports: + - "8080" + - "8081" + volumes: + - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro + - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..87af264 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + astrocore.api: + image: ${DOCKER_REGISTRY-}astrocoreapi + build: + context: Astrocore.Api + dockerfile: Dockerfile diff --git a/launchSettings.json b/launchSettings.json new file mode 100644 index 0000000..bee2e8f --- /dev/null +++ b/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "Docker Compose": { + "commandName": "DockerCompose", + "commandVersion": "1.0", + "serviceActions": { + "astrocore.api": "StartDebugging" + } + } + } +} \ No newline at end of file