2020-04-08 13:24:06 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Link represents a sharing link.
|
|
|
|
type Link struct {
|
2020-05-23 20:58:58 +02:00
|
|
|
LinkToken string `gorm:"type:varbinary(255);primary_key;" json:"Token"`
|
|
|
|
LinkPassword string `gorm:"type:varbinary(255);" json:"Password"`
|
|
|
|
LinkExpires *time.Time `gorm:"type:datetime;" json:"Expires"`
|
2020-06-09 19:40:32 +02:00
|
|
|
ShareUID string `gorm:"type:varbinary(42);index;" json:"ShareUID"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CanComment bool `json:"CanComment"`
|
|
|
|
CanEdit bool `json:"CanEdit"`
|
|
|
|
CreatedAt time.Time `deepcopier:"skip" json:"CreatedAt"`
|
|
|
|
UpdatedAt time.Time `deepcopier:"skip" json:"UpdatedAt"`
|
|
|
|
DeletedAt *time.Time `deepcopier:"skip" sql:"index" json:"DeletedAt,omitempty"`
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
2020-04-08 13:24:06 +02:00
|
|
|
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
|
|
|
|
}
|