Skip to content

Commit

Permalink
fix(local): filename with whitespace issue (#3928)
Browse files Browse the repository at this point in the history
* fix(local): filename whitespace problem

* fix(deps): remove deprecated package io/ioutil

---------

Co-authored-by: XZB <i@1248.ink>
  • Loading branch information
XZB-1248 and XZB authored Mar 23, 2023
1 parent c6af22b commit 0eab31b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
3 changes: 1 addition & 2 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
stdpath "path"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (d *Local) GetAddition() driver.Additional {

func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
fullPath := dir.GetPath()
rawFiles, err := ioutil.ReadDir(fullPath)
rawFiles, err := readDir(fullPath)
if err != nil {
return nil, err
}
Expand Down
18 changes: 17 additions & 1 deletion drivers/local/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package local
import (
"bytes"
"fmt"
ffmpeg "github.com/u2takey/ffmpeg-go"
"io/fs"
"os"
"path/filepath"
"sort"

ffmpeg "github.com/u2takey/ffmpeg-go"
)

func isSymlinkDir(f fs.FileInfo, path string) bool {
Expand Down Expand Up @@ -39,3 +41,17 @@ func GetSnapshot(videoPath string, frameNum int) (imgData *bytes.Buffer, err err
}
return srcBuf, nil
}

func readDir(dirname string) ([]fs.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
return list, nil
}
6 changes: 3 additions & 3 deletions pkg/aria2/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/base64"
"errors"
"io/ioutil"
"net/url"
"os"
"time"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *client) AddURI(uris []string, options ...interface{}) (gid string, err
// If a file with the same name already exists, it is overwritten!
// If the file cannot be saved successfully or --rpc-save-upload-metadata is false, the downloads added by this method are not saved by --save-session.
func (c *client) AddTorrent(filename string, options ...interface{}) (gid string, err error) {
co, err := ioutil.ReadFile(filename)
co, err := os.ReadFile(filename)
if err != nil {
return
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *client) AddTorrent(filename string, options ...interface{}) (gid string
// If a file with the same name already exists, it is overwritten!
// If the file cannot be saved successfully or --rpc-save-upload-metadata is false, the downloads added by this method are not saved by --save-session.
func (c *client) AddMetalink(filename string, options ...interface{}) (gid []string, err error) {
co, err := ioutil.ReadFile(filename)
co, err := os.ReadFile(filename)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions server/common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -61,7 +60,8 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.
if err != nil {
return err
}
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, file.GetName(), url.QueryEscape(file.GetName())))
filename := file.GetName()
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, filename, url.PathEscape(filename)))
http.ServeContent(w, r, file.GetName(), fileStat.ModTime(), f)
return nil
} else {
Expand Down Expand Up @@ -93,7 +93,7 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.
}
w.WriteHeader(res.StatusCode)
if res.StatusCode >= 400 {
all, _ := ioutil.ReadAll(res.Body)
all, _ := io.ReadAll(res.Body)
msg := string(all)
log.Debugln(msg)
return errors.New(msg)
Expand Down

0 comments on commit 0eab31b

Please sign in to comment.