Added TTS
This commit is contained in:
30
Astrocore.Api/.dockerignore
Normal file
30
Astrocore.Api/.dockerignore
Normal file
@@ -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/**
|
||||
@@ -6,9 +6,12 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>916bd77d-132b-42cb-93e0-55d05b0d4f4b</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>.</DockerfileContext>
|
||||
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FFMpegCore" Version="5.2.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
67
Astrocore.Api/Controllers/TextToSpeechController.cs
Normal file
67
Astrocore.Api/Controllers/TextToSpeechController.cs
Normal file
@@ -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<IActionResult> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"]
|
||||
@@ -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();
|
||||
|
||||
|
||||
19
docker-compose.dcproj
Normal file
19
docker-compose.dcproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectVersion>2.1</ProjectVersion>
|
||||
<DockerTargetOS>Linux</DockerTargetOS>
|
||||
<DockerPublishLocally>False</DockerPublishLocally>
|
||||
<ProjectGuid>a88d4e72-07da-43d3-b2a5-a71f5278b0e1</ProjectGuid>
|
||||
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
|
||||
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/swagger</DockerServiceUrl>
|
||||
<DockerServiceName>astrocore.api</DockerServiceName>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Include="docker-compose.override.yml">
|
||||
<DependentUpon>docker-compose.yml</DependentUpon>
|
||||
</None>
|
||||
<None Include="docker-compose.yml" />
|
||||
<None Include=".dockerignore" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
12
docker-compose.override.yml
Normal file
12
docker-compose.override.yml
Normal file
@@ -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
|
||||
6
docker-compose.yml
Normal file
6
docker-compose.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
services:
|
||||
astrocore.api:
|
||||
image: ${DOCKER_REGISTRY-}astrocoreapi
|
||||
build:
|
||||
context: Astrocore.Api
|
||||
dockerfile: Dockerfile
|
||||
11
launchSettings.json
Normal file
11
launchSettings.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Docker Compose": {
|
||||
"commandName": "DockerCompose",
|
||||
"commandVersion": "1.0",
|
||||
"serviceActions": {
|
||||
"astrocore.api": "StartDebugging"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user