diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/service/gallery_service.go | 24 | ||||
| -rw-r--r-- | internal/service/load.go | 33 | ||||
| -rw-r--r-- | internal/service/service.go | 4 |
3 files changed, 44 insertions, 17 deletions
diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index 1145bb7..e500bec 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -2,32 +2,22 @@ package service import ( "context" + "errors" "github.com/RanXom/galleryd/internal/gallery" ) // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { - files, err := s.scanner.Scan(ctx) - if err != nil { - return nil, err + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: } - photos, err := s.builder.Build(ctx, files) - if err != nil { - return nil, err + if s.index.byID == nil { + return nil, errors.New("gallery not loaded") } - index := galleryIndex{ - photos: photos, - byID: make(map[string]gallery.Photo, len(photos)), - } - - for _, photo := range photos { - index.byID[photo.ID] = photo - } - - s.index = index - return s.index.photos, nil } diff --git a/internal/service/load.go b/internal/service/load.go new file mode 100644 index 0000000..09bbc20 --- /dev/null +++ b/internal/service/load.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" +) + +// Load scans the configured roots and rebuilds the in-memory gallery. +func (s *galleryService) Load(ctx context.Context) error { + files, err := s.scanner.Scan(ctx) + if err != nil { + return err + } + + photos, err := s.builder.Build(ctx, files) + if err != nil { + return err + } + + index := galleryIndex{ + photos: photos, + byID: make(map[string]gallery.Photo, len(photos)), + } + + for _, photo := range photos { + index.byID[photo.ID] = photo + } + + s.index = index + + return nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 6183e26..252abed 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,5 +8,9 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { + // Load scans the configured roots and builds the in-memory gallery. + Load(ctx context.Context) error + + // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) } |