fix(ftp-server): rewrite download in a more appropriate method (#7656)

pull/7673/head
KirCute_ECT 2024-12-25 21:11:45 +08:00 committed by GitHub
parent 6aaf5975c6
commit b72e85a73a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 38 deletions

View File

@ -6,10 +6,8 @@ import (
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/net"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/internal/stream"
"github.com/alist-org/alist/v3/server/common"
"github.com/pkg/errors"
"io"
@ -21,8 +19,7 @@ import (
type FileDownloadProxy struct {
ftpserver.FileTransfer
reader io.ReadCloser
closers *utils.Closers
reader io.ReadCloser
}
func OpenDownload(ctx context.Context, reqPath string) (*FileDownloadProxy, error) {
@ -47,37 +44,15 @@ func OpenDownload(ctx context.Context, reqPath string) (*FileDownloadProxy, erro
if err != nil {
return nil, err
}
storage, err := fs.GetStorage(reqPath, &fs.GetStoragesArgs{})
fileStream := stream.FileStream{
Obj: obj,
Ctx: ctx,
}
ss, err := stream.NewSeekableStream(fileStream, link)
if err != nil {
return nil, err
}
if storage.GetStorage().ProxyRange {
common.ProxyRange(link, obj.GetSize())
}
reader, closers, err := proxy(link)
if err != nil {
return nil, err
}
return &FileDownloadProxy{reader: reader, closers: closers}, nil
}
func proxy(link *model.Link) (io.ReadCloser, *utils.Closers, error) {
if link.MFile != nil {
return link.MFile, nil, nil
} else if link.RangeReadCloser != nil {
rc, err := link.RangeReadCloser.RangeRead(context.Background(), http_range.Range{Length: -1})
if err != nil {
return nil, nil, err
}
closers := link.RangeReadCloser.GetClosers()
return rc, &closers, nil
} else {
res, err := net.RequestHttp(context.Background(), http.MethodGet, link.Header, link.URL)
if err != nil {
return nil, nil, err
}
return res.Body, nil, nil
}
return &FileDownloadProxy{reader: ss}, nil
}
func (f *FileDownloadProxy) Read(p []byte) (n int, err error) {
@ -93,11 +68,6 @@ func (f *FileDownloadProxy) Seek(offset int64, whence int) (int64, error) {
}
func (f *FileDownloadProxy) Close() error {
defer func() {
if f.closers != nil {
_ = f.closers.Close()
}
}()
return f.reader.Close()
}