Skip to content

Commit

Permalink
rename all jpegs to .jpg
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Dec 2, 2022
1 parent 1de431f commit 15f484c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/imageconvert/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/kmulvey/imageconvert/pkg/imageconvert"
log "github.com/sirupsen/logrus"
)

var incorrectJPGRegex = regexp.MustCompile(".*.jpeg$|.*.JPG$|.*.JPEG$|")

type conversionResult struct {
OriginalFileName string
ConvertedFileName string
Expand Down Expand Up @@ -72,6 +77,17 @@ func convertImage(file string, compress bool) conversionResult {
}
}

// all jpgs must end with ".jpg" case sensitive, not .jpeg, .JPG, etc.
if incorrectJPGRegex.MatchString(result.ConvertedFileName) {
var renamedFile = strings.Replace(result.ConvertedFileName, filepath.Ext(result.ConvertedFileName), ".jpg", 1)
if err := os.Rename(result.ConvertedFileName, renamedFile); err != nil {
result.Error = fmt.Errorf("could rename file: %s, err: %w", result.ConvertedFileName, err)
return result
}
result.ConvertedFileName = renamedFile
result.Renamed = true
}

// RESET MODTIME
err = os.Chtimes(result.ConvertedFileName, originalFileStat.ModTime(), originalFileStat.ModTime())
if err != nil {
Expand Down

0 comments on commit 15f484c

Please sign in to comment.