-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Fix--CR3-date-of-catpture-TZ-error-#56' into implement-…
…-stack-jpg+raw-wen-uploading-photos-#54
- Loading branch information
Showing
4 changed files
with
261 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package metadata | ||
|
||
import ( | ||
"immich-go/helpers/tzone" | ||
"os" | ||
"path" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func mustParse(s string) time.Time { | ||
local, err := tzone.Local() | ||
if err != nil { | ||
panic(err) | ||
} | ||
t, err := time.ParseInLocation("2006:01:02 15:04:05-07:00", s, local) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} | ||
|
||
func TestGetFromReader(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
filename string | ||
want time.Time | ||
}{ | ||
{ | ||
name: "cr3", | ||
filename: "../../../test-data/burst/Reflex/3H2A0018.CR3", | ||
want: mustParse("2023:06:23 13:32:52+02:00"), | ||
}, | ||
{ | ||
name: "jpg", | ||
filename: "../../../test-data/burst/Reflex/3H2A0018.JPG", | ||
want: mustParse("2023:06:23 13:32:52+02:00"), | ||
}, | ||
{ | ||
name: "jpg", | ||
filename: "../../../test-data/burst/PXL6/PXL_20231029_062723981.jpg", | ||
want: mustParse("2023:10:29 07:27:23+01:00"), | ||
}, | ||
{ | ||
name: "dng", | ||
filename: "../../../test-data/burst/PXL6/PXL_20231029_062723981.dng", | ||
want: mustParse("2023:10:29 07:27:24+01:00"), | ||
}, | ||
{ | ||
name: "cr2", | ||
filename: "../../../test-data/burst/IMG_4879.CR2", | ||
want: mustParse("2023:02:24 18:59:09+01:00"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r, err := os.Open(tt.filename) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
defer r.Close() | ||
ext := path.Ext(tt.filename) | ||
got, err := GetFromReader(r, ext) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !tt.want.Equal(got.DateTaken) { | ||
t.Errorf("GetFromReader() = %v, want %v", got.DateTaken, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package metadata | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"immich-go/helpers/tzone" | ||
"io" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rwcarlsen/goexif/exif" | ||
) | ||
|
||
func getExifFromReader(r io.Reader) (MetaData, error) { | ||
var md MetaData | ||
local, err := tzone.Local() | ||
if err != nil { | ||
return md, err | ||
} | ||
// Decode the EXIF data | ||
x, err := exif.Decode(r) | ||
if err != nil && exif.IsCriticalError(err) { | ||
if errors.Is(err, io.EOF) { | ||
return md, nil | ||
} | ||
return md, fmt.Errorf("can't get DateTaken: %w", err) | ||
} | ||
|
||
tag, err := getTagSting(x, exif.GPSDateStamp) | ||
if err == nil { | ||
md.DateTaken, err = time.ParseInLocation("2006:01:02 15:04:05Z", tag, local) | ||
} | ||
if err != nil { | ||
tag, err = getTagSting(x, exif.DateTimeOriginal) | ||
if err == nil { | ||
md.DateTaken, err = time.ParseInLocation("2006:01:02 15:04:05", tag, local) | ||
} | ||
} | ||
if err != nil { | ||
tag, err = getTagSting(x, exif.DateTime) | ||
if err == nil { | ||
md.DateTaken, err = time.ParseInLocation("2006:01:02 15:04:05", tag, local) | ||
} | ||
} | ||
|
||
return md, err | ||
} | ||
|
||
func getTagSting(x *exif.Exif, tagName exif.FieldName) (string, error) { | ||
t, err := x.Get(tagName) | ||
if err != nil { | ||
return "", err | ||
} | ||
s := strings.TrimRight(strings.TrimLeft(t.String(), `"`), `"`) | ||
return s, nil | ||
} |
Oops, something went wrong.