mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 22:59:05 +00:00
Fix #1204 * Add bad medias to scanner_test. * Fix the script. * Add GenerateThumbnail function. * Add identify dimension * Use new API. Cover json condition. * Use magick to generate thumbnails. * Update go mod. * Remove dependency of executable_worker -> test_utils * Reduce dependency of magick. * Use magick to get dimension. * Remove almost all code around `image` package. * Fix thumbnailing with multi-frame gif. * Remove the struct in wrapper. * Fix typo. * Build images. * Update logs * Add jpeg decoder. * Remove code dependency. * Add log to confirm scan in parallel. * Update log * Add reformat image perf tests. * Rollback the build workflow.
82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package processing_tasks
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/photoview/photoview/api/graphql/models"
|
|
"github.com/photoview/photoview/api/scanner/media_encoding"
|
|
"github.com/photoview/photoview/api/utils"
|
|
"github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Higher order function used to check if MediaURL for a given MediaPurpose exists
|
|
func makePhotoURLChecker(tx *gorm.DB, mediaID int) func(purpose models.MediaPurpose) (*models.MediaURL, error) {
|
|
return func(purpose models.MediaPurpose) (*models.MediaURL, error) {
|
|
var mediaURL []*models.MediaURL
|
|
|
|
result := tx.Where("purpose = ?", purpose).Where("media_id = ?", mediaID).Find(&mediaURL)
|
|
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
|
|
if result.RowsAffected > 0 {
|
|
return mediaURL[0], nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
func generateUniqueMediaNamePrefixed(prefix string, mediaPath string, extension string) string {
|
|
mediaName := fmt.Sprintf("%s_%s_%s", prefix, path.Base(mediaPath), utils.GenerateToken())
|
|
mediaName = models.SanitizeMediaName(mediaName)
|
|
mediaName = mediaName + extension
|
|
return mediaName
|
|
}
|
|
|
|
func generateUniqueMediaName(mediaPath string) string {
|
|
|
|
filename := path.Base(mediaPath)
|
|
baseName := filename[0 : len(filename)-len(path.Ext(filename))]
|
|
baseExt := path.Ext(filename)
|
|
|
|
mediaName := fmt.Sprintf("%s_%s", baseName, utils.GenerateToken())
|
|
mediaName = models.SanitizeMediaName(mediaName) + baseExt
|
|
|
|
return mediaName
|
|
}
|
|
|
|
func saveOriginalPhotoToDB(tx *gorm.DB, photo *models.Media, imageData *media_encoding.EncodeMediaData, photoDimensions media_encoding.Dimension) (*models.MediaURL, error) {
|
|
originalImageName := generateUniqueMediaName(photo.Path)
|
|
|
|
contentType, err := imageData.ContentType()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileStats, err := os.Stat(photo.Path)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "reading file stats of original photo")
|
|
}
|
|
|
|
mediaURL := models.MediaURL{
|
|
Media: photo,
|
|
MediaName: originalImageName,
|
|
Width: photoDimensions.Width,
|
|
Height: photoDimensions.Height,
|
|
Purpose: models.MediaOriginal,
|
|
ContentType: contentType.String(),
|
|
FileSize: fileStats.Size(),
|
|
}
|
|
|
|
if err := tx.Create(&mediaURL).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "inserting original photo url: %d, %s", photo.ID, photo.Title)
|
|
}
|
|
|
|
return &mediaURL, nil
|
|
}
|