Cleanup + filesize check

This commit is contained in:
Keroosha 2023-04-13 21:29:02 +03:00
parent f9feb1f0e7
commit 25c2abdb83
4 changed files with 45 additions and 28 deletions

View File

@ -42,10 +42,10 @@ let runProc filename args startDir =
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
FileName = filename,
Arguments = ArgumentEscaper.EscapeAndConcatenate args
FileName = filename
)
List.iter procStartInfo.ArgumentList.Add <| args
match startDir with | Some d -> procStartInfo.WorkingDirectory <- d | _ -> ()
let outputs = System.Collections.Generic.List<string>()

View File

@ -27,5 +27,9 @@
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Keroosha.SilencerBot.Misc\Keroosha.SilencerBot.Misc.csproj" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,6 @@
module Keroosha.SilencerBot.Processing
open System
open System.Collections.Generic
open System.IO
open System.Net.Http
open System.Text.Json
@ -16,12 +15,18 @@ open Microsoft.FSharp.Control
module TgClient = Funogram.Tools.Api
let http = new HttpClient()
let inline private (>>=) a b = (a, b) |> async.Bind
let getContext (x: UserJob) = x.Context |> JsonSerializer.Deserialize<JsonJobContext>
let serializeContext (x: JsonJobContext) = x |> JsonSerializer.Serialize<JsonJobContext>
let downloadUrl token path = $"https://api.telegram.org/file/bot{token}/{path}"
let getArtifactsPath (ctx: JsonJobContext) =
let cleanName = Path.GetFileNameWithoutExtension ctx.savePath
Path.Combine(Path.GetDirectoryName ctx.savePath, $"{cleanName}_Instruments.wav"),
Path.Combine(Path.GetDirectoryName ctx.savePath, $"{cleanName}_Vocals.wav")
let failJob (x: UserJob, ctx: JsonJobContext) (errMessage: String) =
{ x with
State = JobState.Failed
@ -103,18 +108,19 @@ let processExecuting (job: UserJob, botConfig: Funogram.Types.BotConfig, config:
]
let! stdout, stderr = runProc $"/usr/bin/python" args (Some config.processorWorkingPath)
let ctxWithOutput = { ctx with stdout = stdout; stderr = stderr }
return { job with
State = JobState.UploadingResults
Context = serializeContext ctxWithOutput
}
let instrumentalPath, vocalsPath = getArtifactsPath ctx
return match File.Exists instrumentalPath && File.Exists vocalsPath with
| true -> { job with State = JobState.UploadingResults; Context = serializeContext ctxWithOutput }
| false ->
File.Delete ctx.savePath
failJob (job, ctxWithOutput) "Missing artifacts"
}
let processUploading (job: UserJob, botConfig: Funogram.Types.BotConfig, config: BotConfig) =
async {
let ctx = getContext job
let cleanName = Path.GetFileNameWithoutExtension ctx.savePath
let instrumentalPath = Path.Combine(Path.GetDirectoryName ctx.savePath, $"{cleanName}_Instruments.wav")
let vocalsPath = Path.Combine(Path.GetDirectoryName ctx.savePath, $"{cleanName}_Vocals.wav")
let instrumentalPath, vocalsPath = getArtifactsPath ctx
use fInstrumental = File.OpenRead instrumentalPath
use fVocals = File.OpenRead vocalsPath

View File

@ -16,6 +16,7 @@ type VoiceRemoveArgs = {
fileId: string
filename: string
chatId: int64
fileSize: uint64
}
type StartArgs = {
@ -34,6 +35,7 @@ let runDate = DateTime.UtcNow
let greetingText = "Привет, отправь мне вавку и я попробую убрать из нее голос"
let jobCreatedText = "Запустил задачу, обрабатываю"
let unknownUserText = "Прости, но мы с тобой не знакомы, отправь мне в ЛС /start"
let fileTooLargeText = "Файл слишком большой, я умею работать с файлами до 40мб"
let isVoiceRemoveAction(update: Update) =
update.Message.IsSome &&
@ -54,7 +56,8 @@ let resolveUpdate (ctx: UpdateContext) =
VoiceRemove {
fileId = x.Message.Value.Audio.Value.FileId
chatId = x.Message.Value.From.Value.Id
filename = x.Message.Value.Audio.Value.FileName.Value
filename = x.Message.Value.Audio.Value.FileName.Value
fileSize = x.Message.Value.Audio.Value.FileSize.Value |> uint64
}
| x when isStartCommand x ->
Start {
@ -78,22 +81,26 @@ let createBotInbox (cfg: BotConfig, botCfg: Funogram.Types.BotConfig, dbFactory:
do! TgClient.makeRequestAsync botCfg <| Api.sendMessage x.chatId unknownUserText |> Async.Ignore
()
| _ ->
let jobContext: JsonJobContext = {
stderr = ""
stdout = ""
chatId = x.chatId
fileId = x.fileId
savePath = Path.Combine(cfg.tempSavePath, x.filename)
}
let job: UserJob = { Id = Guid.NewGuid()
State = JobState.New
UserId = user.Id
Context = JsonSerializer.Serialize(jobContext)
WorkerId = Nullable()
}
do! db.InsertAsync(job) |> Async.AwaitTask |> Async.Ignore
do! trx.CommitAsync() |> Async.AwaitTask
do! TgClient.makeRequestAsync botCfg <| Api.sendMessage x.chatId jobCreatedText |> Async.Ignore
match Math.Floor(double(x.fileSize) / double(Math.Pow(1024, 2))) < double(40) with
| true ->
let jobContext: JsonJobContext = {
stderr = ""
stdout = ""
chatId = x.chatId
fileId = x.fileId
savePath = Path.Combine(cfg.tempSavePath, x.filename)
}
let job: UserJob = { Id = Guid.NewGuid()
State = JobState.New
UserId = user.Id
Context = JsonSerializer.Serialize(jobContext)
WorkerId = Nullable()
}
do! db.InsertAsync(job) |> Async.AwaitTask |> Async.Ignore
do! trx.CommitAsync() |> Async.AwaitTask
do! TgClient.makeRequestAsync botCfg <| Api.sendMessage x.chatId jobCreatedText |> Async.Ignore
| false ->
do! TgClient.makeRequestAsync botCfg <| Api.sendMessage x.chatId fileTooLargeText |> Async.Ignore
()
| Start x ->
let db = dbFactory()