|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/alist-org/alist/v3/internal/model" |
| 7 | + "gorm.io/gorm" |
| 8 | + "gorm.io/gorm/clause" |
| 9 | +) |
| 10 | + |
| 11 | +func GetShareByShareID(shareID string) (*model.Share, error) { |
| 12 | + var share model.Share |
| 13 | + if err := db.Where("share_id = ?", shareID).Take(&share).Error; err != nil { |
| 14 | + return nil, err |
| 15 | + } |
| 16 | + return &share, nil |
| 17 | +} |
| 18 | + |
| 19 | +func GetShareByCreatorAndShareID(creatorID uint, shareID string) (*model.Share, error) { |
| 20 | + var share model.Share |
| 21 | + if err := db.Where("creator_id = ? AND share_id = ?", creatorID, shareID).Take(&share).Error; err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + return &share, nil |
| 25 | +} |
| 26 | + |
| 27 | +func ShareIDExists(shareID string) (bool, error) { |
| 28 | + var count int64 |
| 29 | + if err := db.Model(&model.Share{}).Where("share_id = ?", shareID).Count(&count).Error; err != nil { |
| 30 | + return false, err |
| 31 | + } |
| 32 | + return count > 0, nil |
| 33 | +} |
| 34 | + |
| 35 | +func ShareIDExistsExceptID(shareID string, id uint) (bool, error) { |
| 36 | + var count int64 |
| 37 | + if err := db.Model(&model.Share{}).Where("share_id = ? AND id <> ?", shareID, id).Count(&count).Error; err != nil { |
| 38 | + return false, err |
| 39 | + } |
| 40 | + return count > 0, nil |
| 41 | +} |
| 42 | + |
| 43 | +func CreateShare(share *model.Share) error { |
| 44 | + return db.Create(share).Error |
| 45 | +} |
| 46 | + |
| 47 | +func UpdateShare(share *model.Share) error { |
| 48 | + return db.Save(share).Error |
| 49 | +} |
| 50 | + |
| 51 | +func GetSharesByCreator(creatorID uint, pageIndex, pageSize int) (shares []model.Share, count int64, err error) { |
| 52 | + tx := db.Model(&model.Share{}).Where("creator_id = ?", creatorID) |
| 53 | + err = tx.Count(&count).Error |
| 54 | + if err != nil { |
| 55 | + return nil, 0, err |
| 56 | + } |
| 57 | + err = tx.Order("created_at desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&shares).Error |
| 58 | + return |
| 59 | +} |
| 60 | + |
| 61 | +func DeleteShareByShareID(creatorID uint, shareID string) error { |
| 62 | + return db.Where("creator_id = ? AND share_id = ?", creatorID, shareID).Delete(&model.Share{}).Error |
| 63 | +} |
| 64 | + |
| 65 | +func DisableShareByShareID(creatorID uint, shareID string) error { |
| 66 | + return db.Model(&model.Share{}). |
| 67 | + Where("creator_id = ? AND share_id = ?", creatorID, shareID). |
| 68 | + Update("enabled", false).Error |
| 69 | +} |
| 70 | + |
| 71 | +func TouchShareView(shareID string) error { |
| 72 | + now := time.Now() |
| 73 | + return db.Model(&model.Share{}). |
| 74 | + Where("share_id = ?", shareID). |
| 75 | + UpdateColumns(map[string]interface{}{ |
| 76 | + "last_access_at": now, |
| 77 | + "view_count": gorm.Expr("view_count + ?", 1), |
| 78 | + }).Error |
| 79 | +} |
| 80 | + |
| 81 | +func TouchShareDownload(shareID string) error { |
| 82 | + now := time.Now() |
| 83 | + return db.Model(&model.Share{}). |
| 84 | + Where("share_id = ?", shareID). |
| 85 | + UpdateColumns(map[string]interface{}{ |
| 86 | + "last_access_at": now, |
| 87 | + "download_count": gorm.Expr("download_count + ?", 1), |
| 88 | + }).Error |
| 89 | +} |
| 90 | + |
| 91 | +func RecordShareAccess(shareID string) (*model.Share, error) { |
| 92 | + var updated model.Share |
| 93 | + err := db.Transaction(func(tx *gorm.DB) error { |
| 94 | + if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}). |
| 95 | + Where("share_id = ?", shareID). |
| 96 | + Take(&updated).Error; err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + now := time.Now() |
| 101 | + updated.AccessCount++ |
| 102 | + updated.LastAccessAt = &now |
| 103 | + updates := map[string]interface{}{ |
| 104 | + "access_count": updated.AccessCount, |
| 105 | + "last_access_at": now, |
| 106 | + } |
| 107 | + |
| 108 | + limit := updated.EffectiveAccessLimit() |
| 109 | + if limit > 0 && updated.AccessCount >= limit { |
| 110 | + updated.Enabled = false |
| 111 | + updated.ConsumedAt = &now |
| 112 | + updates["enabled"] = false |
| 113 | + updates["consumed_at"] = now |
| 114 | + } |
| 115 | + |
| 116 | + return tx.Model(&model.Share{}). |
| 117 | + Where("id = ?", updated.ID). |
| 118 | + Updates(updates).Error |
| 119 | + }) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + return &updated, nil |
| 124 | +} |
0 commit comments