2020-06-22 21:52:53 +02:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/viktorstrate/photoview/api/graphql/models"
|
|
|
|
)
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
func scanAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB) {
|
|
|
|
// Scan for photos
|
|
|
|
albumPhotos, err := findPhotosForAlbum(album, cache, db, func(photo *models.Photo, newPhoto bool) {
|
|
|
|
// notifyThrottle.Trigger(func() {
|
|
|
|
// notification.BroadcastNotification(&models.Notification{
|
|
|
|
// Key: processKey,
|
|
|
|
// Type: models.NotificationTypeMessage,
|
|
|
|
// Header: fmt.Sprintf("Scanning photo for user '%s'", user.Username),
|
|
|
|
// Content: fmt.Sprintf("Scanning image at %s", photo.Path),
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Failed to find photos for album (%s): %s", album.Path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, photo := range albumPhotos {
|
2020-06-23 00:40:47 +02:00
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Failed to begin database transaction: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
err = ProcessPhoto(tx, photo)
|
|
|
|
if err != nil {
|
2020-06-23 00:40:47 +02:00
|
|
|
tx.Rollback()
|
2020-06-22 23:52:41 +02:00
|
|
|
ScannerError("Failed to process photo (%s): %s", photo.Path, err)
|
|
|
|
}
|
|
|
|
|
2020-06-23 00:40:47 +02:00
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Failed to commit database transaction: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
// TODO: Broadcast progress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func findPhotosForAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB, onScanPhoto func(photo *models.Photo, newPhoto bool)) ([]*models.Photo, error) {
|
2020-06-22 21:52:53 +02:00
|
|
|
|
|
|
|
newPhotos := make([]*models.Photo, 0)
|
|
|
|
|
|
|
|
dirContent, err := ioutil.ReadDir(album.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range dirContent {
|
|
|
|
photoPath := path.Join(album.Path, item.Name())
|
|
|
|
|
|
|
|
if !item.IsDir() && isPathImage(photoPath, cache) {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not begin database transaction for image %s: %s\n", photoPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
photo, isNewPhoto, err := ScanPhoto(tx, photoPath, album.AlbumID)
|
|
|
|
if err != nil {
|
2020-06-23 00:40:47 +02:00
|
|
|
ScannerError("Scanning image (%s): %s", photoPath, err)
|
2020-06-22 21:52:53 +02:00
|
|
|
tx.Rollback()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
onScanPhoto(photo, isNewPhoto)
|
|
|
|
|
|
|
|
if isNewPhoto {
|
|
|
|
newPhotos = append(newPhotos, photo)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPhotos, nil
|
|
|
|
}
|