Skip to content

Commit

Permalink
fix: better handling of long file name on google photos
Browse files Browse the repository at this point in the history
  • Loading branch information
laaqxdze1k committed Aug 21, 2023
1 parent 1edf2e5 commit a82a37d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions immich/assets/googlephotos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assets

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io/fs"
Expand All @@ -12,6 +13,8 @@ import (
"strconv"
"strings"
"time"
"unicode/utf16"
"unsafe"
)

type NameResolver interface {
Expand Down Expand Up @@ -79,7 +82,6 @@ func (fsys *GooglePhotosAssetBrowser) Browse(ctx context.Context) chan *LocalAss
if path.Base(dir) == "Failed Videos" {
return nil
}
// base := strings.TrimSuffix(md.Title, ext)

f := LocalAssetFile{
FSys: fsys,
Expand Down Expand Up @@ -128,10 +130,13 @@ func (fsys *GooglePhotosAssetBrowser) ResolveName(la *LocalAssetFile) (string, e
ext := path.Ext(la.Title)
base := strings.TrimSuffix(la.Title, ext)
dir := path.Dir(la.FileName)
if len(base) >= 46 {
base = base[:46]
baseUnicode := utf16.Encode([]rune(base))

if len(baseUnicode) > 46 {
baseUnicode = baseUnicode[:46]
base = string(utf16.Decode(baseUnicode))
}
pattern := nameReplacer.Replace(base) + ".*"
pattern := nameReplacer.Replace(base) + "*.*"

matches, err := fs.Glob(fsys, filepath.Join(dir, pattern))
if err != nil {
Expand All @@ -150,9 +155,12 @@ func (fsys *GooglePhotosAssetBrowser) ResolveName(la *LocalAssetFile) (string, e
return "", fmt.Errorf("can't find the image with title %q, pattern: %q: %w", la.Title, pattern, os.ErrNotExist)
}

var nameReplacer = strings.NewReplacer(" ", "?", "/", "?", ":", "?")
func hexPrint(s string, u []uint16) {
fmt.Println(s, len(s))
fmt.Println(hex.EncodeToString(*(*[]byte)(unsafe.Pointer(&u))))
}

var gp = regexp.MustCompile(`Photos from \d{4}`)
var nameReplacer = strings.NewReplacer(" ", "?", "/", "?", ":", "?")
var commaAlbum = regexp.MustCompile(`^,\s+`)

type googleMetaData struct {
Expand Down
6 changes: 4 additions & 2 deletions upcmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func UploadCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log
if err != nil {
return err
}
log.MessageTerminate(logger.OK, "%d received", len(list))
log.MessageTerminate(logger.OK, " %d received", len(list))

app.AssetIndex = &AssetIndex{
assets: list,
Expand Down Expand Up @@ -398,7 +398,9 @@ func (app *UpCmd) ManageAlbums(ctx context.Context) error {
app.logger.Warning("%s: %s", r.ID, r.Error)
}
}
app.logger.OK("%d asset(s) added to the album %q", added, album)
if added > 0 {
app.logger.OK("%d asset(s) added to the album %q", added, album)
}
} else {
app.logger.OK("Update album %s skipped - dry run mode", album)
}
Expand Down

0 comments on commit a82a37d

Please sign in to comment.