2020-02-01 17:58:45 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-02-01 17:58:45 +01:00
|
|
|
"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-11-23 19:39:44 +01:00
|
|
|
"gorm.io/gorm"
|
2020-02-01 17:58:45 +01:00
|
|
|
)
|
|
|
|
|
2020-11-23 19:39:44 +01:00
|
|
|
func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScannerCache) ([]*models.Album, []error) {
|
2020-02-21 20:51:50 +01:00
|
|
|
|
2020-04-16 11:23:34 +02:00
|
|
|
// Check if user directory exists on the file system
|
|
|
|
if _, err := os.Stat(user.RootPath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-06-22 23:52:41 +02:00
|
|
|
return nil, []error{errors.Errorf("Photo directory for user '%s' does not exist '%s'\n", user.Username, user.RootPath)}
|
2020-04-16 11:23:34 +02:00
|
|
|
} else {
|
2020-06-22 23:52:41 +02:00
|
|
|
return nil, []error{errors.Errorf("Could not read photo directory for user '%s': %s\n", user.Username, user.RootPath)}
|
2020-04-16 11:23:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:58:45 +01:00
|
|
|
type scanInfo struct {
|
|
|
|
path string
|
2020-11-25 23:06:47 +01:00
|
|
|
parentID *uint
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
scanQueue := list.New()
|
|
|
|
scanQueue.PushBack(scanInfo{
|
|
|
|
path: user.RootPath,
|
2020-11-25 23:06:47 +01:00
|
|
|
parentID: nil,
|
2020-02-01 17:58:45 +01:00
|
|
|
})
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
userAlbums := make([]*models.Album, 0)
|
|
|
|
albumErrors := make([]error, 0)
|
|
|
|
// newPhotos := make([]*models.Photo, 0)
|
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
|
2020-11-25 23:06:47 +01:00
|
|
|
albumParentID := albumInfo.parentID
|
2020-02-01 17:58:45 +01:00
|
|
|
|
|
|
|
// Read path
|
|
|
|
dirContent, err := ioutil.ReadDir(albumPath)
|
|
|
|
if err != nil {
|
2020-06-22 23:52:41 +02:00
|
|
|
albumErrors = append(albumErrors, errors.Wrapf(err, "read directory (%s)", albumPath))
|
2020-02-23 11:59:57 +01:00
|
|
|
continue
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 23:06:47 +01:00
|
|
|
// Will become new album or album from db
|
|
|
|
var album models.Album
|
2020-02-01 17:58:45 +01:00
|
|
|
|
2020-11-25 23:06:47 +01:00
|
|
|
transErr := db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
log.Printf("Scanning directory: %s", albumPath)
|
2020-02-01 17:58:45 +01:00
|
|
|
|
2020-11-25 23:06:47 +01:00
|
|
|
// Make album if not exists
|
|
|
|
albumTitle := path.Base(albumPath)
|
2020-02-01 17:58:45 +01:00
|
|
|
|
2020-11-25 23:06:47 +01:00
|
|
|
err = tx.FirstOrCreate(&album, models.Album{
|
|
|
|
Title: albumTitle,
|
|
|
|
ParentAlbumID: albumParentID,
|
|
|
|
OwnerID: user.ID,
|
|
|
|
Path: albumPath,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "insert album into database")
|
|
|
|
}
|
|
|
|
|
|
|
|
userAlbums = append(userAlbums, &album)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-02-01 17:58:45 +01:00
|
|
|
|
2020-11-25 23:06:47 +01:00
|
|
|
if transErr != nil {
|
|
|
|
albumErrors = append(albumErrors, errors.Wrap(transErr, "begin database transaction"))
|
2020-06-22 23:52:41 +02:00
|
|
|
continue
|
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-06-22 23:52:41 +02:00
|
|
|
if item.IsDir() && directoryContainsPhotos(subalbumPath, album_cache) {
|
2020-02-01 17:58:45 +01:00
|
|
|
scanQueue.PushBack(scanInfo{
|
|
|
|
path: subalbumPath,
|
2020-11-25 23:06:47 +01:00
|
|
|
parentID: &album.ID,
|
2020-02-01 17:58:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
deleteErrors := deleteOldUserAlbums(db, userAlbums, user)
|
|
|
|
albumErrors = append(albumErrors, deleteErrors...)
|
2020-02-26 19:44:47 +01:00
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
return userAlbums, albumErrors
|
2020-02-01 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
2020-02-12 18:45:58 +01:00
|
|
|
|
2020-06-22 23:52:41 +02:00
|
|
|
if contains_image := cache.AlbumContainsPhotos(rootPath); contains_image != nil {
|
2020-02-12 18:45:58 +01:00
|
|
|
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-07-11 14:05:06 +02:00
|
|
|
if isPathMedia(filePath, cache) {
|
2020-06-22 23:52:41 +02:00
|
|
|
cache.InsertAlbumPaths(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 {
|
2020-06-22 23:52:41 +02:00
|
|
|
cache.InsertAlbumPath(scanned_path, false)
|
2020-02-12 18:45:58 +01:00
|
|
|
}
|
2020-02-01 17:58:45 +01:00
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
|
}
|