Skip to content

Commit

Permalink
feat(sftp): support range header
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Feb 20, 2023
1 parent 0d07d81 commit 95b3b87
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
30 changes: 30 additions & 0 deletions drivers/base/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package base

import (
"io"
"net/http"
"strconv"

"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/alist-org/alist/v3/pkg/utils"
)

func HandleRange(link *model.Link, file io.ReadSeekCloser, header http.Header, size int64) {
if header.Get("Range") != "" {
r, err := http_range.ParseRange(header.Get("Range"), size)
if err == nil && len(r) > 0 {
_, err := file.Seek(r[0].Start, io.SeekStart)
if err == nil {
link.Data = utils.NewLimitReadCloser(file, func() error {
return file.Close()
}, r[0].Length)
link.Status = http.StatusPartialContent
link.Header = http.Header{
"Content-Range": []string{r[0].ContentRange(size)},
"Content-Length": []string{strconv.FormatInt(r[0].Length, 10)},
}
}
}
}
}
7 changes: 5 additions & 2 deletions drivers/sftp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
Expand Down Expand Up @@ -52,9 +53,11 @@ func (d *SFTP) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*
if err != nil {
return nil, err
}
return &model.Link{
link := &model.Link{
Data: remoteFile,
}, nil
}
base.HandleRange(link, remoteFile, args.Header, file.GetSize())
return link, nil
}

func (d *SFTP) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
Expand Down
22 changes: 2 additions & 20 deletions drivers/smb/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package smb
import (
"context"
"errors"
"io"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/alist-org/alist/v3/pkg/utils"

"github.com/hirochachacha/go-smb2"
Expand Down Expand Up @@ -86,22 +83,7 @@ func (d *SMB) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*m
link := &model.Link{
Data: remoteFile,
}
if args.Header.Get("Range") != "" {
r, err := http_range.ParseRange(args.Header.Get("Range"), file.GetSize())
if err == nil && len(r) > 0 {
_, err := remoteFile.Seek(r[0].Start, io.SeekStart)
if err == nil {
link.Data = utils.NewLimitReadCloser(remoteFile, func() error {
return remoteFile.Close()
}, r[0].Length)
link.Status = 206
link.Header = http.Header{
"Content-Range": []string{r[0].ContentRange(file.GetSize())},
"Content-Length": []string{strconv.FormatInt(r[0].Length, 10)},
}
}
}
}
base.HandleRange(link, remoteFile, args.Header, file.GetSize())
d.updateLastConnTime()
return link, nil
}
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5AU=
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 h1:gtexQ/VGyN+VVFRXSFiguSNcXmS6rkKT+X7FdIrTtfo=
Expand Down

0 comments on commit 95b3b87

Please sign in to comment.