Skip to content

Commit

Permalink
immich-go crashes while importing a Google Takeout export (#367)
Browse files Browse the repository at this point in the history
* immich-go crashes while importing a Google Takeout export
Fixes #364

* Merge branch 'main' into simulot/issue364

* Photos named as a timestamp should be parsed as UTC
Fixes #358

* fiw error handling
  • Loading branch information
simulot committed Jul 11, 2024
1 parent edb9eb4 commit 8ceb306
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
27 changes: 20 additions & 7 deletions browser/gp/googlephotos.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,30 @@ func (to *Takeout) passTwo(ctx context.Context, dir string, assetChan chan *brow

for _, base := range gen.MapKeys(linkedFiles) {
var a *browser.LocalAssetFile
var err error

linked := linkedFiles[base]

if linked.image.md != nil {
a = to.googleMDToAsset(linked.image.md, linked.image.fsys, path.Join(dir, linked.image.base))
a, err = to.googleMDToAsset(linked.image.md, linked.image.fsys, path.Join(dir, linked.image.base))
if err != nil {
to.log.Record(ctx, fileevent.Error, nil, path.Join(dir, linked.image.base), "error", err.Error())
continue
}
if linked.video.md != nil {
i := to.googleMDToAsset(linked.video.md, linked.video.fsys, path.Join(dir, linked.video.base))
a.LivePhoto = i
i, err := to.googleMDToAsset(linked.video.md, linked.video.fsys, path.Join(dir, linked.video.base))
if err != nil {
to.log.Record(ctx, fileevent.Error, nil, path.Join(dir, linked.video.base), "error", err.Error())
} else {
a.LivePhoto = i
}
}
} else {
a = to.googleMDToAsset(linked.video.md, linked.video.fsys, path.Join(dir, linked.video.base))
a, err = to.googleMDToAsset(linked.video.md, linked.video.fsys, path.Join(dir, linked.video.base))
if err != nil {
to.log.Record(ctx, fileevent.Error, nil, path.Join(dir, linked.video.base), "error", err.Error())
continue
}
}

select {
Expand All @@ -514,7 +527,7 @@ func (to *Takeout) passTwo(ctx context.Context, dir string, assetChan chan *brow
}

// googleMDToAsset makes a localAssetFile based on the google metadata
func (to *Takeout) googleMDToAsset(md *GoogleMetaData, fsys fs.FS, name string) *browser.LocalAssetFile {
func (to *Takeout) googleMDToAsset(md *GoogleMetaData, fsys fs.FS, name string) (*browser.LocalAssetFile, error) {
// Change file's title with the asset's title and the actual file's extension
title := md.Title
titleExt := path.Ext(title)
Expand All @@ -529,7 +542,7 @@ func (to *Takeout) googleMDToAsset(md *GoogleMetaData, fsys fs.FS, name string)

i, err := fs.Stat(fsys, name)
if err != nil {
return nil
return nil, err
}
a := browser.LocalAssetFile{
FileName: name,
Expand Down Expand Up @@ -568,5 +581,5 @@ func (to *Takeout) googleMDToAsset(md *GoogleMetaData, fsys fs.FS, name string)
}

a.Metadata = sidecar
return &a
return &a, nil
}
36 changes: 18 additions & 18 deletions immich/metadata/namesdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,51 @@ func TestTakeTimeFromPath(t *testing.T) {
},
{
name: "A/B/2022/2022.11/2022.11.09/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, local),
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, time.UTC),
},
{
name: "A/B/2022/2022.11/IMG_1234.HEIC",
expected: time.Time{},
},
{
name: "A/B/2022.11.09/2022.11/2022/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, local),
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, time.UTC),
},
{
name: "2024-05-05.png",
expected: time.Date(2024, 5, 5, 0, 0, 0, 0, local),
expected: time.Date(2024, 5, 5, 0, 0, 0, 0, time.UTC),
},
{
name: "PXL_20220909_154515546.TS.mp4",
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, local),
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, time.UTC),
},
{
name: "Screenshot from 2022-12-17 19-45-43.png",
expected: time.Date(2022, 12, 17, 19, 45, 43, 0, local),
expected: time.Date(2022, 12, 17, 19, 45, 43, 0, time.UTC),
},
{
name: "Bebop2_20180719194940+0200.mp4", // It's local time anyway, so ignore +0200 part
expected: time.Date(2018, 0o7, 19, 19, 49, 40, 0, local),
expected: time.Date(2018, 0o7, 19, 19, 49, 40, 0, time.UTC),
},
{
name: "AR_EFFECT_20141126193511.mp4",
expected: time.Date(2014, 11, 26, 19, 35, 11, 0, local),
expected: time.Date(2014, 11, 26, 19, 35, 11, 0, time.UTC),
},
{
name: "2023-07-20 14:15:30", // Format: YYYY-MM-DD HH:MM:SS
expected: time.Date(2023, 7, 20, 14, 15, 30, 0, local),
expected: time.Date(2023, 7, 20, 14, 15, 30, 0, time.UTC),
},
{
name: "20001010120000", // Format: YYYYMMDDHHMMSS
expected: time.Date(2000, 10, 10, 12, 0, 0, 0, local),
expected: time.Date(2000, 10, 10, 12, 0, 0, 0, time.UTC),
},
{
name: "2023_07_20_10_09_20.mp4",
expected: time.Date(2023, 0o7, 20, 10, 9, 20, 0, local),
expected: time.Date(2023, 0o7, 20, 10, 9, 20, 0, time.UTC),
},
{
name: "19991231",
expected: time.Date(1999, 12, 31, 0, 0, 0, 0, local),
expected: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC),
},
{
name: "991231-125200",
Expand All @@ -76,35 +76,35 @@ func TestTakeTimeFromPath(t *testing.T) {
},
{
name: "00015IMG_00015_BURST20171111030039_COVER.jpg",
expected: time.Date(2017, 11, 11, 3, 0, 39, 0, local),
expected: time.Date(2017, 11, 11, 3, 0, 39, 0, time.UTC),
},
{
name: "PXL_20220909_154515546.TS.mp4",
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, local),
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, time.UTC),
},
{
name: "IMG_1234.HEIC",
expected: time.Time{},
},
{
name: "20221109/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, local),
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, time.UTC),
},
{
name: "20221109T2030/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 20, 30, 0, 0, local),
expected: time.Date(2022, 11, 9, 20, 30, 0, 0, time.UTC),
},
{
name: "2022.11.09T20.30/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 20, 30, 0, 0, local),
expected: time.Date(2022, 11, 9, 20, 30, 0, 0, time.UTC),
},
{
name: "2022/11/09/IMG_1234.HEIC",
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, local),
expected: time.Date(2022, 11, 9, 0, 0, 0, 0, time.UTC),
},
{
name: "something_2011-05-11 something/IMG_1234.JPG",
expected: time.Date(2011, 0o5, 11, 0, 0, 0, 0, local),