1
Fork 0
photoview/api/scanner/scanner_media.go

90 lines
2.3 KiB
Go
Raw Permalink Normal View History

2020-02-23 18:00:08 +01:00
package scanner
import (
2022-03-02 17:26:06 +01:00
"context"
2020-02-23 18:00:08 +01:00
"log"
"os"
2020-02-23 18:00:08 +01:00
"path"
2020-02-26 19:44:47 +01:00
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/graphql/models"
2022-03-02 17:26:06 +01:00
"github.com/photoview/photoview/api/scanner/media_encoding"
"github.com/photoview/photoview/api/scanner/scanner_cache"
2022-03-02 17:26:06 +01:00
"github.com/photoview/photoview/api/scanner/scanner_task"
2020-07-10 18:57:27 +02:00
"github.com/pkg/errors"
2020-11-23 19:59:01 +01:00
"gorm.io/gorm"
2020-02-23 18:00:08 +01:00
)
func ScanMedia(tx *gorm.DB, mediaPath string, albumId int, cache *scanner_cache.AlbumScannerCache) (*models.Media, bool, error) {
2020-07-10 18:57:27 +02:00
mediaName := path.Base(mediaPath)
2020-02-23 18:00:08 +01:00
2020-11-23 20:43:00 +01:00
// Check if media already exists
{
2020-11-30 21:29:49 +01:00
var media []*models.Media
2021-01-17 12:45:23 +01:00
result := tx.Where("path_hash = ?", models.MD5Hash(mediaPath)).Find(&media)
2020-11-30 21:29:49 +01:00
if result.Error != nil {
return nil, false, errors.Wrap(result.Error, "scan media fetch from database")
}
if result.RowsAffected > 0 {
2021-11-21 11:41:52 +01:00
// log.Printf("Media already scanned: %s\n", mediaPath)
2020-11-30 21:29:49 +01:00
return media[0], false, nil
2020-02-23 18:00:08 +01:00
}
}
2020-07-10 18:57:27 +02:00
log.Printf("Scanning media: %s\n", mediaPath)
mediaType, err := cache.GetMediaType(mediaPath)
if err != nil {
return nil, false, errors.Wrap(err, "could determine if media was photo or video")
}
2020-11-23 20:43:00 +01:00
var mediaTypeText models.MediaType
if mediaType.IsVideo() {
2020-11-23 20:43:00 +01:00
mediaTypeText = models.MediaTypeVideo
} else {
2020-11-23 20:43:00 +01:00
mediaTypeText = models.MediaTypePhoto
}
stat, err := os.Stat(mediaPath)
if err != nil {
return nil, false, err
}
2020-11-23 20:43:00 +01:00
media := models.Media{
Title: mediaName,
Path: mediaPath,
AlbumID: albumId,
Type: mediaTypeText,
DateShot: stat.ModTime(),
2020-02-23 18:00:08 +01:00
}
2020-11-23 20:43:00 +01:00
if err := tx.Create(&media).Error; err != nil {
return nil, false, errors.Wrap(err, "could not insert media into database")
2020-02-23 18:00:08 +01:00
}
2022-03-02 17:26:06 +01:00
return &media, true, nil
}
// ProcessSingleMedia processes a single media, might be used to reprocess media with corrupted cache
// Function waits for processing to finish before returning.
func ProcessSingleMedia(db *gorm.DB, media *models.Media) error {
album_cache := scanner_cache.MakeAlbumCache()
var album models.Album
if err := db.Model(media).Association("Album").Find(&album); err != nil {
return err
2020-07-12 14:17:49 +02:00
}
2022-03-02 17:26:06 +01:00
media_data := media_encoding.NewEncodeMediaData(media)
task_context := scanner_task.NewTaskContext(context.Background(), db, &album, album_cache)
2023-02-03 09:14:30 +01:00
if err := scanMedia(task_context, media, &media_data, 0, 1); err != nil {
return errors.Wrap(err, "single media scan")
2022-03-02 17:26:06 +01:00
}
return nil
2020-02-23 18:00:08 +01:00
}