From a6e06844f3871407d992ed6c6da09fbda8c10bc9 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 1 Jun 2020 01:35:07 +0200 Subject: [PATCH] Ignore whitespace when stripping sequence from filename #335 Signed-off-by: Michael Mayer --- pkg/fs/base.go | 4 +++- pkg/fs/base_test.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/fs/base.go b/pkg/fs/base.go index 749749c55..6a7fdaa8a 100644 --- a/pkg/fs/base.go +++ b/pkg/fs/base.go @@ -29,7 +29,7 @@ func Base(fileName string, stripSequence bool) string { } // other common sequential naming schemes - if end := strings.Index(basename, " ("); end != -1 { + if end := strings.Index(basename, "("); end != -1 { // copies created by Chrome & Windows, example: IMG_1234 (2) basename = basename[:end] } else if end := strings.Index(basename, " copy"); end != -1 { @@ -37,6 +37,8 @@ func Base(fileName string, stripSequence bool) string { basename = basename[:end] } + basename = strings.TrimSpace(basename) + return basename } diff --git a/pkg/fs/base_test.go b/pkg/fs/base_test.go index 0ce086915..93e2ca6e0 100644 --- a/pkg/fs/base_test.go +++ b/pkg/fs/base_test.go @@ -67,6 +67,26 @@ func TestBase(t *testing.T) { result := Base("/testdata/Test (3).jpg", false) assert.Equal(t, "Test (3)", result) }) + t.Run("20180506_091537_DSC02122.JPG", func(t *testing.T) { + result := Base("20180506_091537_DSC02122.JPG", true) + assert.Equal(t, "20180506_091537_DSC02122", result) + }) + t.Run("20180506_091537_DSC02122 (+3.3).JPG", func(t *testing.T) { + result := Base("20180506_091537_DSC02122 (+3.3).JPG", true) + assert.Equal(t, "20180506_091537_DSC02122", result) + }) + t.Run("20180506_091537_DSC02122 (-2.7).JPG", func(t *testing.T) { + result := Base("20180506_091537_DSC02122 (-2.7).JPG", true) + assert.Equal(t, "20180506_091537_DSC02122", result) + }) + t.Run("20180506_091537_DSC02122(+3.3).JPG", func(t *testing.T) { + result := Base("20180506_091537_DSC02122(+3.3).JPG", true) + assert.Equal(t, "20180506_091537_DSC02122", result) + }) + t.Run("20180506_091537_DSC02122(-2.7).JPG", func(t *testing.T) { + result := Base("20180506_091537_DSC02122(-2.7).JPG", true) + assert.Equal(t, "20180506_091537_DSC02122", result) + }) } func TestRelativeBase(t *testing.T) {