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-10-04 00:54:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
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
|
|
|
)
|
|
|
|
|
2022-10-04 00:54:39 +02:00
|
|
|
// LinkPrefix for RefID.
|
2022-09-30 00:42:19 +02:00
|
|
|
const (
|
2022-10-04 00:54:39 +02:00
|
|
|
LinkUID = byte('s')
|
|
|
|
LinkPrefix = "link"
|
2022-09-30 00:42:19 +02:00
|
|
|
)
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
type Links []Link
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Link represents a link to share content.
|
2020-04-08 13:24:06 +02:00
|
|
|
type Link struct {
|
2022-10-02 11:38:30 +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" yaml:"ShareUID"`
|
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"`
|
2022-10-02 11:38:30 +02:00
|
|
|
Comment string `gorm:"size:512;" json:"Comment,omitempty" yaml:"Comment,omitempty"`
|
|
|
|
Perm uint `json:"Perm,omitempty" yaml:"Perm,omitempty"`
|
|
|
|
RefID string `gorm:"type:VARBINARY(16);" json:"-" yaml:"-"`
|
|
|
|
CreatedBy string `gorm:"type:VARBINARY(42);index" json:"CreatedBy,omitempty" yaml:"CreatedBy,omitempty"`
|
2020-06-28 14:52:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2022-09-02 21:30:50 +02:00
|
|
|
func (Link) TableName() string {
|
|
|
|
return "links"
|
|
|
|
}
|
|
|
|
|
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-10-02 11:38:30 +02:00
|
|
|
if rnd.InvalidRefID(m.RefID) {
|
2022-10-04 00:54:39 +02:00
|
|
|
m.RefID = rnd.RefID(LinkPrefix)
|
2022-10-02 11:38:30 +02:00
|
|
|
Log("link", "set ref id", scope.SetColumn("RefID", m.RefID))
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
if rnd.IsUnique(m.LinkUID, LinkUID) {
|
2020-06-22 15:16:26 +02:00
|
|
|
return nil
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
return scope.SetColumn("LinkUID", rnd.GenerateUID(LinkUID))
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLink creates a sharing link.
|
2022-10-02 11:38:30 +02:00
|
|
|
func NewLink(shareUid string, canComment, canEdit bool) Link {
|
|
|
|
return NewUserLink(shareUid, OwnerUnknown)
|
2022-09-30 19:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserLink creates a sharing link owned by a user.
|
2022-10-02 11:38:30 +02:00
|
|
|
func NewUserLink(shareUid, userUid string) 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-09-30 00:42:19 +02:00
|
|
|
LinkUID: rnd.GenerateUID(LinkUID),
|
2022-10-02 11:38:30 +02:00
|
|
|
ShareUID: shareUid,
|
2022-04-15 09:42:07 +02:00
|
|
|
LinkToken: rnd.GenerateToken(10),
|
2022-10-02 11:38:30 +02:00
|
|
|
CreatedBy: userUid,
|
2020-06-26 12:16:13 +02:00
|
|
|
CreatedAt: now,
|
|
|
|
ModifiedAt: now,
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Redeem increases the number of link visitors by one.
|
|
|
|
func (m *Link) Redeem() *Link {
|
2020-06-28 14:52:26 +02:00
|
|
|
m.LinkViews += 1
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if err := Db().Model(m).UpdateColumn("link_views", gorm.Expr("link_views + 1")).Error; err != nil {
|
|
|
|
event.AuditWarn([]string{"link %s", "failed to update view counter"}, clean.Log(m.RefID), err)
|
|
|
|
}
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpiresAt returns the time when the share link expires or nil if it never expires.
|
|
|
|
func (m *Link) ExpiresAt() *time.Time {
|
|
|
|
if m.LinkExpires <= 0 {
|
|
|
|
return nil
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
2022-10-02 11:38:30 +02:00
|
|
|
|
|
|
|
expires := TimeStamp()
|
|
|
|
expires = m.ModifiedAt.Add(Seconds(m.LinkExpires))
|
|
|
|
|
|
|
|
return &expires
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Expired checks if the share link has expired.
|
2020-06-26 12:16:13 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if expires := m.ExpiresAt(); expires == nil {
|
2020-06-26 12:16:13 +02:00
|
|
|
return false
|
2022-10-02 11:38:30 +02:00
|
|
|
} else {
|
|
|
|
return TimeStamp().After(*expires)
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// SetSlug sets the URL slug of the link.
|
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
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// SetPassword sets the password required to use the share link.
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// InvalidPassword checks if the password provided to use the share link is invalid.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Link) InvalidPassword(password string) bool {
|
|
|
|
if !m.HasPassword {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
pw := FindPassword(m.LinkUID)
|
|
|
|
|
|
|
|
if pw == nil {
|
|
|
|
return password != ""
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
return pw.IsWrong(password)
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Save updates the record in the database or inserts a new record if it does not already exist.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Link) Save() error {
|
2022-09-28 09:01:17 +02:00
|
|
|
if !rnd.IsUID(m.ShareUID, 0) {
|
2022-10-02 11:38:30 +02:00
|
|
|
return fmt.Errorf("invalid share uid")
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
if m.LinkToken == "" {
|
2022-10-02 11:38:30 +02:00
|
|
|
return fmt.Errorf("empty link token")
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Delete permanently deletes 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 == "" {
|
2022-10-02 11:38:30 +02:00
|
|
|
return fmt.Errorf("empty link token")
|
2022-10-04 00:54:39 +02:00
|
|
|
} else if m.LinkUID == "" {
|
|
|
|
return fmt.Errorf("empty link uid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove related user shares.
|
|
|
|
if err := UnscopedDb().Delete(UserShare{}, "link_uid = ?", m.LinkUID).Error; err != nil {
|
|
|
|
event.AuditErr([]string{"link %s", "failed to remove related user shares", "%s"}, clean.Log(m.RefID), err)
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// DeleteShareLinks removes all links that match the shared UID.
|
|
|
|
func DeleteShareLinks(shareUid string) error {
|
2022-10-04 00:54:39 +02:00
|
|
|
if shareUid == "" {
|
|
|
|
return fmt.Errorf("empty share uid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove related user shares.
|
|
|
|
if err := UnscopedDb().Delete(UserShare{}, "share_uid = ?", shareUid).Error; err != nil {
|
|
|
|
event.AuditErr([]string{"share %s", "failed to remove related user shares", "%s"}, clean.Log(shareUid), err)
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
return Db().Delete(&Link{}, "share_uid = ?", shareUid).Error
|
2021-11-23 19:23:10 +01:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// FindLink finds the link with the specified UID or nil if it is not found.
|
|
|
|
func FindLink(linkUid string) *Link {
|
|
|
|
if rnd.InvalidUID(linkUid, LinkUID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
result := Link{}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if Db().Where("link_uid = ?", linkUid).First(&result).RecordNotFound() {
|
|
|
|
event.AuditWarn([]string{"link %s", "not found"}, clean.Log(linkUid))
|
|
|
|
return nil
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
return &result
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// FindLinks returns a slice of links for a token and a share UID (at least one must be specified).
|
|
|
|
func FindLinks(token, shared string) (found Links) {
|
|
|
|
found = Links{}
|
2022-09-28 09:01:17 +02:00
|
|
|
token = clean.ShareToken(token)
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if token == "" && shared == "" {
|
|
|
|
return found
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if shared != "" {
|
|
|
|
if rnd.IsUID(shared, 0) {
|
|
|
|
q = q.Where("share_uid = ?", shared)
|
2020-06-27 13:08:45 +02:00
|
|
|
} else {
|
2022-10-02 11:38:30 +02:00
|
|
|
q = q.Where("share_slug = ?", shared)
|
2020-06-27 13:08:45 +02:00
|
|
|
}
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if err := q.Order("modified_at DESC").Find(&found).Error; err != nil {
|
|
|
|
event.AuditErr([]string{"token %s", "%s"}, clean.Log(token), err)
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
return found
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
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).
|
2022-10-02 11:38:30 +02:00
|
|
|
func FindValidLinks(token, shared string) (found Links) {
|
|
|
|
found = Links{}
|
|
|
|
|
|
|
|
for _, link := range FindLinks(token, shared) {
|
|
|
|
if link.Expired() {
|
|
|
|
continue
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
2022-10-02 11:38:30 +02:00
|
|
|
|
|
|
|
found = append(found, link)
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
return found
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// String returns a human-readable identifier for use in logs.
|
2020-06-22 15:16:26 +02:00
|
|
|
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
|
|
|
}
|