🐛 fix ftp download error

This commit is contained in:
微凉 2022-01-13 22:56:07 +08:00
parent b472c2ee18
commit 424ec10692
3 changed files with 33 additions and 10 deletions

View File

@ -53,15 +53,22 @@ func (driver FTP) Items() []base.Item {
}
func (driver FTP) Save(account *model.Account, old *model.Account) error {
if old != nil {
conn, ok := connMap[old.Name]
if ok {
err := conn.Quit()
log.Error("ftp:", err)
delete(connMap, old.Name)
}
}
if account.RootFolder == "" {
account.RootFolder = "/"
}
conn, err := driver.Login(account)
_, err := driver.Login(account)
if err != nil {
account.Status = err.Error()
} else {
account.Status = "work"
_ = conn.Quit()
}
_ = model.SaveAccount(account)
return err
@ -106,7 +113,7 @@ func (driver FTP) Files(path string, account *model.Account) ([]model.File, erro
if err != nil {
return nil, err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
entries, err := conn.List(realPath)
if err != nil {
return nil, err
@ -144,7 +151,7 @@ func (driver FTP) Link(args base.Args, account *model.Account) (*base.Link, erro
if err != nil {
return nil, err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
resp, err := conn.Retr(realPath)
if err != nil {
return nil, err
@ -191,7 +198,7 @@ func (driver FTP) MakeDir(path string, account *model.Account) error {
if err != nil {
return err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
err = conn.MakeDir(realPath)
return err
}
@ -203,7 +210,7 @@ func (driver FTP) Move(src string, dst string, account *model.Account) error {
if err != nil {
return err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
err = conn.Rename(realSrc, realDst)
return err
}
@ -223,7 +230,7 @@ func (driver FTP) Delete(path string, account *model.Account) error {
if err != nil {
return err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
err = conn.Delete(realPath)
return err
}
@ -237,7 +244,7 @@ func (driver FTP) Upload(file *model.FileStream, account *model.Account) error {
if err != nil {
return err
}
defer func() { _ = conn.Quit() }()
//defer func() { _ = conn.Quit() }()
err = conn.Stor(realPath, file)
return err
}

View File

@ -6,7 +6,13 @@ import (
"github.com/jlaffaye/ftp"
)
var connMap map[string]*ftp.ServerConn
func (driver FTP) Login(account *model.Account) (*ftp.ServerConn, error) {
conn, ok := connMap[account.Name]
if ok {
return conn, nil
}
conn, err := ftp.Connect(account.SiteUrl)
if err != nil {
return nil, err
@ -20,4 +26,4 @@ func (driver FTP) Login(account *model.Account) (*ftp.ServerConn, error) {
func init() {
base.RegisterDriver(&FTP{})
}
}

View File

@ -11,8 +11,10 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
)
func Proxy(c *gin.Context) {
@ -47,6 +49,12 @@ func Proxy(c *gin.Context) {
c.Redirect(302, link)
return
}
// 检查文件
file, err := driver.File(path, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// 对于中转,不需要重设IP
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
@ -60,7 +68,9 @@ func Proxy(c *gin.Context) {
_ = link.Data.Close()
}()
c.Status(http.StatusOK)
c.Header("content", "application/octet-stream")
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())