mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 21:09:05 +00:00
Replace database, initial setup now works
This commit is contained in:
@@ -9,12 +9,12 @@ import (
|
|||||||
|
|
||||||
type Album struct {
|
type Album struct {
|
||||||
Model
|
Model
|
||||||
Title string
|
Title string `gorm:"not null"`
|
||||||
ParentAlbumID *int
|
ParentAlbumID *int
|
||||||
ParentAlbum *Album
|
ParentAlbum *Album
|
||||||
OwnerID int
|
OwnerID int `gorm:"not null"`
|
||||||
Owner User
|
Owner User
|
||||||
Path string
|
Path string `gorm:"not null"`
|
||||||
PathHash string `gorm:"unique"`
|
PathHash string `gorm:"unique"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
ID int `gorm:"primarykey"`
|
ID int `gorm:"primarykey"`
|
||||||
|
ModelTimestamps
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModelTimestamps struct {
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||||
|
|||||||
@@ -10,18 +10,18 @@ import (
|
|||||||
|
|
||||||
type Media struct {
|
type Media struct {
|
||||||
Model
|
Model
|
||||||
Title string
|
Title string `gorm:"not null"`
|
||||||
Path string
|
Path string `gorm:"not null"`
|
||||||
PathHash string
|
PathHash string `gorm:"not null"`
|
||||||
AlbumID int
|
AlbumID int `gorm:"not null"`
|
||||||
Album Album
|
Album Album
|
||||||
ExifID *int
|
ExifID *int
|
||||||
Exif *MediaEXIF
|
Exif *MediaEXIF
|
||||||
MediaURL []MediaURL
|
MediaURL []MediaURL
|
||||||
DateShot time.Time
|
DateShot time.Time `gorm:"not null"`
|
||||||
DateImported time.Time
|
DateImported time.Time `gorm:"not null"`
|
||||||
Favorite bool
|
Favorite bool `gorm:"not null, default:false"`
|
||||||
Type MediaType
|
Type MediaType `gorm:"not null"`
|
||||||
VideoMetadataID *int
|
VideoMetadataID *int
|
||||||
VideoMetadata *VideoMetadata
|
VideoMetadata *VideoMetadata
|
||||||
SideCarPath *string
|
SideCarPath *string
|
||||||
@@ -44,14 +44,14 @@ const (
|
|||||||
|
|
||||||
type MediaURL struct {
|
type MediaURL struct {
|
||||||
Model
|
Model
|
||||||
MediaID int
|
MediaID int `gorm:"not null"`
|
||||||
Media Media
|
Media Media
|
||||||
MediaName string
|
MediaName string `gorm:"not null"`
|
||||||
Width int
|
Width int `gorm:"not null"`
|
||||||
Height int
|
Height int `gorm:"not null"`
|
||||||
Purpose MediaPurpose
|
Purpose MediaPurpose `gorm:"not null"`
|
||||||
ContentType string
|
ContentType string `gorm:"not null"`
|
||||||
FileSize int64
|
FileSize int64 `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MediaURL) URL() string {
|
func (p *MediaURL) URL() string {
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ type MediaEXIF struct {
|
|||||||
GPSLonitude *float64
|
GPSLonitude *float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (MediaEXIF) TableName() string {
|
||||||
|
return "media_exif"
|
||||||
|
}
|
||||||
|
|
||||||
func (exif *MediaEXIF) Media() *Media {
|
func (exif *MediaEXIF) Media() *Media {
|
||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
|
|
||||||
type ShareToken struct {
|
type ShareToken struct {
|
||||||
Model
|
Model
|
||||||
Value string
|
Value string `gorm:"not null"`
|
||||||
OwnerID int
|
OwnerID int `gorm:"not null"`
|
||||||
Owner User
|
Owner User
|
||||||
Expire *time.Time
|
Expire *time.Time
|
||||||
Password *string
|
Password *string
|
||||||
AlbumID *int
|
AlbumID *int
|
||||||
Album Album
|
Album *Album
|
||||||
MediaID *int
|
MediaID *int
|
||||||
Media *Media
|
Media *Media
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SiteInfo struct {
|
type SiteInfo struct {
|
||||||
Model
|
InitialSetup bool `gorm:"not null"`
|
||||||
InitialSetup bool
|
PeriodicScanInterval int `gorm:"not null"`
|
||||||
PeriodicScanInterval int
|
ConcurrentWorkers int `gorm:"not null"`
|
||||||
ConcurrentWorkers int
|
}
|
||||||
|
|
||||||
|
func (SiteInfo) TableName() string {
|
||||||
|
return "site_info"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSiteInfo gets the site info row from the database, and creates it if it does not exist
|
// GetSiteInfo gets the site info row from the database, and creates it if it does not exist
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ type User struct {
|
|||||||
|
|
||||||
type AccessToken struct {
|
type AccessToken struct {
|
||||||
Model
|
Model
|
||||||
UserID int
|
UserID int `gorm:"not null"`
|
||||||
User User `gorm:"constraint:OnDelete:CASCADE;"`
|
User User `gorm:"constraint:OnDelete:CASCADE;"`
|
||||||
Value string `gorm:"size:24`
|
Value string `gorm:"not null, size:24`
|
||||||
Expire time.Time
|
Expire time.Time `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrorInvalidUserCredentials = errors.New("invalid credentials")
|
var ErrorInvalidUserCredentials = errors.New("invalid credentials")
|
||||||
@@ -163,12 +163,10 @@ func (user *User) GenerateAccessToken(db *gorm.DB) (*AccessToken, error) {
|
|||||||
|
|
||||||
func VerifyTokenAndGetUser(db *gorm.DB, token string) (*User, error) {
|
func VerifyTokenAndGetUser(db *gorm.DB, token string) (*User, error) {
|
||||||
|
|
||||||
now := time.Now().UTC().Format("2006-01-02 15:04:05")
|
|
||||||
|
|
||||||
// row := database.QueryRow("SELECT (user_id) FROM access_token WHERE expire > ? AND value = ?", now, token)
|
// row := database.QueryRow("SELECT (user_id) FROM access_token WHERE expire > ? AND value = ?", now, token)
|
||||||
|
|
||||||
var accessToken AccessToken
|
var accessToken AccessToken
|
||||||
result := db.Where("expire > ? AND value = ?", now, token).First(&accessToken)
|
result := db.Where("expire > ? AND value = ?", time.Now(), token).First(&accessToken)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ package models
|
|||||||
|
|
||||||
type VideoMetadata struct {
|
type VideoMetadata struct {
|
||||||
Model
|
Model
|
||||||
Width int
|
Width int `gorm:"not null"`
|
||||||
Height int
|
Height int `gorm:"not null"`
|
||||||
Duration float64
|
Duration float64 `gorm:"not null"`
|
||||||
Codec *string
|
Codec *string
|
||||||
Framerate *float64
|
Framerate *float64
|
||||||
Bitrate *string
|
Bitrate *string
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onl
|
|||||||
}
|
}
|
||||||
|
|
||||||
if showEmpty == nil || *showEmpty == false {
|
if showEmpty == nil || *showEmpty == false {
|
||||||
subQuery := r.Database.Model(&models.Media{}).Where("album_id = album.album_id")
|
subQuery := r.Database.Model(&models.Media{}).Where("album_id = albums.id")
|
||||||
|
|
||||||
if onlyWithFavorites != nil && *onlyWithFavorites == true {
|
if onlyWithFavorites != nil && *onlyWithFavorites == true {
|
||||||
subQuery = subQuery.Where("favorite = 1")
|
subQuery = subQuery.Where("favorite = 1")
|
||||||
@@ -35,7 +35,7 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onl
|
|||||||
// TODO: Incorporate models.FormatSQL
|
// TODO: Incorporate models.FormatSQL
|
||||||
|
|
||||||
var albums []*models.Album
|
var albums []*models.Album
|
||||||
if err := query.Find(albums).Error; err != nil {
|
if err := query.Find(&albums).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ func (r *albumResolver) Thumbnail(ctx context.Context, obj *models.Album) (*mode
|
|||||||
func (r *albumResolver) SubAlbums(ctx context.Context, parent *models.Album, filter *models.Filter) ([]*models.Album, error) {
|
func (r *albumResolver) SubAlbums(ctx context.Context, parent *models.Album, filter *models.Filter) ([]*models.Album, error) {
|
||||||
|
|
||||||
var albums []*models.Album
|
var albums []*models.Album
|
||||||
if err := r.Database.Where("parent_album = ?", parent.ID).Find(albums).Error; err != nil {
|
if err := r.Database.Where("parent_album = ?", parent.ID).Find(&albums).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ func (r *albumResolver) Owner(ctx context.Context, obj *models.Album) (*models.U
|
|||||||
func (r *albumResolver) Shares(ctx context.Context, album *models.Album) ([]*models.ShareToken, error) {
|
func (r *albumResolver) Shares(ctx context.Context, album *models.Album) ([]*models.ShareToken, error) {
|
||||||
|
|
||||||
var shareTokens []*models.ShareToken
|
var shareTokens []*models.ShareToken
|
||||||
if err := r.Database.Where("album_id = ?", album.ID).Find(shareTokens).Error; err != nil {
|
if err := r.Database.Where("album_id = ?", album.ID).Find(&shareTokens).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ func (r *Resolver) Media() api.MediaResolver {
|
|||||||
|
|
||||||
func (r *mediaResolver) Shares(ctx context.Context, media *models.Media) ([]*models.ShareToken, error) {
|
func (r *mediaResolver) Shares(ctx context.Context, media *models.Media) ([]*models.ShareToken, error) {
|
||||||
var shareTokens []*models.ShareToken
|
var shareTokens []*models.ShareToken
|
||||||
if err := r.Database.Where("media_id = ?", media.ID).Find(shareTokens).Error; err != nil {
|
if err := r.Database.Where("media_id = ?", media.ID).Find(&shareTokens).Error; err != nil {
|
||||||
return nil, errors.Wrapf(err, "get shares for media (%s)", media.Path)
|
return nil, errors.Wrapf(err, "get shares for media (%s)", media.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user