mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-15 02:21:37 +00:00
Refactored the backend code
This commit is contained in:
parent
bab7b10d2b
commit
8ed5927f67
@ -1,65 +0,0 @@
|
|||||||
using AIStudio.Tools.Services;
|
|
||||||
|
|
||||||
namespace AIStudio;
|
|
||||||
|
|
||||||
public static class AudioRecorderHandler
|
|
||||||
{
|
|
||||||
public static void AddAudioRecorderHandlers(this IEndpointRouteBuilder app)
|
|
||||||
{
|
|
||||||
var router = app.MapGroup("/audio");
|
|
||||||
|
|
||||||
router.MapPost("/upload", UploadAudio)
|
|
||||||
.DisableAntiforgery();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task<IResult> UploadAudio(HttpRequest request, RustService rustService)
|
|
||||||
{
|
|
||||||
var form = await request.ReadFormAsync();
|
|
||||||
var file = form.Files.GetFile("audio");
|
|
||||||
var mimeType = form["mimeType"].ToString();
|
|
||||||
|
|
||||||
if (file is null || file.Length == 0)
|
|
||||||
return Results.BadRequest("No audio file uploaded.");
|
|
||||||
|
|
||||||
var actualMimeType = !string.IsNullOrWhiteSpace(mimeType)
|
|
||||||
? mimeType
|
|
||||||
: file.ContentType;
|
|
||||||
|
|
||||||
var extension = GetFileExtension(actualMimeType);
|
|
||||||
|
|
||||||
var dataDirectory = await rustService.GetDataDirectory();
|
|
||||||
var recordingDirectory = Path.Combine(dataDirectory, "audioRecordings");
|
|
||||||
if(!Path.Exists(recordingDirectory))
|
|
||||||
Directory.CreateDirectory(recordingDirectory);
|
|
||||||
|
|
||||||
var fileName = $"recording_{DateTime.UtcNow:yyyyMMdd_HHmmss}{extension}";
|
|
||||||
var filePath = Path.Combine(recordingDirectory, fileName);
|
|
||||||
|
|
||||||
await using var stream = File.Create(filePath);
|
|
||||||
await file.CopyToAsync(stream);
|
|
||||||
|
|
||||||
return Results.Ok(new
|
|
||||||
{
|
|
||||||
FileName = fileName,
|
|
||||||
MimeType = actualMimeType,
|
|
||||||
Size = file.Length
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static string GetFileExtension(string mimeType)
|
|
||||||
{
|
|
||||||
var baseMimeType = mimeType.Split(';')[0].Trim().ToLowerInvariant();
|
|
||||||
|
|
||||||
return baseMimeType switch
|
|
||||||
{
|
|
||||||
"audio/webm" => ".webm",
|
|
||||||
"audio/ogg" => ".ogg",
|
|
||||||
"audio/mp4" => ".m4a",
|
|
||||||
"audio/mpeg" => ".mp3",
|
|
||||||
"audio/wav" => ".wav",
|
|
||||||
"audio/x-wav" => ".wav",
|
|
||||||
"audio/aac" => ".aac",
|
|
||||||
_ => ".audio"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -403,18 +403,16 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
|
|||||||
|
|
||||||
private async Task SendAudioToBackend(AudioRecordingResult recording)
|
private async Task SendAudioToBackend(AudioRecordingResult recording)
|
||||||
{
|
{
|
||||||
#warning No need to send the recording to the backend (Blazor Hybrid)
|
|
||||||
var audioBytes = Convert.FromBase64String(recording.Data);
|
var audioBytes = Convert.FromBase64String(recording.Data);
|
||||||
|
|
||||||
using var content = new MultipartFormDataContent();
|
|
||||||
var fileContent = new ByteArrayContent(audioBytes);
|
|
||||||
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(recording.MimeType);
|
|
||||||
|
|
||||||
var extension = GetFileExtension(recording.MimeType);
|
var extension = GetFileExtension(recording.MimeType);
|
||||||
content.Add(fileContent, "audio", $"recording{extension}");
|
var dataDirectory = await this.RustService.GetDataDirectory();
|
||||||
content.Add(new StringContent(recording.MimeType), "mimeType");
|
var recordingDirectory = Path.Combine(dataDirectory, "audioRecordings");
|
||||||
|
if(!Path.Exists(recordingDirectory))
|
||||||
|
Directory.CreateDirectory(recordingDirectory);
|
||||||
|
|
||||||
await this.HttpClient.PostAsync("/audio/upload", content);
|
var fileName = $"recording_{DateTime.UtcNow:yyyyMMdd_HHmmss}{extension}";
|
||||||
|
var filePath = Path.Combine(recordingDirectory, fileName);
|
||||||
|
await File.WriteAllBytesAsync(filePath, audioBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetFileExtension(string mimeType)
|
private static string GetFileExtension(string mimeType)
|
||||||
|
|||||||
@ -192,7 +192,6 @@ internal sealed class Program
|
|||||||
programLogger.LogInformation("Initialize internal file system.");
|
programLogger.LogInformation("Initialize internal file system.");
|
||||||
app.Use(Redirect.HandlerContentAsync);
|
app.Use(Redirect.HandlerContentAsync);
|
||||||
app.Use(FileHandler.HandlerAsync);
|
app.Use(FileHandler.HandlerAsync);
|
||||||
app.AddAudioRecorderHandlers();
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user