github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/source_link.go (about) 1 package model 2 3 import ( 4 "fmt" 5 "github.com/cloudreve/Cloudreve/v3/pkg/hashid" 6 "github.com/jinzhu/gorm" 7 "net/url" 8 ) 9 10 // SourceLink represent a shared file source link 11 type SourceLink struct { 12 gorm.Model 13 FileID uint // corresponding file ID 14 Name string // name of the file while creating the source link, for annotation 15 Downloads int // 下载数 16 17 // 关联模型 18 File File `gorm:"save_associations:false:false"` 19 } 20 21 // Link gets the URL of a SourceLink 22 func (s *SourceLink) Link() (string, error) { 23 baseURL := GetSiteURL() 24 linkPath, err := url.Parse(fmt.Sprintf("/f/%s/%s", hashid.HashID(s.ID, hashid.SourceLinkID), s.File.Name)) 25 if err != nil { 26 return "", err 27 } 28 return baseURL.ResolveReference(linkPath).String(), nil 29 } 30 31 // GetTasksByID queries source link based on ID 32 func GetSourceLinkByID(id interface{}) (*SourceLink, error) { 33 link := &SourceLink{} 34 result := DB.Where("id = ?", id).First(link) 35 files, _ := GetFilesByIDs([]uint{link.FileID}, 0) 36 if len(files) > 0 { 37 link.File = files[0] 38 } 39 40 return link, result.Error 41 } 42 43 // Viewed 增加访问次数 44 func (s *SourceLink) Downloaded() { 45 s.Downloads++ 46 DB.Model(s).UpdateColumn("downloads", gorm.Expr("downloads + ?", 1)) 47 }