2020-02-01 17:58:45 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-02-12 18:10:52 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-03-12 12:55:53 +01:00
|
|
|
"time"
|
2020-02-01 17:58:45 +01:00
|
|
|
|
|
|
|
"github.com/h2non/filetype"
|
|
|
|
"github.com/viktorstrate/photoview/api/graphql/models"
|
2020-02-21 20:51:50 +01:00
|
|
|
"github.com/viktorstrate/photoview/api/graphql/notification"
|
|
|
|
"github.com/viktorstrate/photoview/api/utils"
|
2020-02-01 17:58:45 +01:00
|
|
|
)
|
|
|
|
|
2020-02-09 14:21:53 +01:00
|
|
|
type scanner_cache map[string]interface{}
|
|
|
|
|
|
|
|
func (cache *scanner_cache) insert_photo_type(path string, content_type string) {
|
2020-02-12 18:45:58 +01:00
|
|
|
(*cache)["photo_type//"+path] = content_type
|
2020-02-09 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cache *scanner_cache) get_photo_type(path string) *string {
|
2020-02-12 18:45:58 +01:00
|
|
|
result, found := (*cache)["photo_type//"+path].(string)
|
|
|
|
if found {
|
|
|
|
// log.Printf("Image cache hit: %s\n", path)
|
|
|
|
return &result
|
2020-02-09 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:45:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert single album directory in cache
|
|
|
|
func (cache *scanner_cache) insert_album_path(path string, contains_photo bool) {
|
|
|
|
(*cache)["album_path//"+path] = contains_photo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert album path and all parent directories up to the given root directory in cache
|
|
|
|
func (cache *scanner_cache) insert_album_paths(end_path string, root string, contains_photo bool) {
|
|
|
|
curr_path := path.Clean(end_path)
|
|
|
|
root_path := path.Clean(root)
|
|
|
|
|
|
|
|
for curr_path != root_path || curr_path == "." {
|
|
|
|
|
|
|
|
cache.insert_album_path(curr_path, contains_photo)
|
|
|
|
|
|
|
|
curr_path = path.Dir(curr_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cache *scanner_cache) album_contains_photo(path string) *bool {
|
|
|
|
contains_photo, found := (*cache)["album_path//"+path].(bool)
|
|
|
|
if found {
|
|
|
|
// log.Printf("Album cache hit: %s\n", path)
|
|
|
|
return &contains_photo
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-09 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:25:33 +01:00
|
|
|
func ScanUser(database *sql.DB, userId int) error {
|
2020-02-01 17:58:45 +01:00
|
|
|
|
|
|
|
row := database.QueryRow("SELECT * FROM user WHERE user_id = ?", userId)
|
|
|
|
user, err := models.NewUserFromRow(row)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not find user to scan: %s\n", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Starting scan for user '%s'\n", user.Username)
|
|
|
|
go scan(database, user)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scan(database *sql.DB, user *models.User) {
|
2020-02-21 20:51:50 +01:00
|
|
|
|
|
|
|
notifyKey := utils.GenerateToken()
|
2020-02-26 19:44:47 +01:00
|
|
|
processKey := utils.GenerateToken()
|
2020-03-12 13:26:11 +01:00
|
|
|
notifyThrottle := utils.NewThrottle(500 * time.Millisecond)
|
2020-02-21 20:51:50 +01:00
|
|
|
|
2020-02-26 21:23:13 +01:00
|
|
|
timeout := 3000
|
2020-02-21 20:51:50 +01:00
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: notifyKey,
|
|
|
|
Type: models.NotificationTypeMessage,
|
|
|
|
Header: "User scan started",
|
|
|
|
Content: "Scanning has started...",
|
2020-02-26 21:23:13 +01:00
|
|
|
Timeout: &timeout,
|
2020-02-21 20:51:50 +01:00
|
|
|
})
|
|
|
|
|
2020-02-12 18:10:52 +01:00
|
|
|
// Start scanning
|
2020-02-09 14:21:53 +01:00
|
|
|
scanner_cache := make(scanner_cache)
|
2020-02-12 18:10:52 +01:00
|
|
|
album_paths_scanned := make([]interface{}, 0)
|
2020-03-10 18:30:23 +01:00
|
|
|
photo_paths_scanned := make([]interface{}, 0)
|
2020-02-09 14:21:53 +01:00
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
type scanInfo struct {
|
|
|
|
path string
|
|
|
|
parentId *int
|
|
|
|
}
|
|
|
|
|
|
|
|
scanQueue := list.New()
|
|
|
|
scanQueue.PushBack(scanInfo{
|
|
|
|
path: user.RootPath,
|
|
|
|
parentId: nil,
|
|
|
|
})
|
|
|
|
|
2020-02-27 16:26:53 +01:00
|
|
|
newPhotos := list.New()
|
2020-02-26 21:23:13 +01:00
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
for scanQueue.Front() != nil {
|
|
|
|
albumInfo := scanQueue.Front().Value.(scanInfo)
|
|
|
|
scanQueue.Remove(scanQueue.Front())
|
|
|
|
|
|
|
|
albumPath := albumInfo.path
|
|
|
|
albumParentId := albumInfo.parentId
|
|
|
|
|
2020-02-12 18:10:52 +01:00
|
|
|
album_paths_scanned = append(album_paths_scanned, albumPath)
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
// Read path
|
|
|
|
dirContent, err := ioutil.ReadDir(albumPath)
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not read directory: %s\n", err.Error())
|
|
|
|
continue
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := database.Begin()
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not begin database transaction: %s\n", err)
|
|
|
|
continue
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Scanning directory: %s", albumPath)
|
|
|
|
|
|
|
|
// Make album if not exists
|
|
|
|
albumTitle := path.Base(albumPath)
|
|
|
|
_, err = tx.Exec("INSERT IGNORE INTO album (title, parent_album, owner_id, path) VALUES (?, ?, ?, ?)", albumTitle, albumParentId, user.UserID, albumPath)
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not insert album into database: %s\n", err)
|
2020-02-01 17:58:45 +01:00
|
|
|
tx.Rollback()
|
2020-02-23 11:59:57 +01:00
|
|
|
continue
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
row := tx.QueryRow("SELECT album_id FROM album WHERE path = ?", albumPath)
|
|
|
|
var albumId int
|
|
|
|
if err := row.Scan(&albumId); err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not get id of album: %s\n", err)
|
2020-02-01 17:58:45 +01:00
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:18:38 +01:00
|
|
|
// Commit album transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
log.Printf("ERROR: Could not commit database transaction: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
// Scan for photos
|
|
|
|
for _, item := range dirContent {
|
|
|
|
photoPath := path.Join(albumPath, item.Name())
|
|
|
|
|
2020-02-09 14:21:53 +01:00
|
|
|
if !item.IsDir() && isPathImage(photoPath, &scanner_cache) {
|
2020-02-02 18:18:38 +01:00
|
|
|
tx, err := database.Begin()
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not begin database transaction for image %s: %s\n", photoPath, err)
|
|
|
|
continue
|
2020-02-02 18:18:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
photo_paths_scanned = append(photo_paths_scanned, photoPath)
|
|
|
|
|
2020-03-12 12:30:55 +01:00
|
|
|
photo, isNewPhoto, err := ScanPhoto(tx, photoPath, albumId, processKey)
|
2020-02-26 21:23:13 +01:00
|
|
|
if err != nil {
|
|
|
|
ScannerError("Scanning image %s: %s", photoPath, err)
|
|
|
|
tx.Rollback()
|
2020-02-23 11:59:57 +01:00
|
|
|
continue
|
2020-02-09 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 12:30:55 +01:00
|
|
|
if isNewPhoto {
|
2020-02-27 16:26:53 +01:00
|
|
|
newPhotos.PushBack(photo)
|
2020-02-26 21:23:13 +01:00
|
|
|
|
2020-03-12 13:26:11 +01:00
|
|
|
notifyThrottle.Trigger(func() {
|
2020-02-26 21:23:13 +01:00
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: processKey,
|
|
|
|
Type: models.NotificationTypeMessage,
|
|
|
|
Header: "Scanning photo",
|
|
|
|
Content: fmt.Sprintf("Scanning image at %s", photoPath),
|
|
|
|
})
|
2020-03-12 12:55:53 +01:00
|
|
|
})
|
2020-02-02 00:29:42 +01:00
|
|
|
}
|
2020-02-01 17:58:45 +01:00
|
|
|
|
2020-02-02 18:18:38 +01:00
|
|
|
tx.Commit()
|
|
|
|
}
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for sub-albums
|
|
|
|
for _, item := range dirContent {
|
|
|
|
subalbumPath := path.Join(albumPath, item.Name())
|
|
|
|
|
2020-03-07 15:34:32 +01:00
|
|
|
// Skip if directory is hidden
|
|
|
|
if path.Base(subalbumPath)[0:1] == "." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:21:53 +01:00
|
|
|
if item.IsDir() && directoryContainsPhotos(subalbumPath, &scanner_cache) {
|
2020-02-01 17:58:45 +01:00
|
|
|
scanQueue.PushBack(scanInfo{
|
|
|
|
path: subalbumPath,
|
|
|
|
parentId: &albumId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:23:13 +01:00
|
|
|
completeMessage := "No new photos were found"
|
2020-02-27 16:26:53 +01:00
|
|
|
if newPhotos.Len() > 0 {
|
|
|
|
completeMessage = fmt.Sprintf("%d new photos were found", newPhotos.Len())
|
2020-02-26 21:23:13 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 20:51:50 +01:00
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: notifyKey,
|
|
|
|
Type: models.NotificationTypeMessage,
|
2020-02-26 21:23:13 +01:00
|
|
|
Header: "Scan completed",
|
|
|
|
Content: completeMessage,
|
2020-02-21 20:51:50 +01:00
|
|
|
Positive: true,
|
|
|
|
})
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
cleanupCache(database, album_paths_scanned, photo_paths_scanned, user)
|
2020-02-26 21:23:13 +01:00
|
|
|
|
2020-02-27 16:26:53 +01:00
|
|
|
err := processUnprocessedPhotos(database, user, notifyKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: processing photos: %s\n", err)
|
2020-02-26 21:23:13 +01:00
|
|
|
}
|
2020-02-26 19:44:47 +01:00
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
log.Println("Done scanning")
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:21:53 +01:00
|
|
|
func directoryContainsPhotos(rootPath string, cache *scanner_cache) bool {
|
2020-02-12 18:45:58 +01:00
|
|
|
|
|
|
|
if contains_image := cache.album_contains_photo(rootPath); contains_image != nil {
|
|
|
|
return *contains_image
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
scanQueue := list.New()
|
|
|
|
scanQueue.PushBack(rootPath)
|
|
|
|
|
2020-02-12 18:45:58 +01:00
|
|
|
scanned_directories := make([]string, 0)
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
for scanQueue.Front() != nil {
|
|
|
|
|
|
|
|
dirPath := scanQueue.Front().Value.(string)
|
|
|
|
scanQueue.Remove(scanQueue.Front())
|
|
|
|
|
2020-02-12 18:45:58 +01:00
|
|
|
scanned_directories = append(scanned_directories, dirPath)
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
dirContent, err := ioutil.ReadDir(dirPath)
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not read directory: %s\n", err.Error())
|
2020-02-01 17:58:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInfo := range dirContent {
|
|
|
|
filePath := path.Join(dirPath, fileInfo.Name())
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
scanQueue.PushBack(filePath)
|
|
|
|
} else {
|
2020-02-09 14:21:53 +01:00
|
|
|
if isPathImage(filePath, cache) {
|
2020-02-12 18:45:58 +01:00
|
|
|
cache.insert_album_paths(dirPath, rootPath, true)
|
2020-02-01 17:58:45 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:45:58 +01:00
|
|
|
for _, scanned_path := range scanned_directories {
|
|
|
|
cache.insert_album_path(scanned_path, false)
|
|
|
|
}
|
2020-02-01 17:58:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:05:58 +01:00
|
|
|
var SupportedMimetypes = [...]string{
|
2020-02-01 17:58:45 +01:00
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
2020-03-02 16:32:24 +01:00
|
|
|
"image/tiff",
|
2020-02-02 21:44:09 +01:00
|
|
|
"image/webp",
|
|
|
|
"image/x-canon-cr2",
|
2020-02-01 17:58:45 +01:00
|
|
|
"image/bmp",
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:05:58 +01:00
|
|
|
var WebMimetypes = [...]string{
|
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp",
|
|
|
|
"image/bmp",
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:21:53 +01:00
|
|
|
func isPathImage(path string, cache *scanner_cache) bool {
|
|
|
|
if cache.get_photo_type(path) != nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-02-01 17:58:45 +01:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not open file %s: %s\n", path, err)
|
2020-02-01 17:58:45 +01:00
|
|
|
return false
|
|
|
|
}
|
2020-02-02 18:18:38 +01:00
|
|
|
defer file.Close()
|
2020-02-01 17:58:45 +01:00
|
|
|
|
|
|
|
head := make([]byte, 261)
|
|
|
|
if _, err := file.Read(head); err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not read file %s: %s\n", path, err)
|
2020-02-01 17:58:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
imgType, err := filetype.Image(head)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:05:58 +01:00
|
|
|
for _, supported_mime := range SupportedMimetypes {
|
2020-02-01 17:58:45 +01:00
|
|
|
if supported_mime == imgType.MIME.Value {
|
2020-02-09 14:21:53 +01:00
|
|
|
cache.insert_photo_type(path, supported_mime)
|
2020-02-01 17:58:45 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Unsupported image %s of type %s\n", path, imgType.MIME.Value)
|
|
|
|
return false
|
|
|
|
}
|
2020-02-12 18:10:52 +01:00
|
|
|
|
2020-02-27 16:26:53 +01:00
|
|
|
func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey string) error {
|
|
|
|
|
|
|
|
processKey := utils.GenerateToken()
|
2020-03-12 13:26:11 +01:00
|
|
|
notifyThrottle := utils.NewThrottle(500 * time.Millisecond)
|
2020-02-27 16:26:53 +01:00
|
|
|
|
|
|
|
rows, err := database.Query(`
|
|
|
|
SELECT photo.* FROM photo JOIN album ON photo.album_id = album.album_id
|
|
|
|
WHERE album.owner_id = ?
|
|
|
|
AND photo.photo_id NOT IN (
|
|
|
|
SELECT photo_id FROM photo_url WHERE photo_url.photo_id = photo.photo_id
|
|
|
|
)
|
|
|
|
`, user.UserID)
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not get photos to process from db")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
photosToProcess, err := models.NewPhotosFromRows(rows)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// No photos to process
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ScannerError("Could not parse photos to process from db %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proccess all photos
|
|
|
|
for count, photo := range photosToProcess {
|
|
|
|
|
|
|
|
tx, err := database.Begin()
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not start database transaction: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:26:11 +01:00
|
|
|
notifyThrottle.Trigger(func() {
|
2020-03-12 12:55:53 +01:00
|
|
|
var progress float64 = float64(count) / float64(len(photosToProcess)) * 100.0
|
2020-02-27 16:26:53 +01:00
|
|
|
|
2020-03-12 12:55:53 +01:00
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: processKey,
|
|
|
|
Type: models.NotificationTypeProgress,
|
|
|
|
Header: fmt.Sprintf("Processing photos (%d of %d)", count, len(photosToProcess)),
|
|
|
|
Content: fmt.Sprintf("Processing photo at %s", photo.Path),
|
|
|
|
Progress: &progress,
|
|
|
|
})
|
2020-02-27 16:26:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
err = ProcessPhoto(tx, photo)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2020-03-12 12:30:55 +01:00
|
|
|
ScannerError("Could not process photo (%s): %s", photo.Path, err)
|
2020-02-27 16:26:53 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not commit db transaction: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(photosToProcess) > 0 {
|
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: notifyKey,
|
|
|
|
Type: models.NotificationTypeMessage,
|
|
|
|
Header: "Processing completed",
|
|
|
|
Content: fmt.Sprintf("%d photos have been processed", len(photosToProcess)),
|
|
|
|
Positive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: processKey,
|
|
|
|
Type: models.NotificationTypeClose,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
func cleanupCache(database *sql.DB, scanned_albums []interface{}, scanned_photos []interface{}, user *models.User) {
|
2020-02-12 18:10:52 +01:00
|
|
|
if len(scanned_albums) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
// Delete old albums
|
|
|
|
album_args := make([]interface{}, 0)
|
|
|
|
album_args = append(album_args, user.UserID)
|
|
|
|
album_args = append(album_args, scanned_albums...)
|
2020-02-12 18:19:16 +01:00
|
|
|
|
2020-02-12 18:10:52 +01:00
|
|
|
albums_questions := strings.Repeat("?,", len(scanned_albums))[:len(scanned_albums)*2-1]
|
2020-03-10 18:30:23 +01:00
|
|
|
rows, err := database.Query("SELECT album_id FROM album WHERE album.owner_id = ? AND path NOT IN ("+albums_questions+")", album_args...)
|
2020-02-12 18:10:52 +01:00
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not get albums from database: %s\n", err)
|
2020-02-12 18:10:52 +01:00
|
|
|
return
|
|
|
|
}
|
2020-02-29 09:21:41 +01:00
|
|
|
defer rows.Close()
|
2020-02-12 18:10:52 +01:00
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
deleted_album_ids := make([]interface{}, 0)
|
2020-02-12 18:10:52 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var album_id int
|
2020-02-26 21:23:13 +01:00
|
|
|
if err := rows.Scan(&album_id); err != nil {
|
|
|
|
ScannerError("Could not parse album to be removed (album_id %d): %s\n", album_id, err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
deleted_album_ids = append(deleted_album_ids, album_id)
|
2020-02-26 21:23:13 +01:00
|
|
|
cache_path := path.Join("./photo_cache", strconv.Itoa(album_id))
|
2020-02-12 18:10:52 +01:00
|
|
|
err := os.RemoveAll(cache_path)
|
|
|
|
if err != nil {
|
2020-02-23 11:59:57 +01:00
|
|
|
ScannerError("Could not delete unused cache folder: %s\n%s\n", cache_path, err)
|
2020-02-12 18:10:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
if len(deleted_album_ids) > 0 {
|
|
|
|
albums_questions = strings.Repeat("?,", len(deleted_album_ids))[:len(deleted_album_ids)*2-1]
|
2020-02-26 19:44:47 +01:00
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
if _, err := database.Exec("DELETE FROM album WHERE album_id IN ("+albums_questions+")", deleted_album_ids...); err != nil {
|
2020-02-26 19:44:47 +01:00
|
|
|
ScannerError("Could not delete old albums from database:\n%s\n", err)
|
2020-02-12 18:10:52 +01:00
|
|
|
}
|
2020-03-10 18:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete old photos
|
|
|
|
photo_args := make([]interface{}, 0)
|
|
|
|
photo_args = append(photo_args, user.UserID)
|
|
|
|
photo_args = append(photo_args, scanned_photos...)
|
|
|
|
|
|
|
|
photo_questions := strings.Repeat("?,", len(scanned_photos))[:len(scanned_photos)*2-1]
|
|
|
|
|
|
|
|
rows, err = database.Query(`
|
|
|
|
SELECT photo.photo_id as photo_id, album.album_id as album_id FROM photo JOIN album ON photo.album_id = album.album_id
|
|
|
|
WHERE album.owner_id = ? AND photo.path NOT IN (`+photo_questions+`)
|
|
|
|
`, photo_args...)
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not get deleted photos from database: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
deleted_photo_ids := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var photo_id int
|
|
|
|
var album_id int
|
|
|
|
|
|
|
|
if err := rows.Scan(&photo_id, &album_id); err != nil {
|
|
|
|
ScannerError("Could not parse photo to be removed (album_id %d, photo_id %d): %s\n", album_id, photo_id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleted_photo_ids = append(deleted_photo_ids, photo_id)
|
|
|
|
cache_path := path.Join("./photo_cache", strconv.Itoa(album_id), strconv.Itoa(photo_id))
|
|
|
|
err := os.RemoveAll(cache_path)
|
|
|
|
if err != nil {
|
|
|
|
ScannerError("Could not delete unused cache photo folder: %s\n%s\n", cache_path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(deleted_photo_ids) > 0 {
|
|
|
|
photo_questions = strings.Repeat("?,", len(deleted_photo_ids))[:len(deleted_photo_ids)*2-1]
|
|
|
|
|
|
|
|
if _, err := database.Exec("DELETE FROM photo WHERE photo_id IN ("+photo_questions+")", deleted_photo_ids...); err != nil {
|
|
|
|
ScannerError("Could not delete old photos from database:\n%s\n", err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 19:44:47 +01:00
|
|
|
|
2020-03-10 18:30:23 +01:00
|
|
|
if len(deleted_album_ids) > 0 || len(deleted_photo_ids) > 0 {
|
|
|
|
timeout := 3000
|
2020-02-26 19:44:47 +01:00
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: utils.GenerateToken(),
|
|
|
|
Type: models.NotificationTypeMessage,
|
2020-03-10 18:30:23 +01:00
|
|
|
Header: "Deleted old photos",
|
|
|
|
Content: fmt.Sprintf("Deleted %d albums and %d photos, that was not found on disk", len(deleted_album_ids), len(deleted_photo_ids)),
|
|
|
|
Timeout: &timeout,
|
2020-02-26 19:44:47 +01:00
|
|
|
})
|
2020-02-12 18:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-23 11:59:57 +01:00
|
|
|
|
|
|
|
func ScannerError(format string, args ...interface{}) {
|
|
|
|
message := fmt.Sprintf(format, args...)
|
|
|
|
|
|
|
|
log.Printf("ERROR: %s", message)
|
|
|
|
notification.BroadcastNotification(&models.Notification{
|
|
|
|
Key: utils.GenerateToken(),
|
|
|
|
Type: models.NotificationTypeMessage,
|
|
|
|
Header: "Scanner error",
|
|
|
|
Content: message,
|
|
|
|
Negative: true,
|
|
|
|
})
|
|
|
|
}
|
2020-02-23 12:43:45 +01:00
|
|
|
|
|
|
|
func PhotoCache() string {
|
|
|
|
photoCache := os.Getenv("PHOTO_CACHE")
|
|
|
|
if photoCache == "" {
|
|
|
|
photoCache = "./photo_cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
return photoCache
|
|
|
|
}
|