Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix names #36

Merged
merged 4 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
matrix:
go-version: [1.17.x]
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
Expand Down
1 change: 1 addition & 0 deletions cmd/got/variables_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package main
Expand Down
1 change: 1 addition & 0 deletions cmd/got/variables_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package main
Expand Down
24 changes: 12 additions & 12 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type (

path string

unsafeName string

ctx context.Context

size, lastSize uint64
Expand Down Expand Up @@ -84,6 +86,9 @@ func (d *Download) GetInfoOrDownload() (*Info, error) {
return &Info{}, fmt.Errorf("Response status code is not ok: %d", res.StatusCode)
}

// Set content disposition non trusted name
d.unsafeName = res.Header.Get("content-disposition")

if dest, err = os.Create(d.Path()); err != nil {
return &Info{}, err
}
Expand All @@ -99,8 +104,7 @@ func (d *Download) GetInfoOrDownload() (*Info, error) {
l := strings.Split(cr, "/")
if len(l) == 2 {
if length, err := strconv.ParseUint(l[1], 10, 64); err == nil {
// Update the filename if needed
d.updatePath(getNameFromHeader(res.Header.Get("content-disposition")))

return &Info{
Size: length,
Rangeable: true,
Expand Down Expand Up @@ -325,25 +329,21 @@ func (d *Download) dl(dest io.WriterAt, errC chan error) {

// Return constant path which will not change once the download starts
func (d *Download) Path() string {

// Set the default path
if d.path == "" {
// Set the default path

if d.Dest != "" {
d.path = d.Dest
} else if d.unsafeName != "" {
d.path = getNameFromHeader(d.unsafeName)
} else {
d.path = GetFilename(d.URL)
}
d.path = filepath.Join(d.Dir, d.path)
}
return d.path
}

// Update the path onle before the download has started
func (d *Download) updatePath(name string) {
if d.Dest == "" && name != "" {
// Update only if the name is valid and the path
// hasn't been set to dest before, which has higher priority
d.path = filepath.Join(d.Dir, name)
}
return d.path
}

// DownloadChunk downloads a file chunk.
Expand Down