Add remove mode

This commit is contained in:
2024-08-29 11:37:56 +02:00
parent b168c688cd
commit 32ad037601

15
main.go
View File

@@ -39,6 +39,7 @@ func main() {
quality := flag.Int("quality", 80, "Quality of the image")
to := flag.String("to", ".jpg", "Extension to transcode to")
nosafe := flag.Bool("nosafe", false, "Prevents backup of the original before encoding")
rm := flag.Bool("rm", false, "Removes the original after transcoding")
flag.Parse()
if _, ok := codecs[*to]; !ok {
@@ -49,6 +50,7 @@ func main() {
log.Printf("Using quality: %d", *quality)
log.Printf("Transcoding to: %s", *to)
log.Printf("Nosafe mode: %v", *nosafe)
log.Printf("Remove mode: %v", *rm)
var wg sync.WaitGroup
for _, file := range flag.Args() {
@@ -110,6 +112,19 @@ func main() {
return
}
log.Printf("Wrote %d bytes to %s", n, newfile)
if *rm {
if file == newfile {
log.Printf("Won't remove newfile %s", file)
return
}
log.Printf("Removing %s", file)
err = os.Remove(file)
if err!= nil {
Error.Printf("Failed to remove file %v: %v", file, err)
return
}
}
}(file)
}
wg.Wait()