1
Fork 0
photoview/api/graphql/models/media.go

91 lines
2.4 KiB
Go
Raw Normal View History

package models
import (
"path"
"strings"
"time"
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/utils"
2020-11-30 21:29:49 +01:00
"gorm.io/gorm"
)
type Media struct {
Model
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"`
Type MediaType `gorm:"not null;index"`
VideoMetadataID *int `gorm:"index"`
VideoMetadata *VideoMetadata `gorm:"constraint:OnDelete:CASCADE;"`
2020-11-16 21:57:34 +01:00
SideCarPath *string
SideCarHash *string `gorm:"unique"`
// Only used internally
2021-01-19 18:35:19 +01:00
CounterpartPath *string `gorm:"-"`
}
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
}
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"
)
type MediaURL struct {
Model
MediaID int `gorm:"not null;index"`
Media Media `gorm:"constraint:OnDelete:CASCADE;"`
MediaName string `gorm:"not null"`
Width int `gorm:"not null"`
Height int `gorm:"not null"`
Purpose MediaPurpose `gorm:"not null;index"`
ContentType string `gorm:"not null"`
FileSize int64 `gorm:"not null"`
}
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)
}
return imageUrl.String()
}
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
}