From 6ef9b20fbca25a10490a00442a34598c82f71450 Mon Sep 17 00:00:00 2001 From: Meisterlala <6453306+Meisterlala@users.noreply.github.com> Date: Sun, 4 Jan 2026 14:07:12 +0100 Subject: [PATCH] Fix output checking --- PKGBUILD | 2 +- cmd/compressor/processor.go | 35 +++++++++++++++++++---------------- install.sh | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 3c2effc..d9302dd 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: Misti pkgname=compressor-git -pkgver=r17.739419a +pkgver=r22.43d7421 pkgrel=1 pkgdesc="Video compressor service that watches input directory and compresses videos using ffmpeg" arch=('x86_64' 'aarch64') diff --git a/cmd/compressor/processor.go b/cmd/compressor/processor.go index 2d4a1b4..97e88e2 100644 --- a/cmd/compressor/processor.go +++ b/cmd/compressor/processor.go @@ -33,6 +33,17 @@ func processFile(ctx context.Context, cfg config, originalPath string) error { return fmt.Errorf("stability check: %w", err) } + // If the intended output already exists, do not queue/process this input. + outputPath, err := buildOutputPath(cfg, originalPath) + if err != nil { + if errors.Is(err, os.ErrExist) { + log.Printf("skip %s: output already exists: %v", originalPath, err) + return nil + } + sendDiscordFailure(cfg.discordWebhookURL, originalPath, fmt.Sprintf("build output path: %v", err)) + return err + } + processingPath := originalPath + cfg.processingSuffix if err := os.Rename(originalPath, processingPath); err != nil { if errors.Is(err, os.ErrNotExist) { @@ -63,12 +74,6 @@ func processFile(ctx context.Context, cfg config, originalPath string) error { } }() - outputPath, err := buildOutputPath(cfg, originalPath) - if err != nil { - sendDiscordFailure(cfg.discordWebhookURL, originalPath, fmt.Sprintf("build output path: %v", err)) - return err - } - if err := runFFMPEG(ctx, cfg, processingPath, outputPath); err != nil { sendDiscordFailure(cfg.discordWebhookURL, originalPath, err.Error()) if removeErr := os.Remove(outputPath); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) { @@ -162,20 +167,18 @@ func buildOutputPath(cfg config, originalPath string) (string, error) { ext = "." + ext } - candidate := filepath.Join(cfg.outputDir, base+ext) - if _, err := os.Stat(candidate); errors.Is(err, os.ErrNotExist) { - return candidate, nil - } if err := os.MkdirAll(cfg.outputDir, 0o755); err != nil { return "", fmt.Errorf("ensure output dir: %w", err) } - for idx := 1; idx < 10_000; idx++ { - candidate = filepath.Join(cfg.outputDir, fmt.Sprintf("%s_%d%s", base, idx, ext)) - if _, err := os.Stat(candidate); errors.Is(err, os.ErrNotExist) { - return candidate, nil - } + + candidate := filepath.Join(cfg.outputDir, base+ext) + if _, err := os.Stat(candidate); err == nil { + return "", fmt.Errorf("output already exists: %s: %w", candidate, os.ErrExist) + } else if !errors.Is(err, os.ErrNotExist) { + return "", fmt.Errorf("stat output candidate: %w", err) } - return "", fmt.Errorf("unable to find free output name for %s", originalPath) + + return candidate, nil } func runFFMPEG(ctx context.Context, cfg config, inputPath, outputPath string) error { diff --git a/install.sh b/install.sh index 97a0864..9cbf037 100755 --- a/install.sh +++ b/install.sh @@ -9,7 +9,7 @@ makepkg -si --noextract echo "Enabling and starting the user service..." systemctl --user enable compressor.service -systemctl --user start compressor.service +systemctl --user restart compressor.service echo "Service status:" systemctl --user status compressor.service -- 2.51.2