alist/server/handles/down.go

116 lines
2.7 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-28 10:00:11 +00:00
import (
2022-06-28 13:58:46 +00:00
"fmt"
2022-06-29 08:08:55 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-28 13:58:46 +00:00
"github.com/alist-org/alist/v3/internal/sign"
2022-06-28 10:12:53 +00:00
stdpath "path"
"strings"
2022-06-28 10:00:11 +00:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
)
func Down(c *gin.Context) {
2022-06-28 13:58:46 +00:00
rawPath := c.MustGet("path").(string)
2022-06-28 10:00:11 +00:00
filename := stdpath.Base(rawPath)
2022-07-10 06:45:39 +00:00
storage, err := fs.GetStorage(rawPath)
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 13:58:46 +00:00
common.ErrorResp(c, err, 500)
return
2022-06-28 10:00:11 +00:00
}
2022-07-10 06:45:39 +00:00
if shouldProxy(storage, filename) {
2022-06-28 13:58:46 +00:00
Proxy(c)
return
} else {
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
})
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 13:58:46 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
return
}
2022-06-28 13:58:46 +00:00
c.Redirect(302, link.URL)
2022-06-28 10:00:11 +00:00
}
2022-06-28 13:58:46 +00:00
}
func Proxy(c *gin.Context) {
rawPath := c.MustGet("path").(string)
filename := stdpath.Base(rawPath)
2022-07-10 06:45:39 +00:00
storage, err := fs.GetStorage(rawPath)
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
return
}
2022-07-10 06:45:39 +00:00
if canProxy(storage, filename) {
downProxyUrl := storage.GetStorage().DownProxyUrl
2022-06-28 13:58:46 +00:00
if downProxyUrl != "" {
_, ok := c.GetQuery("d")
if ok {
URL := fmt.Sprintf("%s%s?sign=%s", strings.Split(downProxyUrl, "\n")[0], rawPath, sign.Sign(filename))
c.Redirect(302, URL)
return
}
}
link, file, err := fs.Link(c, rawPath, model.LinkArgs{
2022-06-28 10:00:11 +00:00
Header: c.Request.Header,
})
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
return
}
2022-06-28 13:58:46 +00:00
err = common.Proxy(c.Writer, c.Request, link, file)
2022-06-28 10:00:11 +00:00
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
} else {
2022-06-28 13:58:46 +00:00
common.ErrorStrResp(c, "proxy not allowed", 403)
return
2022-06-28 10:00:11 +00:00
}
}
2022-06-28 13:58:46 +00:00
// TODO need optimize
// when should be proxy?
// 1. config.MustProxy()
2022-07-10 06:45:39 +00:00
// 2. storage.WebProxy
2022-06-28 13:58:46 +00:00
// 3. proxy_types
2022-07-10 06:45:39 +00:00
func shouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
2022-06-28 13:58:46 +00:00
return true
2022-06-28 10:00:11 +00:00
}
2022-06-29 08:08:55 +00:00
proxyTypes := setting.GetByKey(conf.ProxyTypes)
2022-06-28 13:58:46 +00:00
if strings.Contains(proxyTypes, utils.Ext(filename)) {
return true
2022-06-28 10:00:11 +00:00
}
2022-06-28 13:58:46 +00:00
return false
2022-06-28 10:00:11 +00:00
}
2022-06-28 13:58:46 +00:00
// TODO need optimize
// when can be proxy?
// 1. text file
// 2. config.MustProxy()
2022-07-10 06:45:39 +00:00
// 3. storage.WebProxy
2022-06-28 13:58:46 +00:00
// 4. proxy_types
// solution: text_file + shouldProxy()
2022-07-10 06:45:39 +00:00
func canProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
2022-06-28 10:00:11 +00:00
return true
}
2022-06-29 08:08:55 +00:00
proxyTypes := setting.GetByKey(conf.ProxyTypes)
2022-06-28 10:00:11 +00:00
if strings.Contains(proxyTypes, utils.Ext(filename)) {
return true
}
2022-06-29 08:08:55 +00:00
textTypes := setting.GetByKey(conf.TextTypes)
2022-06-28 13:58:46 +00:00
if strings.Contains(textTypes, utils.Ext(filename)) {
return true
}
2022-06-28 10:00:11 +00:00
return false
}