Skip to content

Commit

Permalink
perf: fix some switch sftp downlaod failed
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Jan 8, 2025
1 parent f27a794 commit 6057656
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions pkg/httpd/sftpvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,30 @@ func (u *UserVolume) Parents(path string, dep int) []elfinder.FileDir {
func (u *UserVolume) GetFile(path string) (fileData elfinder.FileData, err error) {
logger.Debug("GetFile path: ", path)
var rest elfinder.FileData
sf, err := u.UserSftp.Open(filepath.Join(u.basePath, TrimPrefix(path)))
sfStatus, err := u.UserSftp.Stat(filepath.Join(u.basePath, TrimPrefix(path)))
if err != nil {
logger.Errorf("Get File path %s stat failed: %s", path, err)
return rest, err
}

fileInfo, err := sf.Stat()
sf, err := u.UserSftp.Open(filepath.Join(u.basePath, TrimPrefix(path)))
if err != nil {
return rest, err
}
go func() {
sf1, err := u.UserSftp.Open(filepath.Join(u.basePath, TrimPrefix(path)))
if err != nil {
logger.Errorf("Record file %s err: %s", path, err)
return
}
if err1 := u.recorder.ChunkedRecord(sf.FTPLog, sf1, 0, sfStatus.Size()); err1 != nil {
logger.Errorf("Record file err: %s", err1)
}
}()

if err1 := u.recorder.ChunkedRecord(sf.FTPLog, sf, 0, fileInfo.Size()); err1 != nil {
logger.Errorf("Record file err: %s", err1)
}
_, _ = sf.Seek(0, io.SeekStart)
// _, _ = sf.Seek(0, io.SeekStart)
// 屏蔽 sftp*File 的 WriteTo 方法,防止调用 sftp stat 命令
fileData = elfinder.FileData{Reader: sf, Size: fileInfo.Size()}
fileData = elfinder.FileData{Reader: &fileReader{read: sf}, Size: sfStatus.Size()}
return fileData, nil
}

Expand Down Expand Up @@ -528,3 +536,15 @@ func hashPath(id, path string) string {
func TrimPrefix(path string) string {
return strings.TrimPrefix(path, "/")
}

type fileReader struct {
read io.ReadCloser
}

func (f *fileReader) Read(p []byte) (nr int, err error) {
return f.read.Read(p)
}

func (f *fileReader) Close() error {
return f.read.Close()
}

0 comments on commit 6057656

Please sign in to comment.