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

View File

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

View File

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

View File

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