feat(source link): create perm source link with shorter url
This commit is contained in:
41
models/source_link.go
Normal file
41
models/source_link.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
|
||||
"github.com/jinzhu/gorm"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// SourceLink represent a shared file source link
|
||||
type SourceLink struct {
|
||||
gorm.Model
|
||||
FileID uint // corresponding file ID
|
||||
Name string // name of the file while creating the source link, for annotation
|
||||
Downloads int // 下载数
|
||||
|
||||
// 关联模型
|
||||
File File `gorm:"save_associations:false:false"`
|
||||
}
|
||||
|
||||
// Link gets the URL of a SourceLink
|
||||
func (s *SourceLink) Link() (string, error) {
|
||||
baseURL := GetSiteURL()
|
||||
linkPath, err := url.Parse(fmt.Sprintf("/f/%s/%s", hashid.HashID(s.ID, hashid.SourceLinkID), s.File.Name))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return baseURL.ResolveReference(linkPath).String(), nil
|
||||
}
|
||||
|
||||
// GetTasksByID queries source link based on ID
|
||||
func GetSourceLinkByID(id interface{}) (*SourceLink, error) {
|
||||
link := &SourceLink{}
|
||||
result := DB.Where("id = ?", id).First(link)
|
||||
files, _ := GetFilesByIDs([]uint{link.FileID}, 0)
|
||||
if len(files) > 0 {
|
||||
link.File = files[0]
|
||||
}
|
||||
|
||||
return link, result.Error
|
||||
}
|
||||
Reference in New Issue
Block a user