2020-06-22 21:52:53 +02:00
|
|
|
package scanner
|
|
|
|
|
2020-06-23 00:40:47 +02:00
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
)
|
2020-06-22 21:52:53 +02:00
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
type AlbumScannerCache struct {
|
|
|
|
path_contains_photos map[string]bool
|
|
|
|
photo_types map[string]ImageType
|
2020-06-23 00:40:47 +02:00
|
|
|
mutex sync.Mutex
|
2020-06-22 21:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
func MakeAlbumCache() *AlbumScannerCache {
|
|
|
|
return &AlbumScannerCache{
|
|
|
|
path_contains_photos: make(map[string]bool),
|
|
|
|
photo_types: make(map[string]ImageType),
|
2020-06-22 21:52:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert single album directory in cache
|
2020-06-22 23:52:41 +02:00
|
|
|
func (c *AlbumScannerCache) InsertAlbumPath(path string, contains_photo bool) {
|
2020-06-23 00:40:47 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2020-06-22 23:52:41 +02:00
|
|
|
c.path_contains_photos[path] = contains_photo
|
2020-06-22 21:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert album path and all parent directories up to the given root directory in cache
|
2020-06-22 23:52:41 +02:00
|
|
|
func (c *AlbumScannerCache) InsertAlbumPaths(end_path string, root string, contains_photo bool) {
|
2020-06-22 21:52:53 +02:00
|
|
|
curr_path := path.Clean(end_path)
|
|
|
|
root_path := path.Clean(root)
|
|
|
|
|
2020-06-23 00:40:47 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2020-06-22 21:52:53 +02:00
|
|
|
for curr_path != root_path || curr_path == "." {
|
|
|
|
|
2020-06-23 16:42:02 +02:00
|
|
|
c.path_contains_photos[curr_path] = contains_photo
|
2020-06-22 21:52:53 +02:00
|
|
|
|
|
|
|
curr_path = path.Dir(curr_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
func (c *AlbumScannerCache) AlbumContainsPhotos(path string) *bool {
|
2020-06-23 00:40:47 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
contains_photo, found := c.path_contains_photos[path]
|
2020-06-22 21:52:53 +02:00
|
|
|
if found {
|
|
|
|
// log.Printf("Album cache hit: %s\n", path)
|
|
|
|
return &contains_photo
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-22 23:52:41 +02:00
|
|
|
|
|
|
|
func (c *AlbumScannerCache) InsertPhotoType(path string, content_type ImageType) {
|
2020-06-23 00:40:47 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
(c.photo_types)[path] = content_type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlbumScannerCache) GetPhotoType(path string) *ImageType {
|
2020-06-23 00:40:47 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
result, found := c.photo_types[path]
|
|
|
|
if found {
|
|
|
|
// log.Printf("Image cache hit: %s\n", path)
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|