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"
|
2020-06-27 13:08:45 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2020-06-27 13:08:45 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-04-08 13:24:06 +02:00
|
|
|
)
|
|
|
|
|
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-09-13 17:51:43 +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:"Share" yaml:"Share"`
|
2021-09-23 23:46:17 +02:00
|
|
|
ShareSlug string `gorm:"type:VARBINARY(160);index;" json:"Slug" yaml:"Slug,omitempty"`
|
|
|
|
LinkToken string `gorm:"type:VARBINARY(160);unique_index:idx_links_uid_token;" json:"Token" yaml:"Token,omitempty"`
|
2020-06-28 14:52:26 +02:00
|
|
|
LinkExpires int `json:"Expires" yaml:"Expires,omitempty"`
|
|
|
|
LinkViews uint `json:"Views" yaml:"-"`
|
|
|
|
MaxViews uint `json:"MaxViews" 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"`
|
2021-09-23 15:38:55 +02:00
|
|
|
ModifiedAt time.Time `deepcopier:"skip" json:"ModifiedAt" 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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
if rnd.ValidID(m.LinkUID, 's') {
|
2020-06-22 15:16:26 +02:00
|
|
|
return nil
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
return scope.SetColumn("LinkUID", rnd.GenerateUID('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 {
|
2021-08-29 13:26:05 +02:00
|
|
|
now := TimeStamp()
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
result := Link{
|
2022-04-15 09:42:07 +02:00
|
|
|
LinkUID: rnd.GenerateUID('s'),
|
2020-06-22 15:16:26 +02:00
|
|
|
ShareUID: shareUID,
|
2022-04-15 09:42:07 +02:00
|
|
|
LinkToken: rnd.GenerateToken(10),
|
2020-06-22 15:16:26 +02:00
|
|
|
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() {
|
2020-06-28 14:52:26 +02:00
|
|
|
m.LinkViews += 1
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2022-04-04 08:54:03 +02:00
|
|
|
result := Db().Model(m).UpdateColumn("LinkViews", m.LinkViews)
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
log.Warnf("link: failed updating share view counter for %s", m.LinkUID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Link) Expired() bool {
|
2020-06-28 14:52:26 +02:00
|
|
|
if m.MaxViews > 0 && m.LinkViews >= m.MaxViews {
|
2020-06-26 12:24:05 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
if m.LinkExpires <= 0 {
|
2020-06-26 12:16:13 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
now := TimeStamp()
|
2020-06-28 14:52:26 +02:00
|
|
|
expires := m.ModifiedAt.Add(Seconds(m.LinkExpires))
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2020-11-22 03:12:31 +01:00
|
|
|
return now.After(expires)
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
func (m *Link) SetSlug(s string) {
|
2021-09-23 23:46:17 +02:00
|
|
|
m.ShareSlug = txt.Slug(s)
|
2020-06-27 13:08:45 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
if !rnd.EntityUID(m.ShareUID, 0) {
|
2020-06-22 15:16:26 +02:00
|
|
|
return fmt.Errorf("link: invalid share uid (%s)", m.ShareUID)
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
if m.LinkToken == "" {
|
2020-06-22 15:16:26 +02:00
|
|
|
return fmt.Errorf("link: empty share token")
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
m.ModifiedAt = TimeStamp()
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
// Delete the link.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Link) Delete() error {
|
2020-06-28 14:52:26 +02:00
|
|
|
if m.LinkToken == "" {
|
2020-06-22 15:16:26 +02:00
|
|
|
return fmt.Errorf("link: empty share token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
// DeleteShareLinks removed all links matching the share uid.
|
|
|
|
func DeleteShareLinks(shareUID string) error {
|
|
|
|
return Db().Delete(&Link{}, "share_uid = ?", shareUID).Error
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// 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-28 14:52:26 +02:00
|
|
|
func FindLinks(token, share string) (result Links) {
|
|
|
|
if token == "" && share == "" {
|
2020-06-22 15:16:26 +02:00
|
|
|
log.Errorf("link: share token and uid must not be empty at the same time (find links)")
|
|
|
|
return []Link{}
|
|
|
|
}
|
|
|
|
|
|
|
|
q := Db()
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
if token != "" {
|
|
|
|
q = q.Where("link_token = ?", token)
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
if share != "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
if rnd.EntityUID(share, 'a') {
|
2020-06-27 13:08:45 +02:00
|
|
|
q = q.Where("share_uid = ?", share)
|
|
|
|
} else {
|
|
|
|
q = q.Where("share_slug = ?", share)
|
|
|
|
}
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-31 12:02:26 +01:00
|
|
|
if err := q.Order("modified_at DESC").Find(&result).Error; err != nil {
|
2020-06-22 15:16:26 +02:00
|
|
|
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).
|
2020-06-28 14:52:26 +02:00
|
|
|
func FindValidLinks(token, share string) (result Links) {
|
|
|
|
for _, link := range FindLinks(token, share) {
|
2020-06-26 12:16:13 +02:00
|
|
|
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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
return clean.Log(m.LinkUID)
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|