Backend: Add a 'search' CLI command
This commit is contained in:
parent
9e086c7ebd
commit
a891da7dec
2 changed files with 57 additions and 0 deletions
|
@ -65,6 +65,7 @@ var PhotoPrism = []cli.Command{
|
|||
ShowCommand,
|
||||
VersionCommand,
|
||||
ShowConfigCommand,
|
||||
SearchCommand,
|
||||
ConnectCommand,
|
||||
}
|
||||
|
||||
|
|
56
internal/commands/search.go
Normal file
56
internal/commands/search.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue