2020-02-23 18:00:08 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
2020-05-14 14:35:08 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-02-23 18:00:08 +01:00
|
|
|
"github.com/viktorstrate/photoview/api/graphql/models"
|
|
|
|
"github.com/viktorstrate/photoview/api/utils"
|
|
|
|
|
|
|
|
// Image decoders
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/png"
|
|
|
|
|
2020-03-02 16:32:24 +01:00
|
|
|
_ "golang.org/x/image/bmp"
|
|
|
|
_ "golang.org/x/image/tiff"
|
2020-02-23 18:00:08 +01:00
|
|
|
_ "golang.org/x/image/webp"
|
|
|
|
)
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
// Higher order function used to check if MediaURL for a given MediaPurpose exists
|
|
|
|
func makePhotoURLChecker(tx *sql.Tx, mediaID int) (func(purpose models.MediaPurpose) (*models.MediaURL, error), error) {
|
|
|
|
mediaURLExistsStmt, err := tx.Prepare("SELECT * FROM media_url WHERE media_id = ? AND purpose = ?")
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
return func(purpose models.MediaPurpose) (*models.MediaURL, error) {
|
|
|
|
row := mediaURLExistsStmt.QueryRow(mediaID, purpose)
|
|
|
|
mediaURL, err := models.NewMediaURLFromRow(row)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
return mediaURL, nil
|
2020-02-23 18:00:08 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
func ProcessMedia(tx *sql.Tx, media *models.Media) (bool, error) {
|
2020-07-11 13:13:31 +02:00
|
|
|
imageData := EncodeMediaData{
|
2020-07-10 14:26:19 +02:00
|
|
|
media: media,
|
2020-07-10 12:58:11 +02:00
|
|
|
}
|
2020-02-23 18:00:08 +01:00
|
|
|
|
2020-07-10 12:58:11 +02:00
|
|
|
contentType, err := imageData.ContentType()
|
|
|
|
if err != nil {
|
2020-07-10 14:26:19 +02:00
|
|
|
return false, errors.Wrapf(err, "get content-type of media (%s)", media.Path)
|
2020-07-10 12:58:11 +02:00
|
|
|
}
|
2020-02-23 18:00:08 +01:00
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
// Make sure media cache directory exists
|
|
|
|
mediaCachePath, err := makeMediaCacheDir(media)
|
2020-07-10 12:58:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "cache directory error")
|
|
|
|
}
|
2020-06-23 15:13:07 +02:00
|
|
|
|
2020-07-10 12:58:11 +02:00
|
|
|
if contentType.isVideo() {
|
|
|
|
return processVideo(tx, &imageData, mediaCachePath)
|
|
|
|
} else {
|
|
|
|
return processPhoto(tx, &imageData, mediaCachePath)
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
2020-07-10 12:58:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 13:13:31 +02:00
|
|
|
func processPhoto(tx *sql.Tx, imageData *EncodeMediaData, photoCachePath *string) (bool, error) {
|
2020-07-10 12:58:11 +02:00
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
photo := imageData.media
|
2020-07-10 12:58:11 +02:00
|
|
|
|
|
|
|
log.Printf("Processing photo: %s\n", photo.Path)
|
|
|
|
|
|
|
|
didProcess := false
|
2020-02-23 18:00:08 +01:00
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
photoUrlFromDB, err := makePhotoURLChecker(tx, photo.MediaID)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// original photo url
|
2020-07-10 12:58:11 +02:00
|
|
|
origURL, err := photoUrlFromDB(models.MediaOriginal)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thumbnail
|
2020-05-14 14:35:08 +02:00
|
|
|
thumbURL, err := photoUrlFromDB(models.PhotoThumbnail)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-07-10 12:58:11 +02:00
|
|
|
return false, errors.Wrap(err, "error processing photo thumbnail")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Highres
|
2020-05-14 14:35:08 +02:00
|
|
|
highResURL, err := photoUrlFromDB(models.PhotoHighRes)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-07-10 12:58:11 +02:00
|
|
|
return false, errors.Wrap(err, "error processing photo highres")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
var photoDimensions *PhotoDimensions
|
|
|
|
var baseImagePath string = photo.Path
|
|
|
|
|
2020-11-19 08:21:35 +01:00
|
|
|
mediaType, err := getMediaType(photo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "could determine if media was photo or video")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mediaType.isRaw() {
|
|
|
|
err = processRawSideCar(tx, imageData, highResURL, thumbURL, photoCachePath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate high res jpeg
|
2020-02-23 18:00:08 +01:00
|
|
|
if highResURL == nil {
|
2020-02-26 21:23:13 +01:00
|
|
|
|
|
|
|
contentType, err := imageData.ContentType()
|
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-02-26 21:23:13 +01:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
if !contentType.isWebCompatible() {
|
2020-06-23 15:18:13 +02:00
|
|
|
didProcess = true
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
highres_name := fmt.Sprintf("highres_%s_%s", path.Base(photo.Path), utils.GenerateToken())
|
2020-10-30 17:14:09 +01:00
|
|
|
highres_name = models.SanitizeMediaName(highres_name)
|
2020-02-23 18:00:08 +01:00
|
|
|
highres_name = highres_name + ".jpg"
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
baseImagePath = path.Join(*photoCachePath, highres_name)
|
|
|
|
|
2020-11-19 08:21:35 +01:00
|
|
|
err = generateSaveHighResJPEG(tx, photo.MediaID, imageData, highres_name, baseImagePath, -1)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:35:08 +02:00
|
|
|
} else {
|
|
|
|
// Verify that highres photo still exists in cache
|
2020-07-10 14:26:19 +02:00
|
|
|
baseImagePath = path.Join(*photoCachePath, highResURL.MediaName)
|
2020-02-23 18:00:08 +01:00
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
if _, err := os.Stat(baseImagePath); os.IsNotExist(err) {
|
2020-07-10 14:26:19 +02:00
|
|
|
fmt.Printf("High-res photo found in database but not in cache, re-encoding photo to cache: %s\n", highResURL.MediaName)
|
2020-06-23 15:13:07 +02:00
|
|
|
didProcess = true
|
2020-02-23 18:00:08 +01:00
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
err = imageData.EncodeHighRes(tx, baseImagePath)
|
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, errors.Wrap(err, "creating high-res cached image")
|
2020-05-15 16:36:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save original photo to database
|
|
|
|
if origURL == nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
didProcess = true
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
// Make sure photo dimensions is set
|
|
|
|
if photoDimensions == nil {
|
|
|
|
photoDimensions, err = GetPhotoDimensions(baseImagePath)
|
2020-05-15 15:23:21 +02:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-05-15 15:23:21 +02:00
|
|
|
}
|
2020-05-15 16:36:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = saveOriginalPhotoToDB(tx, photo, imageData, photoDimensions); err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, errors.Wrap(err, "saving original photo to database")
|
2020-05-15 16:36:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save thumbnail to cache
|
|
|
|
if thumbURL == nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
didProcess = true
|
|
|
|
|
2020-05-15 16:36:02 +02:00
|
|
|
thumbnail_name := fmt.Sprintf("thumbnail_%s_%s", path.Base(photo.Path), utils.GenerateToken())
|
2020-10-30 17:14:09 +01:00
|
|
|
thumbnail_name = models.SanitizeMediaName(thumbnail_name)
|
2020-05-15 16:36:02 +02:00
|
|
|
thumbnail_name = thumbnail_name + ".jpg"
|
|
|
|
|
|
|
|
// thumbnailImage, err := imageData.ThumbnailImage(tx)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2020-05-15 15:23:21 +02:00
|
|
|
|
2020-11-19 08:21:35 +01:00
|
|
|
err = generateSaveThumbnailJPEG(tx, photo.MediaID, thumbnail_name, photoCachePath, baseImagePath, -1)
|
2020-05-15 16:36:02 +02:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, err
|
2020-05-15 16:36:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Verify that thumbnail photo still exists in cache
|
2020-07-10 14:26:19 +02:00
|
|
|
thumbPath := path.Join(*photoCachePath, thumbURL.MediaName)
|
2020-05-15 16:36:02 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(thumbPath); os.IsNotExist(err) {
|
2020-06-23 15:13:07 +02:00
|
|
|
didProcess = true
|
2020-07-10 14:26:19 +02:00
|
|
|
fmt.Printf("Thumbnail photo found in database but not in cache, re-encoding photo to cache: %s\n", thumbURL.MediaName)
|
2020-05-15 16:36:02 +02:00
|
|
|
|
|
|
|
_, err := EncodeThumbnail(baseImagePath, thumbPath)
|
2020-02-23 18:00:08 +01:00
|
|
|
if err != nil {
|
2020-06-23 15:13:07 +02:00
|
|
|
return false, errors.Wrap(err, "could not create thumbnail cached image")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 15:13:07 +02:00
|
|
|
return didProcess, nil
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
func makeMediaCacheDir(photo *models.Media) (*string, error) {
|
2020-02-23 18:00:08 +01:00
|
|
|
|
|
|
|
// Make root cache dir if not exists
|
|
|
|
if _, err := os.Stat(PhotoCache()); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(PhotoCache(), os.ModePerm); err != nil {
|
2020-07-10 12:58:11 +02:00
|
|
|
return nil, errors.Wrap(err, "could not make root image cache directory")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make album cache dir if not exists
|
|
|
|
albumCachePath := path.Join(PhotoCache(), strconv.Itoa(photo.AlbumId))
|
|
|
|
if _, err := os.Stat(albumCachePath); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(albumCachePath, os.ModePerm); err != nil {
|
2020-07-10 12:58:11 +02:00
|
|
|
return nil, errors.Wrap(err, "could not make album image cache directory")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make photo cache dir if not exists
|
2020-07-10 14:26:19 +02:00
|
|
|
photoCachePath := path.Join(albumCachePath, strconv.Itoa(photo.MediaID))
|
2020-02-23 18:00:08 +01:00
|
|
|
if _, err := os.Stat(photoCachePath); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(photoCachePath, os.ModePerm); err != nil {
|
2020-07-10 12:58:11 +02:00
|
|
|
return nil, errors.Wrap(err, "could not make photo image cache directory")
|
2020-02-23 18:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &photoCachePath, nil
|
|
|
|
}
|
2020-05-15 16:36:02 +02:00
|
|
|
|
2020-07-11 13:13:31 +02:00
|
|
|
func saveOriginalPhotoToDB(tx *sql.Tx, photo *models.Media, imageData *EncodeMediaData, photoDimensions *PhotoDimensions) error {
|
2020-05-15 16:36:02 +02:00
|
|
|
photoName := path.Base(photo.Path)
|
|
|
|
photoBaseName := photoName[0 : len(photoName)-len(path.Ext(photoName))]
|
|
|
|
photoBaseExt := path.Ext(photoName)
|
|
|
|
|
|
|
|
original_image_name := fmt.Sprintf("%s_%s", photoBaseName, utils.GenerateToken())
|
2020-10-30 17:14:09 +01:00
|
|
|
original_image_name = models.SanitizeMediaName(original_image_name) + photoBaseExt
|
2020-05-15 16:36:02 +02:00
|
|
|
|
|
|
|
contentType, err := imageData.ContentType()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:18:15 +02:00
|
|
|
fileStats, err := os.Stat(photo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading file stats of original photo")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("INSERT INTO media_url (media_id, media_name, width, height, purpose, content_type, file_size) VALUES (?, ?, ?, ?, ?, ?, ?)", photo.MediaID, original_image_name, photoDimensions.Width, photoDimensions.Height, models.MediaOriginal, contentType, fileStats.Size())
|
2020-05-15 16:36:02 +02:00
|
|
|
if err != nil {
|
2020-07-10 14:26:19 +02:00
|
|
|
log.Printf("Could not insert original photo url: %d, %s\n", photo.MediaID, photoName)
|
2020-05-15 16:36:02 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-19 08:21:35 +01:00
|
|
|
|
|
|
|
func generateSaveHighResJPEG(tx *sql.Tx, mediaID int, imageData *EncodeMediaData, highres_name string, imagePath string, urlID int) error {
|
|
|
|
|
|
|
|
err := imageData.EncodeHighRes(tx, imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating high-res cached image")
|
|
|
|
}
|
|
|
|
|
|
|
|
photoDimensions, err := GetPhotoDimensions(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileStats, err := os.Stat(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading file stats of highres photo")
|
|
|
|
}
|
|
|
|
|
|
|
|
if urlID < 0 {
|
|
|
|
_, err = tx.Exec("INSERT INTO media_url (media_id, media_name, width, height, purpose, content_type, file_size) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
mediaID, highres_name, photoDimensions.Width, photoDimensions.Height, models.PhotoHighRes, "image/jpeg", fileStats.Size())
|
|
|
|
} else {
|
|
|
|
_, err = tx.Exec("UPDATE media_url SET width = ?, height = ?, file_size= ? WHERE url_id = ?",
|
|
|
|
photoDimensions.Width, photoDimensions.Height, fileStats.Size(), urlID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not insert highres media url (%d, %s)", mediaID, highres_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSaveThumbnailJPEG(tx *sql.Tx, mediaID int, thumbnail_name string, photoCachePath *string, baseImagePath string, urlID int) error {
|
|
|
|
thumbOutputPath := path.Join(*photoCachePath, thumbnail_name)
|
|
|
|
|
|
|
|
thumbSize, err := EncodeThumbnail(baseImagePath, thumbOutputPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not create thumbnail cached image")
|
|
|
|
}
|
|
|
|
|
|
|
|
fileStats, err := os.Stat(thumbOutputPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading file stats of thumbnail photo")
|
|
|
|
}
|
|
|
|
|
|
|
|
if urlID < 0 {
|
|
|
|
_, err = tx.Exec("INSERT INTO media_url (media_id, media_name, width, height, purpose, content_type, file_size) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
mediaID, thumbnail_name, thumbSize.Width, thumbSize.Height, models.PhotoThumbnail, "image/jpeg", fileStats.Size())
|
|
|
|
} else {
|
|
|
|
_, err = tx.Exec("UPDATE media_url SET width = ?, height = ?, file_size= ? WHERE url_id = ?",
|
|
|
|
thumbSize.Width, thumbSize.Height, fileStats.Size(), urlID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func processRawSideCar(tx *sql.Tx, imageData *EncodeMediaData, highResURL *models.MediaURL, thumbURL *models.MediaURL, photoCachePath *string) error {
|
|
|
|
photo := imageData.media
|
|
|
|
sideCarFileHasChanged := false
|
|
|
|
var currentFileHash *string
|
|
|
|
currentSideCarPath := scanForSideCarFile(photo.Path)
|
|
|
|
|
|
|
|
if currentSideCarPath != nil {
|
2020-11-22 23:42:42 +01:00
|
|
|
currentFileHash = hashSideCarFile(currentSideCarPath)
|
2020-11-19 08:21:35 +01:00
|
|
|
if photo.SideCarHash == nil || *photo.SideCarHash != *currentFileHash {
|
|
|
|
sideCarFileHasChanged = true
|
|
|
|
}
|
|
|
|
} else if photo.SideCarPath != nil { // sidecar has been deleted since last scan
|
|
|
|
sideCarFileHasChanged = true
|
|
|
|
}
|
|
|
|
if sideCarFileHasChanged {
|
|
|
|
fmt.Printf("Detected changed sidecar file for %s recreating JPG's to reflect changes\n", photo.Path)
|
|
|
|
|
|
|
|
// update high res image may be cropped so dimentions and file size can change
|
|
|
|
baseImagePath := path.Join(*photoCachePath, highResURL.MediaName) // update base image path for thumbnail
|
|
|
|
tempHighResPath := baseImagePath + ".hold"
|
|
|
|
os.Rename(baseImagePath, tempHighResPath)
|
|
|
|
err := generateSaveHighResJPEG(tx, photo.MediaID, imageData, highResURL.MediaName, baseImagePath, highResURL.UrlID)
|
|
|
|
if err != nil {
|
|
|
|
os.Rename(tempHighResPath, baseImagePath)
|
|
|
|
return errors.Wrap(err, "recreating high-res cached image")
|
|
|
|
}
|
|
|
|
os.Remove(tempHighResPath)
|
|
|
|
|
|
|
|
// update thumbnail image may be cropped so dimentions and file size can change
|
|
|
|
thumbPath := path.Join(*photoCachePath, thumbURL.MediaName)
|
|
|
|
tempThumbPath := thumbPath + ".hold" // hold onto the original image incase for some reason we fail to recreate one with the new settings
|
|
|
|
os.Rename(thumbPath, tempThumbPath)
|
|
|
|
err = generateSaveThumbnailJPEG(tx, photo.MediaID, thumbURL.MediaName, photoCachePath, baseImagePath, thumbURL.UrlID)
|
|
|
|
if err != nil {
|
|
|
|
os.Rename(tempThumbPath, thumbPath)
|
|
|
|
return errors.Wrap(err, "recreating thumbnail cached image")
|
|
|
|
}
|
|
|
|
os.Remove(tempThumbPath)
|
|
|
|
|
|
|
|
// save new side car hash
|
|
|
|
_, err = tx.Exec("UPDATE media SET side_car_hash = ?, side_car_path = ? WHERE media_id = ?", currentFileHash, currentSideCarPath, photo.MediaID)
|
|
|
|
if err != nil {
|
2020-11-22 23:42:42 +01:00
|
|
|
return errors.Wrapf(err, "could not update side car hash for media: %s", photo.Path)
|
2020-11-19 08:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|