Skip to content

Commit

Permalink
fix filename and remove updatePath that cause outputing two files.
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Oct 31, 2021
1 parent e30ed52 commit 31066a5
Showing 1 changed file with 12 additions and 12 deletions.
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

0 comments on commit 31066a5

Please sign in to comment.