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

78 lines
1.6 KiB
Go
Raw Normal View History

package models
import (
"path"
"strings"
"time"
"github.com/viktorstrate/photoview/api/utils"
2020-11-23 20:43:00 +01:00
"gorm.io/gorm"
)
type Media struct {
2020-11-23 20:43:00 +01:00
gorm.Model
2020-07-12 14:17:49 +02:00
Title string
Path string
PathHash string
AlbumID uint
2020-11-23 20:43:00 +01:00
Album Album
ExifID *uint
2020-11-28 17:31:19 +01:00
Exif *MediaEXIF
2020-11-23 20:43:00 +01:00
MediaURL []MediaURL
DateShot time.Time
DateImported time.Time
2020-07-12 14:17:49 +02:00
Favorite bool
Type MediaType
2020-11-25 23:06:47 +01:00
VideoMetadataID *uint
VideoMetadata *VideoMetadata
2020-11-16 21:57:34 +01:00
SideCarPath *string
SideCarHash *string
}
2020-11-28 17:31:19 +01:00
func (Media) TableName() string {
return "media"
}
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 {
2020-11-23 20:43:00 +01:00
gorm.Model
2020-11-24 11:46:49 +01:00
MediaID uint
Media Media
MediaName string
Width int
Height int
Purpose MediaPurpose
ContentType string
2020-11-24 11:46:49 +01:00
FileSize int64
}
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
}