From 25c2abdb83dd6037302e10ae8df7cac167015793 Mon Sep 17 00:00:00 2001 From: Keroosha Date: Thu, 13 Apr 2023 21:29:02 +0300 Subject: [PATCH] Cleanup + filesize check --- Keroosha.SilencerBot/Env.fs | 4 +- .../Keroosha.SilencerBot.fsproj | 4 ++ Keroosha.SilencerBot/Processing.fs | 24 +++++++---- Keroosha.SilencerBot/Telegram.fs | 41 +++++++++++-------- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/Keroosha.SilencerBot/Env.fs b/Keroosha.SilencerBot/Env.fs index ba3a214..2589c46 100644 --- a/Keroosha.SilencerBot/Env.fs +++ b/Keroosha.SilencerBot/Env.fs @@ -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() diff --git a/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj b/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj index 73b9c28..4d96368 100644 --- a/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj +++ b/Keroosha.SilencerBot/Keroosha.SilencerBot.fsproj @@ -27,5 +27,9 @@ + + + + diff --git a/Keroosha.SilencerBot/Processing.fs b/Keroosha.SilencerBot/Processing.fs index b4aeac3..9e4c41a 100644 --- a/Keroosha.SilencerBot/Processing.fs +++ b/Keroosha.SilencerBot/Processing.fs @@ -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 let serializeContext (x: JsonJobContext) = x |> JsonSerializer.Serialize 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 diff --git a/Keroosha.SilencerBot/Telegram.fs b/Keroosha.SilencerBot/Telegram.fs index 392fdbc..76a6507 100644 --- a/Keroosha.SilencerBot/Telegram.fs +++ b/Keroosha.SilencerBot/Telegram.fs @@ -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()