Skip to content

Commit

Permalink
fix(ftp-server): rewrite download in a more appropriate method (#7656)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirCute authored Dec 25, 2024
1 parent 6aaf597 commit b72e85a
Showing 1 changed file with 7 additions and 37 deletions.
44 changes: 7 additions & 37 deletions server/ftp/fsread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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{})
if err != nil {
return nil, err
}
if storage.GetStorage().ProxyRange {
common.ProxyRange(link, obj.GetSize())
fileStream := stream.FileStream{
Obj: obj,
Ctx: ctx,
}
reader, closers, err := proxy(link)
ss, err := stream.NewSeekableStream(fileStream, 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) {
Expand All @@ -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()
}

Expand Down

0 comments on commit b72e85a

Please sign in to comment.