Skip to content

Commit

Permalink
Backend: Add a 'search' CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
seeschloss authored and lastzero committed Jun 12, 2023
1 parent 9e086c7 commit a891da7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var PhotoPrism = []cli.Command{
ShowCommand,
VersionCommand,
ShowConfigCommand,
SearchCommand,
ConnectCommand,
}

Expand Down
56 changes: 56 additions & 0 deletions internal/commands/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commands

import (
"context"
"time"
"strings"

"fmt"

"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/search"
"github.com/photoprism/photoprism/internal/entity"
)

// SearchCommand registers the search cli command.
var SearchCommand = cli.Command{
Name: "search",
Usage: "Searches in library using filters",
ArgsUsage: "search-query",
Action: searchAction,
}

// searchAction searches all photos in library
func searchAction(ctx *cli.Context) error {
start := time.Now()

conf, err := InitConfig(ctx)

_, cancel := context.WithCancel(context.Background())
defer cancel()

if err != nil {
return err
}

conf.InitDb()
defer conf.Shutdown()

form := form.SearchPhotos{Query: strings.TrimSpace(ctx.Args().First()), Primary: false, Merged: false}
photos, _, err := search.Photos(form)

for _, photo := range photos {
p := entity.Photo{ID: photo.ID}
p.PreloadFiles()
for _, file := range p.Files {
fmt.Printf("%s\n", file.FileName)
}
}

elapsed := time.Since(start)

log.Infof("searched in %s", elapsed)

return nil
}

0 comments on commit a891da7

Please sign in to comment.