2020-07-10 14:26:19 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-02-15 17:35:28 +01:00
|
|
|
"fmt"
|
2020-07-10 14:26:19 +02:00
|
|
|
"path"
|
2021-02-15 17:35:28 +01:00
|
|
|
"strconv"
|
2020-10-30 17:14:09 +01:00
|
|
|
"strings"
|
2020-08-12 13:04:41 +02:00
|
|
|
"time"
|
2020-07-10 14:26:19 +02:00
|
|
|
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/utils"
|
2021-02-15 17:35:28 +01:00
|
|
|
"github.com/pkg/errors"
|
2020-11-30 21:29:49 +01:00
|
|
|
"gorm.io/gorm"
|
2020-07-10 14:26:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Media struct {
|
2020-11-28 21:29:31 +01:00
|
|
|
Model
|
2021-02-13 15:08:05 +01:00
|
|
|
Title string `gorm:"not null"`
|
|
|
|
Path string `gorm:"not null"`
|
|
|
|
PathHash string `gorm:"not null;unique"`
|
|
|
|
AlbumID int `gorm:"not null;index"`
|
|
|
|
Album Album `gorm:"constraint:OnDelete:CASCADE;"`
|
|
|
|
ExifID *int `gorm:"index"`
|
|
|
|
Exif *MediaEXIF `gorm:"constraint:OnDelete:CASCADE;"`
|
|
|
|
MediaURL []MediaURL `gorm:"constraint:OnDelete:CASCADE;"`
|
|
|
|
DateShot time.Time `gorm:"not null"`
|
2021-01-19 17:31:37 +01:00
|
|
|
Type MediaType `gorm:"not null;index"`
|
|
|
|
VideoMetadataID *int `gorm:"index"`
|
2021-01-27 18:00:37 +01:00
|
|
|
VideoMetadata *VideoMetadata `gorm:"constraint:OnDelete:CASCADE;"`
|
2020-11-16 21:57:34 +01:00
|
|
|
SideCarPath *string
|
2021-01-19 17:31:37 +01:00
|
|
|
SideCarHash *string `gorm:"unique"`
|
2020-12-10 12:30:10 +01:00
|
|
|
|
|
|
|
// Only used internally
|
2021-01-19 18:35:19 +01:00
|
|
|
CounterpartPath *string `gorm:"-"`
|
2020-07-10 14:26:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 17:31:19 +01:00
|
|
|
func (Media) TableName() string {
|
|
|
|
return "media"
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:29:49 +01:00
|
|
|
func (m *Media) BeforeSave(tx *gorm.DB) error {
|
|
|
|
// Update hashes
|
2021-01-17 12:45:23 +01:00
|
|
|
m.PathHash = MD5Hash(m.Path)
|
2020-11-30 21:29:49 +01:00
|
|
|
|
|
|
|
if m.SideCarPath != nil {
|
2021-01-17 12:45:23 +01:00
|
|
|
encodedHash := MD5Hash(*m.SideCarPath)
|
2020-11-30 21:29:49 +01:00
|
|
|
m.SideCarHash = &encodedHash
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:26:19 +02:00
|
|
|
type MediaPurpose string
|
|
|
|
|
|
|
|
const (
|
|
|
|
PhotoThumbnail MediaPurpose = "thumbnail"
|
|
|
|
PhotoHighRes MediaPurpose = "high-res"
|
|
|
|
MediaOriginal MediaPurpose = "original"
|
|
|
|
VideoWeb MediaPurpose = "video-web"
|
2020-07-11 13:39:11 +02:00
|
|
|
VideoThumbnail MediaPurpose = "video-thumbnail"
|
2020-07-10 14:26:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MediaURL struct {
|
2020-11-28 21:29:31 +01:00
|
|
|
Model
|
2021-01-19 17:31:37 +01:00
|
|
|
MediaID int `gorm:"not null;index"`
|
2021-02-15 17:35:28 +01:00
|
|
|
Media *Media `gorm:"constraint:OnDelete:CASCADE;"`
|
2020-11-28 21:49:33 +01:00
|
|
|
MediaName string `gorm:"not null"`
|
|
|
|
Width int `gorm:"not null"`
|
|
|
|
Height int `gorm:"not null"`
|
2021-01-19 17:31:37 +01:00
|
|
|
Purpose MediaPurpose `gorm:"not null;index"`
|
2020-11-28 21:49:33 +01:00
|
|
|
ContentType string `gorm:"not null"`
|
|
|
|
FileSize int64 `gorm:"not null"`
|
2020-07-10 14:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MediaURL) URL() string {
|
|
|
|
|
|
|
|
imageUrl := utils.ApiEndpointUrl()
|
2020-07-11 15:57:58 +02:00
|
|
|
if p.Purpose != VideoWeb {
|
|
|
|
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.MediaName)
|
|
|
|
} else {
|
|
|
|
imageUrl.Path = path.Join(imageUrl.Path, "video", p.MediaName)
|
|
|
|
}
|
2020-07-10 14:26:19 +02:00
|
|
|
|
|
|
|
return imageUrl.String()
|
|
|
|
}
|
|
|
|
|
2021-02-15 17:35:28 +01:00
|
|
|
func (p *MediaURL) CachedPath() (string, error) {
|
|
|
|
var cachedPath string
|
|
|
|
|
|
|
|
if p.Media == nil {
|
|
|
|
return "", errors.New("mediaURL.Media is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Purpose == PhotoThumbnail || p.Purpose == PhotoHighRes || p.Purpose == VideoThumbnail {
|
|
|
|
cachedPath = path.Join(utils.MediaCachePath(), strconv.Itoa(int(p.Media.AlbumID)), strconv.Itoa(int(p.MediaID)), p.MediaName)
|
|
|
|
} else if p.Purpose == MediaOriginal {
|
|
|
|
cachedPath = p.Media.Path
|
|
|
|
} else {
|
|
|
|
return "", errors.New(fmt.Sprintf("cannot determine cache path for purpose (%s)", p.Purpose))
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedPath, nil
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:14:09 +01:00
|
|
|
func SanitizeMediaName(mediaName string) string {
|
|
|
|
result := mediaName
|
|
|
|
result = strings.ReplaceAll(result, "/", "")
|
|
|
|
result = strings.ReplaceAll(result, "\\", "")
|
|
|
|
result = strings.ReplaceAll(result, " ", "_")
|
|
|
|
result = strings.ReplaceAll(result, ".", "_")
|
|
|
|
return result
|
|
|
|
}
|