photoprism/internal/entity/link.go
Michael Mayer a8c48ab40e Initial API and entities for link sharing
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-04-08 13:24:06 +02:00

42 lines
1 KiB
Go

package entity
import (
"time"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/pkg/rnd"
)
// Link represents a sharing link.
type Link struct {
LinkToken string `gorm:"type:varbinary(256);primary_key;"`
LinkPassword string `gorm:"type:varbinary(256);"`
LinkExpires *time.Time `gorm:"type:datetime;"`
ShareUUID string `gorm:"type:varbinary(36);index;"`
CanComment bool
CanEdit bool
CreatedAt time.Time `deepcopier:"skip"`
UpdatedAt time.Time `deepcopier:"skip"`
DeletedAt *time.Time `deepcopier:"skip" sql:"index"`
}
// BeforeCreate creates a new URL token when a new link is created.
func (m *Link) BeforeCreate(scope *gorm.Scope) error {
if err := scope.SetColumn("LinkToken", rnd.Token(10)); err != nil {
return err
}
return nil
}
// NewLink creates a sharing link.
func NewLink(password string, canComment, canEdit bool) Link {
result := Link{
LinkToken: rnd.Token(10),
LinkPassword: password,
CanComment: canComment,
CanEdit: canEdit,
}
return result
}