✨ webdav direct proxy
This commit is contained in:
@ -10,13 +10,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Proxy(c *gin.Context) {
|
||||
@ -63,98 +57,13 @@ func Proxy(c *gin.Context) {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
// 本机读取数据
|
||||
if link.Data != nil {
|
||||
//c.Data(http.StatusOK, "application/octet-stream", link.Data)
|
||||
defer func() {
|
||||
_ = link.Data.Close()
|
||||
}()
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%s`, url.QueryEscape(file.Name)))
|
||||
c.Header("Content-Length", strconv.FormatInt(file.Size, 10))
|
||||
_, err = io.Copy(c.Writer, link.Data)
|
||||
if err != nil {
|
||||
_, _ = c.Writer.WriteString(err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
// 本机文件直接返回文件
|
||||
if account.Type == "Native" {
|
||||
// 对于名称为index.html的文件需要特殊处理
|
||||
if utils.Base(rawPath) == "index.html" {
|
||||
file, err := os.Open(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
fileStat, err := os.Stat(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
http.ServeContent(c.Writer, c.Request, utils.Base(rawPath), fileStat.ModTime(), file)
|
||||
return
|
||||
}
|
||||
c.File(link.Url)
|
||||
return
|
||||
} else {
|
||||
//if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT {
|
||||
// Text(c, link)
|
||||
// return
|
||||
//}
|
||||
r := c.Request
|
||||
w := c.Writer
|
||||
//target, err := url.Parse(link.Url)
|
||||
//if err != nil {
|
||||
// common.ErrorResp(c, err, 500)
|
||||
// return
|
||||
//}
|
||||
req, err := http.NewRequest("GET", link.Url, nil)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
log.Debugf("%+v", r.Header)
|
||||
for h, val := range r.Header {
|
||||
req.Header[h] = val
|
||||
}
|
||||
for _, header := range link.Headers {
|
||||
req.Header.Set(header.Name, header.Value)
|
||||
}
|
||||
log.Debugf("%+v", req.Header)
|
||||
res, err := HttpClient.Do(req)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = res.Body.Close()
|
||||
}()
|
||||
log.Debugf("proxy status: %d", res.StatusCode)
|
||||
w.WriteHeader(res.StatusCode)
|
||||
if res.StatusCode >= 400 {
|
||||
all, _ := ioutil.ReadAll(res.Body)
|
||||
log.Debugln(string(all))
|
||||
common.ErrorStrResp(c, string(all), 500)
|
||||
return
|
||||
}
|
||||
for h, v := range res.Header {
|
||||
w.Header()[h] = v
|
||||
}
|
||||
_, err = io.Copy(w, res.Body)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
err = common.Proxy(c.Writer, c.Request, link, file)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
}
|
||||
}
|
||||
|
||||
var client *resty.Client
|
||||
var HttpClient = &http.Client{}
|
||||
|
||||
func init() {
|
||||
client = resty.New()
|
||||
|
Reference in New Issue
Block a user