Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): Add more linter to the codebase #147

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ run:
issues:
max-issues-per-linter: 100
max-same-issues: 100
exclude:
- "Added to album: "

linters-settings:
gocritic:
Expand Down Expand Up @@ -48,3 +50,13 @@ linters:
- mirror
- nakedret
- gofumpt
- stylecheck
- unused
- goconst
- makezero
- unparam
- prealloc
- predeclared
- unconvert
- unparam
- whitespace
23 changes: 9 additions & 14 deletions browser/files/localassets.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,26 @@ func (la *LocalAssetBrowser) handleFolder(ctx context.Context, fsys fs.FS, fileC
continue
}
fileName := path.Join(folder, e.Name())
la.log.AddEntry(fileName, logger.DISCOVERED_FILE, "")
la.log.AddEntry(fileName, logger.DiscoveredFile, "")
name := e.Name()
ext := strings.ToLower(path.Ext(name))
if fshelper.IsMetadataExt(ext) {
la.log.AddEntry(name, logger.METADATA, "")
la.log.AddEntry(name, logger.Metadata, "")
continue
} else if fshelper.IsIgnoredExt(ext) {
la.log.AddEntry(fileName, logger.UNSUPPORTED, "")
la.log.AddEntry(fileName, logger.Unsupported, "")
continue
}
m, err := fshelper.MimeFromExt(strings.ToLower(ext))
if err != nil {
la.log.AddEntry(fileName, logger.UNSUPPORTED, "")
la.log.AddEntry(fileName, logger.Unsupported, "")
continue
}
ss := strings.Split(m[0], "/")
if ss[0] == "image" {
la.log.AddEntry(name, logger.SCANNED_IMAGE, "")
la.log.AddEntry(name, logger.ScannedImage, "")
} else {
la.log.AddEntry(name, logger.SCANNED_VIDEO, "")
la.log.AddEntry(name, logger.ScannedVideo, "")
}

f := browser.LocalAssetFile{
Expand All @@ -139,7 +139,7 @@ func (la *LocalAssetBrowser) handleFolder(ctx context.Context, fsys fs.FS, fileC
f.DateTaken = time.Now()
}
}
la.checkSidecar(fsys, &f, entries, folder, name)
la.checkSidecar(&f, entries, folder, name)
}
// Check if the context has been cancelled
select {
Expand All @@ -153,7 +153,7 @@ func (la *LocalAssetBrowser) handleFolder(ctx context.Context, fsys fs.FS, fileC
return nil
}

func (la *LocalAssetBrowser) checkSidecar(fsys fs.FS, f *browser.LocalAssetFile, entries []fs.DirEntry, dir, name string) bool {
func (la *LocalAssetBrowser) checkSidecar(f *browser.LocalAssetFile, entries []fs.DirEntry, dir, name string) bool {
assetBase := baseNames(name)

for _, name := range assetBase {
Expand All @@ -168,7 +168,7 @@ func (la *LocalAssetBrowser) checkSidecar(fsys fs.FS, f *browser.LocalAssetFile,
FileName: path.Join(dir, e.Name()),
OnFSsys: true,
}
la.log.AddEntry(name, logger.ASSOCIATED_META, "")
la.log.AddEntry(name, logger.AssociatedMetadata, "")
return true
}
}
Expand Down Expand Up @@ -210,11 +210,6 @@ func escapeName(n string) string {
return b.String()
}

func (la *LocalAssetBrowser) addAlbum(dir string) {
base := path.Base(dir)
la.albums[dir] = base
}

func (la *LocalAssetBrowser) ReadMetadataFromFile(a *browser.LocalAssetFile) error {
ext := strings.ToLower(path.Ext(a.FileName))

Expand Down
37 changes: 18 additions & 19 deletions browser/gp/googlephotos.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewTakeout(ctx context.Context, jnl *logger.Journal, fsyss ...fs.FS) (*Take
return nil, err
}

to.solvePuzzle(ctx)
to.solvePuzzle()
return &to, err
}

Expand Down Expand Up @@ -106,13 +106,13 @@ func (to *Takeout) passOneFsWalk(ctx context.Context, w fs.FS) error {
return nil
}

to.jnl.AddEntry(name, logger.DISCOVERED_FILE, "")
to.jnl.AddEntry(name, logger.DiscoveredFile, "")
dir, base := path.Split(name)
dir = strings.TrimSuffix(dir, "/")
ext := strings.ToLower(path.Ext(base))

if slices.Contains(uselessFiles, base) {
to.jnl.AddEntry(name, logger.DISCARDED, "Useless file")
to.jnl.AddEntry(name, logger.Discarded, "Useless file")
return nil
}

Expand All @@ -130,44 +130,44 @@ func (to *Takeout) passOneFsWalk(ctx context.Context, w fs.FS) error {
if err == nil {
switch {
case md.isAsset():
to.addJson(w, dir, base, md)
to.jnl.AddEntry(name, logger.METADATA, "Asset Title: "+md.Title)
to.addJSON(dir, base, md)
to.jnl.AddEntry(name, logger.Metadata, "Asset Title: "+md.Title)
case md.isAlbum():
to.albums[dir] = md.Title
to.jnl.AddEntry(name, logger.METADATA, "Album title: "+md.Title)
to.jnl.AddEntry(name, logger.Metadata, "Album title: "+md.Title)
default:
to.jnl.AddEntry(name, logger.DISCARDED, "Unknown json file")
to.jnl.AddEntry(name, logger.Discarded, "Unknown json file")
return nil
}
} else {
to.jnl.AddEntry(name, logger.DISCARDED, "Unknown json file")
to.jnl.AddEntry(name, logger.Discarded, "Unknown json file")
return nil
}
default:

if fshelper.IsIgnoredExt(ext) {
to.jnl.AddEntry(name, logger.DISCARDED, "File ignored")
to.jnl.AddEntry(name, logger.Discarded, "File ignored")
return nil
}

m, err := fshelper.MimeFromExt(ext)
if err != nil {
to.jnl.AddEntry(name, logger.UNSUPPORTED, "")
to.jnl.AddEntry(name, logger.Unsupported, "")
return nil
}

if strings.Contains(name, "Failed Videos") {
to.jnl.AddEntry(name, logger.FAILED_VIDEO, "")
to.jnl.AddEntry(name, logger.FailedVideo, "")
return nil
}
dirCatalog.files[base] = fileInfo{
length: int(finfo.Size()),
}
ss := strings.Split(m[0], "/")
if ss[0] == "image" {
to.jnl.AddEntry(name, logger.SCANNED_IMAGE, "")
to.jnl.AddEntry(name, logger.ScannedImage, "")
} else {
to.jnl.AddEntry(name, logger.SCANNED_VIDEO, "")
to.jnl.AddEntry(name, logger.ScannedVideo, "")
}
}
to.catalogs[w][dir] = dirCatalog
Expand All @@ -177,8 +177,8 @@ func (to *Takeout) passOneFsWalk(ctx context.Context, w fs.FS) error {
return err
}

// addJson stores metadata and all paths where the combo base+year has been found
func (to *Takeout) addJson(w fs.FS, dir, base string, md *GoogleMetaData) {
// addJSON stores metadata and all paths where the combo base+year has been found
func (to *Takeout) addJSON(dir, base string, md *GoogleMetaData) {
k := jsonKey{
name: base,
year: md.PhotoTakenTime.Time().Year(),
Expand Down Expand Up @@ -227,7 +227,7 @@ var matchers = []matcherFn{
// The duplicates files (same name, same length in bytes) found in the local source are discarded before been presented to the immich server.
//

func (to *Takeout) solvePuzzle(ctx context.Context) error {
func (to *Takeout) solvePuzzle() {
to.jnl.OK("Associating JSON and assets...")
jsonKeys := gen.MapKeys(to.jsonByYear)
sort.Slice(jsonKeys, func(i, j int) bool {
Expand Down Expand Up @@ -259,7 +259,7 @@ func (to *Takeout) solvePuzzle(ctx context.Context) error {
for f := range l.files {
if l.files[f].md == nil {
if matcher(k.name, f) {
to.jnl.AddEntry(path.Join(d, f), logger.ASSOCIATED_META, fmt.Sprintf("%s (%d)", k.name, k.year))
to.jnl.AddEntry(path.Join(d, f), logger.AssociatedMetadata, fmt.Sprintf("%s (%d)", k.name, k.year))
// if not already matched
i := l.files[f]
i.md = md
Expand All @@ -272,7 +272,6 @@ func (to *Takeout) solvePuzzle(ctx context.Context) error {
}
}
}
return nil
}

// normalMatch
Expand Down Expand Up @@ -460,7 +459,7 @@ func (to *Takeout) passTwoWalk(ctx context.Context, w fs.FS, assetChan chan *bro
year: f.md.PhotoTakenTime.Time().Year(),
}
if _, exists := to.uploaded[key]; exists {
to.jnl.AddEntry(name, logger.LOCAL_DUPLICATE, "")
to.jnl.AddEntry(name, logger.LocalDuplicate, "")
return nil
}
a := to.googleMDToAsset(f.md, key, w, name)
Expand Down
4 changes: 2 additions & 2 deletions browser/gp/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (gmd GoogleMetaData) isPartner() bool {

// Key return an expected unique key for the asset
// based on the title and the timestamp
func (md GoogleMetaData) Key() string {
return fmt.Sprintf("%s,%s", md.Title, md.PhotoTakenTime.Timestamp)
func (gmd GoogleMetaData) Key() string {
return fmt.Sprintf("%s,%s", gmd.Title, gmd.PhotoTakenTime.Timestamp)
}

// googIsPresent is set when the field is present. The content of the field is not relevant
Expand Down
8 changes: 1 addition & 7 deletions browser/gp/testgp_samples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ func takenTime(date string) func(md *GoogleMetaData) {
}
}

func descriptionField(description string) jsonFn {
return func(md *GoogleMetaData) {
md.Description = description
}
}

func (mfs *inMemFS) addJSONImage(name string, title string, modifiers ...jsonFn) *inMemFS {
md := GoogleMetaData{
Title: title,
Expand All @@ -73,7 +67,7 @@ func (mfs *inMemFS) addJSONImage(name string, title string, modifiers ...jsonFn)
if err != nil {
panic(err)
}
mfs.addFile(name, []byte(content.Bytes()))
mfs.addFile(name, content.Bytes())
return mfs
}

Expand Down
39 changes: 0 additions & 39 deletions browser/readersearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,3 @@ func searchPattern(r io.Reader, pattern []byte, maxDataLen int) ([]byte, error)

return nil, io.EOF
}

func seekReaderAtPattern(r io.Reader, pattern []byte) (io.Reader, error) {
var err error
pos := 0
// Create a buffer to hold the chunk of dataZ
buffer := make([]byte, searchBufferSize)
ofs := 0

var bytesRead int
for {
// Read a chunk of data into the buffer
bytesRead, err = r.Read(buffer[bytesRead-ofs:])
if err != nil && err != io.EOF {
return nil, err
}

// Search for the pattern within the buffer
index := bytes.Index(buffer, pattern)
if index >= 0 {
if index < searchBufferSize-len(pattern) {
return io.MultiReader(bytes.NewReader(buffer[index:]), r), nil
}
ofs = index
} else {
ofs = bytesRead - len(pattern) - 1
}

// Check if end of file is reached
if err == io.EOF {
break
}

// Move the remaining bytes of the current buffer to the beginning
copy(buffer, buffer[ofs:bytesRead])
pos += bytesRead
}

return nil, io.EOF
}
8 changes: 4 additions & 4 deletions cmdduplicate/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DuplicateCmd struct {
DateRange immich.DateRange // Set capture date range
IgnoreTZErrors bool // Enable TZ error tolerance

assetsById map[string]*immich.Asset
assetsByID map[string]*immich.Asset
assetsByBaseAndDate map[duplicateKey][]*immich.Asset
}

Expand All @@ -43,7 +43,7 @@ func NewDuplicateCmd(ctx context.Context, ic *immich.ImmichClient, logger *logge
logger: logger,
Immich: ic,
DateRange: validRange,
assetsById: map[string]*immich.Asset{},
assetsByID: map[string]*immich.Asset{},
assetsByBaseAndDate: map[duplicateKey][]*immich.Asset{},
}

Expand All @@ -69,7 +69,7 @@ func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.
if !app.DateRange.InRange(a.ExifInfo.DateTimeOriginal.Time) {
return
}
app.assetsById[a.ID] = a
app.assetsByID[a.ID] = a
d := a.ExifInfo.DateTimeOriginal.Time.Round(time.Minute)
if app.IgnoreTZErrors {
d = time.Date(d.Year(), d.Month(), d.Day(), 0, d.Minute(), d.Second(), 0, time.UTC)
Expand All @@ -88,7 +88,7 @@ func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.
if err != nil {
return err
}
log.MessageTerminate(logger.OK, "%d received", len(app.assetsById))
log.MessageTerminate(logger.OK, "%d received", len(app.assetsByID))
log.MessageTerminate(logger.OK, "%d duplicate(s) determined.", dupCount)

keys := gen.MapFilterKeys(app.assetsByBaseAndDate, func(i []*immich.Asset) bool {
Expand Down
4 changes: 2 additions & 2 deletions cmdstack/cmdstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type StackCmd struct {
DateRange immich.DateRange // Set capture date range
}

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

Expand All @@ -43,7 +43,7 @@ func initSack(xtx context.Context, ic *immich.ImmichClient, log *logger.Log, arg
}

func NewStackCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := initSack(ctx, ic, log, args)
app, err := initSack(ic, log, args)
if err != nil {
return err
}
Expand Down