From c03646dedfac8a5370e1ca9a57553256bae2b7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= Date: Mon, 15 Nov 2021 19:06:10 +0800 Subject: [PATCH] :bug: change text proxy --- server/down.go | 21 +++++++++++++++++++++ server/static.go | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/down.go b/server/down.go index ac0014fd..7dbcc920 100644 --- a/server/down.go +++ b/server/down.go @@ -5,6 +5,7 @@ import ( "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" + "github.com/go-resty/resty/v2" log "github.com/sirupsen/logrus" "net/http/httputil" "net/url" @@ -65,6 +66,10 @@ func Proxy(c *gin.Context) { c.File(link) return } else { + if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT { + Text(c, link) + return + } driver.Proxy(c) r := c.Request w := c.Writer @@ -84,3 +89,19 @@ func Proxy(c *gin.Context) { proxy.ServeHTTP(w, r) } } + +var client *resty.Client + +func init() { + client = resty.New() + client.SetRetryCount(3) +} + +func Text(c *gin.Context, link string) { + res, err := client.R().Get(link) + if err != nil { + ErrorResp(c,err,500) + return + } + c.String(200,res.String()) +} diff --git a/server/static.go b/server/static.go index 8d01c250..bdbd6bee 100644 --- a/server/static.go +++ b/server/static.go @@ -12,7 +12,11 @@ import ( func init() { - index, _ := public.Public.Open("index.html") + index, err := public.Public.Open("index.html") + if err != nil { + log.Errorf(err.Error()) + return + } data, _ := ioutil.ReadAll(index) conf.RawIndexHtml = string(data) }