mirror of
https://github.com/cloudreve/cloudreve.git
synced 2025-12-15 10:04:01 +08:00
feat(direct link): add option to get direct link with download enforced (#2651)
This commit is contained in:
2
assets
2
assets
Submodule assets updated: 3a6a22bb45...e47a708f72
@@ -168,7 +168,7 @@ func (m *manager) GetUrlForRedirectedDirectLink(ctx context.Context, dl *ent.Dir
|
||||
)
|
||||
|
||||
// Try to read from cache.
|
||||
cacheKey := entityUrlCacheKey(primaryEntity.ID(), int64(dl.Speed), dl.Name, false,
|
||||
cacheKey := entityUrlCacheKey(primaryEntity.ID(), int64(dl.Speed), dl.Name, o.IsDownload,
|
||||
m.settings.SiteURL(ctx).String())
|
||||
if cached, ok := m.kv.Get(cacheKey); ok {
|
||||
cachedItem := cached.(EntityUrlCache)
|
||||
@@ -185,7 +185,7 @@ func (m *manager) GetUrlForRedirectedDirectLink(ctx context.Context, dl *ent.Dir
|
||||
m.l, m.config, m.dep.MimeDetector(ctx))
|
||||
downloadUrl, err := source.Url(ctx,
|
||||
entitysource.WithExpire(o.Expire),
|
||||
entitysource.WithDownload(false),
|
||||
entitysource.WithDownload(o.IsDownload),
|
||||
entitysource.WithSpeedLimit(int64(dl.Speed)),
|
||||
entitysource.WithDisplayName(dl.Name),
|
||||
)
|
||||
|
||||
@@ -86,12 +86,14 @@ func ExtractArchive(c *gin.Context) {
|
||||
}
|
||||
|
||||
// AnonymousPermLink 文件中转后的永久直链接
|
||||
func AnonymousPermLink(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
if err := explorer.RedirectDirectLink(c, name); err != nil {
|
||||
c.JSON(404, serializer.Err(c, err))
|
||||
c.Abort()
|
||||
return
|
||||
func AnonymousPermLink(download bool) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
if err := explorer.RedirectDirectLink(c, name, download); err != nil {
|
||||
c.JSON(404, serializer.Err(c, err))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +245,10 @@ func initMasterRouter(dep dependency.Dep) *gin.Engine {
|
||||
{
|
||||
source.GET(":id/:name",
|
||||
middleware.HashID(hashid.SourceLinkID),
|
||||
controllers.AnonymousPermLink)
|
||||
controllers.AnonymousPermLink(false))
|
||||
source.GET("d/:id/:name",
|
||||
middleware.HashID(hashid.SourceLinkID),
|
||||
controllers.AnonymousPermLink(true))
|
||||
}
|
||||
|
||||
shareShort := r.Group("s")
|
||||
|
||||
@@ -659,7 +659,7 @@ func (s *GetFileInfoService) Get(c *gin.Context) (*FileResponse, error) {
|
||||
return BuildFileResponse(c, user, file, dep.HashIDEncoder(), nil), nil
|
||||
}
|
||||
|
||||
func RedirectDirectLink(c *gin.Context, name string) error {
|
||||
func RedirectDirectLink(c *gin.Context, name string, download bool) error {
|
||||
dep := dependency.FromContext(c)
|
||||
settings := dep.SettingProvider()
|
||||
|
||||
@@ -680,6 +680,7 @@ func RedirectDirectLink(c *gin.Context, name string) error {
|
||||
expire := time.Now().Add(settings.EntityUrlValidDuration(c))
|
||||
res, earliestExpire, err := m.GetUrlForRedirectedDirectLink(c, dl,
|
||||
fs.WithUrlExpire(&expire),
|
||||
fs.WithIsDownload(download),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user