parent
1f836a4b8b
commit
8d7ecedf47
@ -0,0 +1,29 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidateSourceLink validates if the perm source link is a valid redirect link
|
||||||
|
func ValidateSourceLink() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
linkID, ok := c.Get("object_id")
|
||||||
|
if !ok {
|
||||||
|
c.JSON(200, serializer.Err(serializer.CodeFileNotFound, "", nil))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceLink, err := model.GetSourceLinkByID(linkID)
|
||||||
|
if err != nil || sourceLink.File.ID == 0 || sourceLink.File.Name != c.Param("name") {
|
||||||
|
c.JSON(200, serializer.Err(serializer.CodeFileNotFound, "", nil))
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Set("source_link", sourceLink)
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
Loading…
Reference in new issue