| 274 | } |
| 275 | |
| 276 | func (pd *ParallelDownload) getOutputFile() (io.Writer, error) { |
| 277 | outputFile := pd.output |
| 278 | if outputFile != nil { |
| 279 | return outputFile, nil |
| 280 | } |
| 281 | if pd.filename == "" { |
| 282 | u, err := urlpkg.Parse(pd.url) |
| 283 | if err != nil { |
| 284 | panic(err) |
| 285 | } |
| 286 | paths := strings.Split(u.Path, "/") |
| 287 | for i := len(paths) - 1; i > 0; i-- { |
| 288 | if paths[i] != "" { |
| 289 | pd.filename = paths[i] |
| 290 | break |
| 291 | } |
| 292 | } |
| 293 | if pd.filename == "" { |
| 294 | pd.filename = "download" |
| 295 | } |
| 296 | } |
| 297 | if pd.client.outputDirectory != "" && !filepath.IsAbs(pd.filename) { |
| 298 | pd.filename = filepath.Join(pd.client.outputDirectory, pd.filename) |
| 299 | } |
| 300 | return os.OpenFile(pd.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, pd.perm) |
| 301 | } |