2020-04-08 13:24:06 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2020-06-22 15:16:26 +02:00
|
|
|
"fmt"
|
2020-04-08 13:24:06 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
type Links []Link
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
// Link represents a sharing link.
|
|
|
|
type Link struct {
|
2020-06-22 15:16:26 +02:00
|
|
|
LinkUID string `gorm:"type:varbinary(42);primary_key;" json:"UID,omitempty" yaml:"UID,omitempty"`
|
|
|
|
ShareUID string `gorm:"type:varbinary(42);unique_index:idx_links_uid_token;" json:"ShareUID"`
|
|
|
|
ShareToken string `gorm:"type:varbinary(255);unique_index:idx_links_uid_token;" json:"ShareToken"`
|
2020-06-26 12:16:13 +02:00
|
|
|
ShareExpires int `json:"ShareExpires" yaml:"ShareExpires,omitempty"`
|
|
|
|
ShareViews uint `json:"ShareViews" yaml:"-"`
|
|
|
|
HasPassword bool `json:"HasPassword" yaml:"HasPassword,omitempty"`
|
|
|
|
CanComment bool `json:"CanComment" yaml:"CanComment,omitempty"`
|
|
|
|
CanEdit bool `json:"CanEdit" yaml:"CanEdit,omitempty"`
|
|
|
|
CreatedAt time.Time `deepcopier:"skip" json:"CreatedAt" yaml:"CreatedAt"`
|
|
|
|
ModifiedAt time.Time `deepcopier:"skip" yaml:"ModifiedAt"`
|
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 {
|
2020-06-22 15:16:26 +02:00
|
|
|
if rnd.IsUID(m.LinkUID, 's') {
|
|
|
|
return nil
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
return scope.SetColumn("LinkUID", rnd.PPID('s'))
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLink creates a sharing link.
|
2020-06-22 15:16:26 +02:00
|
|
|
func NewLink(shareUID string, canComment, canEdit bool) Link {
|
2020-06-26 12:16:13 +02:00
|
|
|
now := Timestamp()
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
result := Link{
|
2020-06-22 15:16:26 +02:00
|
|
|
LinkUID: rnd.PPID('s'),
|
|
|
|
ShareUID: shareUID,
|
|
|
|
ShareToken: rnd.Token(10),
|
|
|
|
CanComment: canComment,
|
|
|
|
CanEdit: canEdit,
|
2020-06-26 12:16:13 +02:00
|
|
|
CreatedAt: now,
|
|
|
|
ModifiedAt: now,
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
func (m *Link) Redeem() {
|
|
|
|
m.ShareViews += 1
|
|
|
|
|
|
|
|
result := Db().Model(m).UpdateColumn("ShareViews", m.ShareViews)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
log.Warnf("link: failed updating share view counter for %s", m.LinkUID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Link) Expired() bool {
|
|
|
|
if m.ShareExpires <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
now := Timestamp()
|
|
|
|
expires := m.ModifiedAt.Add(Seconds(m.ShareExpires))
|
|
|
|
|
|
|
|
return now.Before(expires)
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Link) SetPassword(password string) error {
|
|
|
|
pw := NewPassword(m.LinkUID, password)
|
|
|
|
|
|
|
|
if err := pw.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.HasPassword = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Link) InvalidPassword(password string) bool {
|
|
|
|
if !m.HasPassword {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
pw := FindPassword(m.LinkUID)
|
|
|
|
|
|
|
|
if pw == nil {
|
|
|
|
return password != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return pw.InvalidPassword(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save inserts a new row to the database or updates a row if the primary key already exists.
|
|
|
|
func (m *Link) Save() error {
|
|
|
|
if !rnd.IsPPID(m.ShareUID, 0) {
|
|
|
|
return fmt.Errorf("link: invalid share uid (%s)", m.ShareUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ShareToken == "" {
|
|
|
|
return fmt.Errorf("link: empty share token")
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
m.ModifiedAt = Timestamp()
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
// Deletes the link.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Link) Delete() error {
|
|
|
|
if m.ShareToken == "" {
|
|
|
|
return fmt.Errorf("link: empty share token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLink returns an entity pointer if exists.
|
|
|
|
func FindLink(linkUID string) *Link {
|
|
|
|
result := Link{}
|
|
|
|
|
|
|
|
if err := Db().Where("link_uid = ?", linkUID).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else {
|
|
|
|
log.Errorf("link: %s (not found)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLinks returns a slice of links for a token and share UID (at least one must be provided).
|
2020-06-25 01:20:58 +02:00
|
|
|
func FindLinks(shareToken, shareUID string) (result Links) {
|
2020-06-22 15:16:26 +02:00
|
|
|
if shareToken == "" && shareUID == "" {
|
|
|
|
log.Errorf("link: share token and uid must not be empty at the same time (find links)")
|
|
|
|
return []Link{}
|
|
|
|
}
|
|
|
|
|
|
|
|
q := Db()
|
|
|
|
|
|
|
|
if shareToken != "" {
|
|
|
|
q = q.Where("share_token = ?", shareToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if shareUID != "" {
|
|
|
|
q = q.Where("share_uid = ?", shareUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Find(&result).Error; err != nil {
|
|
|
|
log.Errorf("link: %s (not found)", err)
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-06-22 15:16:26 +02:00
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
// FindValidLinks returns a slice of non-expired links for a token and share UID (at least one must be provided).
|
|
|
|
func FindValidLinks(shareToken, shareUID string) (result Links) {
|
|
|
|
for _, link := range FindLinks(shareToken, shareUID) {
|
|
|
|
if !link.Expired() {
|
|
|
|
result = append(result, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// String returns an human readable identifier for logging.
|
|
|
|
func (m *Link) String() string {
|
|
|
|
return fmt.Sprintf("%s/%s", m.ShareUID, m.ShareToken)
|
|
|
|
}
|