Skip to content

Commit

Permalink
add ability to extract double extensions like .tar.gz
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Aug 31, 2024
1 parent e6bd7ba commit 1a1c148
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
8 changes: 6 additions & 2 deletions replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func getExtVars(replacementInput string) (extVars, error) {

submatches := extensionVarRegex.FindAllStringSubmatch(replacementInput, -1)

expectedLength := 2
expectedLength := 3

for _, submatch := range submatches {
if len(submatch) < expectedLength {
Expand All @@ -421,7 +421,11 @@ func getExtVars(replacementInput string) (extVars, error) {

match.regex = regex

match.transformToken = submatch[1]
if submatch[1] != "" {
match.doubleExt = true
}

match.transformToken = submatch[2]

evMatches.matches = append(evMatches.matches, match)
}
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions replace/replace_test/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@ func TestVariables(t *testing.T) {
"-r", "{xt.OtherSerialNumber}{ext}", "--exiftool-opts", `--extractEmbedded`,
},
},
{
Name: "replace file with double extension",
Changes: file.Changes{
{
BaseDir: "testdata",
Source: "file.tar.gz",
},
{
BaseDir: "testdata",
Source: "audio.mp3",
},
},
Want: []string{
"testdata/june.tar.gz",
"testdata/june.mp3",
},
Args: []string{
"-r", "june{2ext}",
},
},
}

replaceTest(t, testCases)
Expand Down
2 changes: 1 addition & 1 deletion replace/variable_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func init() {
fmt.Sprintf("{+f(?:\\.%s)?}+", transformTokens),
)
extensionVarRegex = regexp.MustCompile(
fmt.Sprintf("{+ext(?:\\.%s)?}+", transformTokens),
fmt.Sprintf("{+(2)?ext(?:\\.%s)?}+", transformTokens),
)
parentDirVarRegex = regexp.MustCompile(
fmt.Sprintf("{+(\\d+)?p(?:\\.%s)?}+", transformTokens),
Expand Down
1 change: 1 addition & 0 deletions replace/variable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type filenameVars struct {
type extVarMatch struct {
regex *regexp.Regexp
transformToken string
doubleExt bool
}

type extVars struct {
Expand Down
36 changes: 20 additions & 16 deletions replace/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ const (
md5Hash hashAlgorithm = "md5"
)

// var (
// currentBaseDir string
// // newDirIndex keeps track of the index of the first file in a new
// // directory.
// newDirIndex int
// )

// Exif represents exif information from an image file.
type Exif struct {
Latitude string
Expand Down Expand Up @@ -905,13 +898,30 @@ func replaceFilenameVars(
return target
}

func replaceExtVars(target, fileExt string, ev extVars) string {
func getDoubleExtension(filename string) string {
ext := filepath.Ext(filename)
ext2 := filepath.Ext(pathutil.StripExtension(filename))

return ext2 + ext
}

func replaceExtVars(change *file.Change, ev extVars) (target string) {
for i := range ev.matches {
fileExt := filepath.Ext(change.OriginalName)

if change.IsDir {
fileExt = "" // Directory names do not have extensions
}

current := ev.matches[i]

if current.doubleExt {
fileExt = getDoubleExtension(change.OriginalName)
}

source := transformString(fileExt, current.transformToken)

target = regexReplace(current.regex, target, source, 0)
target = regexReplace(current.regex, change.Target, source, 0)
}

return target
Expand All @@ -924,8 +934,6 @@ func replaceVariables(
change *file.Change,
vars *variables,
) error {
fileExt := filepath.Ext(change.OriginalName)

if len(vars.filename.matches) > 0 {
sourceName := filepath.Base(change.OriginalName)
if !change.IsDir {
Expand All @@ -940,11 +948,7 @@ func replaceVariables(
}

if len(vars.ext.matches) > 0 {
if change.IsDir {
fileExt = ""
}

change.Target = replaceExtVars(change.Target, fileExt, vars.ext)
change.Target = replaceExtVars(change, vars.ext)
}

if len(vars.parentDir.matches) > 0 {
Expand Down

0 comments on commit 1a1c148

Please sign in to comment.