Skip to content

Commit

Permalink
Merge branch 'feature-#58---add-filter-on-directories-during-the-impo…
Browse files Browse the repository at this point in the history
…rt' into Fix-for-#69----Panic--runtime-error--slice-bounds-out-of-range---29--
  • Loading branch information
simulot committed Nov 17, 2023
2 parents 8df7277 + c62e7a0 commit bc6f066
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ builds:
- amd64
- arm
- arm64
flags:
- -trimpath
ldflags:
- '-s -w "-extldflags=-static" -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser'

Expand Down
32 changes: 25 additions & 7 deletions assets/files/localassets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package files
import (
"context"
"immich-go/assets"
"immich-go/helpers/fshelper"
"immich-go/immich/metadata"
"immich-go/logger"
"io/fs"
"path"
"path/filepath"
Expand All @@ -14,12 +16,14 @@ import (
type LocalAssetBrowser struct {
fsys fs.FS
albums map[string]string
log logger.Logger
}

func NewLocalFiles(ctx context.Context, fsys fs.FS) (*LocalAssetBrowser, error) {
func NewLocalFiles(ctx context.Context, fsys fs.FS, log logger.Logger) (*LocalAssetBrowser, error) {
return &LocalAssetBrowser{
fsys: fsys,
albums: map[string]string{},
log: log,
}, nil
}

Expand Down Expand Up @@ -54,6 +58,11 @@ func (la *LocalAssetBrowser) Browse(ctx context.Context) chan *assets.LocalAsset
return nil
}

ext := path.Ext(name)
if _, err := fshelper.MimeFromExt(strings.ToLower(ext)); err != nil {
return nil
}
la.log.Debug("file '%s'", name)
f := assets.LocalAssetFile{
FSys: la.fsys,
FileName: name,
Expand All @@ -76,12 +85,8 @@ func (la *LocalAssetBrowser) Browse(ctx context.Context) chan *assets.LocalAsset
f.DateTaken = time.Now()
}
}
_, err = fs.Stat(la.fsys, name+".xmp")
if err == nil {
f.SideCar = &metadata.SideCar{
FileName: name + ".xmp",
OnFSsys: true,
}
if !la.checkSidecar(&f, name+".xmp") {
la.checkSidecar(&f, strings.TrimSuffix(name, ext)+".xmp")
}
}
fileChan <- &f
Expand All @@ -104,6 +109,19 @@ func (la *LocalAssetBrowser) Browse(ctx context.Context) chan *assets.LocalAsset
return fileChan
}

func (la *LocalAssetBrowser) checkSidecar(f *assets.LocalAssetFile, name string) bool {
_, err := fs.Stat(la.fsys, name+".xmp")
if err == nil {
la.log.Debug(" found sidecar: '%s'", name)
f.SideCar = &metadata.SideCar{
FileName: name + ".xmp",
OnFSsys: true,
}
return true
}
return false
}

func (la *LocalAssetBrowser) addAlbum(dir string) {
base := path.Base(dir)
la.albums[dir] = base
Expand Down
3 changes: 2 additions & 1 deletion assets/files/localassets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"immich-go/assets/files"
"immich-go/logger"
"path"
"reflect"
"sort"
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestLocalAssets(t *testing.T) {
}
ctx := context.Background()

b, err := files.NewLocalFiles(ctx, fsys)
b, err := files.NewLocalFiles(ctx, fsys, logger.NoLogger{})
if err != nil {
t.Error(err)
}
Expand Down
10 changes: 9 additions & 1 deletion assets/gp/googlephotos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"immich-go/assets"
"immich-go/helpers/fshelper"
"immich-go/logger"
"io/fs"
"path"
"strings"
Expand All @@ -16,6 +17,7 @@ type Takeout struct {
filesByDir map[string][]fileKey // files name mapped by dir
jsonByYear map[jsonKey]*GoogleMetaData // JSON by year of capture and full path
albumsByDir map[string]assets.LocalAlbum // album title mapped by dir
log logger.Logger
}

type fileKey struct {
Expand All @@ -31,12 +33,13 @@ type Album struct {
Title string
}

func NewTakeout(ctx context.Context, fsys fs.FS) (*Takeout, error) {
func NewTakeout(ctx context.Context, fsys fs.FS, log logger.Logger) (*Takeout, error) {
to := Takeout{
fsys: fsys,
filesByDir: map[string][]fileKey{},
jsonByYear: map[jsonKey]*GoogleMetaData{},
albumsByDir: map[string]assets.LocalAlbum{},
log: log,
}
err := to.walk(ctx, fsys)

Expand All @@ -49,6 +52,7 @@ func (to *Takeout) walk(ctx context.Context, fsys fs.FS) error {
if err != nil {
return err
}
to.log.Debug("walk file '%s'", name)
select {
case <-ctx.Done():
// Check if the context has been cancelled
Expand Down Expand Up @@ -116,17 +120,21 @@ func (to *Takeout) Browse(ctx context.Context) chan *assets.LocalAssetFile {
go func() {
defer close(c)
for k, md := range to.jsonByYear {
to.log.Debug("Checking '%s', %d", k.name, k.year)
assets := to.jsonAssets(k, md)
for _, a := range assets {
fk := fileKey{name: path.Base(a.FileName), size: int64(a.FileSize)}
if _, exist := passed[fk]; !exist {
to.log.Debug(" associated with '%s'", fk.name)
passed[fk] = nil
select {
case <-ctx.Done():
return
default:
c <- a
}
} else {
to.log.Debug(" associated with '%s', but already seen", fk.name)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion assets/gp/testgp_bigread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gp
import (
"context"
"immich-go/helpers/fshelper"
"immich-go/logger"
"path/filepath"
"testing"
)
Expand All @@ -19,7 +20,7 @@ func TestReadBigTakeout(t *testing.T) {
cnt := 0
fsyss, err := fshelper.ParsePath(m, true)
for _, fsys := range fsyss {
to, err := NewTakeout(context.Background(), fsys)
to, err := NewTakeout(context.Background(), fsys, logger.NoLogger{})
if err != nil {
t.Error(err)
return
Expand Down
5 changes: 3 additions & 2 deletions assets/gp/testgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gp

import (
"context"
"immich-go/logger"
"path"
"reflect"
"testing"
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestBrowse(t *testing.T) {
}
ctx := context.Background()

b, err := NewTakeout(ctx, fsys)
b, err := NewTakeout(ctx, fsys, logger.NoLogger{})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -160,7 +161,7 @@ func TestAlbums(t *testing.T) {
t.Error(fsys.err)
return
}
b, err := NewTakeout(ctx, fsys)
b, err := NewTakeout(ctx, fsys, logger.NoLogger{})
if err != nil {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmdduplicate/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"flag"
"immich-go/helpers/gen"
"immich-go/immich"
"immich-go/immich/logger"
"immich-go/logger"
"immich-go/ui"
"path"
"sort"
Expand All @@ -18,7 +18,7 @@ import (
)

type DuplicateCmd struct {
logger *logger.Logger
logger *logger.Log
Immich *immich.ImmichClient // Immich client

AssumeYes bool // When true, doesn't ask to the user
Expand All @@ -34,7 +34,7 @@ type duplicateKey struct {
Name string
}

func NewDuplicateCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Logger, args []string) (*DuplicateCmd, error) {
func NewDuplicateCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Log, args []string) (*DuplicateCmd, error) {
cmd := flag.NewFlagSet("duplicate", flag.ExitOnError)
validRange := immich.DateRange{}
validRange.Set("1850-01-04,2030-01-01")
Expand All @@ -57,7 +57,7 @@ func NewDuplicateCmd(ctx context.Context, ic *immich.ImmichClient, logger *logge
return &app, err
}

func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) error {
func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := NewDuplicateCmd(ctx, ic, log, args)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmdmetadata/metadatacmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"flag"
"immich-go/helpers/docker"
"immich-go/immich"
"immich-go/immich/logger"
"immich-go/immich/metadata"
"immich-go/logger"
"math"
"path"
"strings"
Expand All @@ -15,14 +15,14 @@ import (

type MetadataCmd struct {
Immich *immich.ImmichClient // Immich client
Log *logger.Logger
Log *logger.Log
DryRun bool
MissingDateDespiteName bool
MissingDate bool
DockerHost string
}

func NewMetadataCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Logger, args []string) (*MetadataCmd, error) {
func NewMetadataCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Log, args []string) (*MetadataCmd, error) {
var err error
cmd := flag.NewFlagSet("metadata", flag.ExitOnError)
app := MetadataCmd{
Expand All @@ -38,7 +38,7 @@ func NewMetadataCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger
return &app, err
}

func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) error {
func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := NewMetadataCmd(ctx, ic, log, args)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmdstack/cmdstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"flag"
"immich-go/helpers/stacking"
"immich-go/immich"
"immich-go/immich/logger"
"immich-go/logger"
"immich-go/ui"
"path"
"sort"
Expand All @@ -14,13 +14,13 @@ import (

type StackCmd struct {
Immich *immich.ImmichClient // Immich client
logger *logger.Logger
logger *logger.Log

AssumeYes bool
DateRange immich.DateRange // Set capture date range
}

func initSack(xtx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) (*StackCmd, error) {
func initSack(xtx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) (*StackCmd, error) {
cmd := flag.NewFlagSet("stack", flag.ExitOnError)
validRange := immich.DateRange{}

Expand All @@ -40,7 +40,7 @@ func initSack(xtx context.Context, ic *immich.ImmichClient, log *logger.Logger,
err := cmd.Parse(args)
return &app, err
}
func NewStackCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) error {
func NewStackCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := initSack(ctx, ic, log, args)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmdtool/cmdalbum/cmdalbum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"flag"
"fmt"
"immich-go/immich"
"immich-go/immich/logger"
"immich-go/logger"
"immich-go/ui"
"regexp"
"sort"
"strconv"
)

func AlbumCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) error {
func AlbumCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
if len(args) > 0 {
cmd := args[0]
args = args[1:]
Expand All @@ -26,13 +26,13 @@ func AlbumCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Logg
}

type DeleteAlbumCmd struct {
log *logger.Logger
log *logger.Log
Immich *immich.ImmichClient // Immich client
pattern *regexp.Regexp // album pattern
AssumeYes bool
}

func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Logger, args []string) error {
func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app := &DeleteAlbumCmd{
log: log,
Immich: ic,
Expand Down
4 changes: 2 additions & 2 deletions cmdtool/cmdtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"immich-go/cmdtool/cmdalbum"
"immich-go/immich"
"immich-go/immich/logger"
"immich-go/logger"
)

func CommandTool(ctx context.Context, ic *immich.ImmichClient, logger *logger.Logger, args []string) error {
func CommandTool(ctx context.Context, ic *immich.ImmichClient, logger *logger.Log, args []string) error {
if len(args) > 0 {
cmd := args[0]
args = args[1:]
Expand Down
Loading

0 comments on commit bc6f066

Please sign in to comment.