Browse Source

slight refactor odl.go

master
Alexander Avery 3 months ago
parent
commit
c1cff821ec
  1. 14
      odl.go

14
odl.go

@ -43,7 +43,6 @@ func download(n int, root, name, uri string) error {
if err != nil {
return fmt.Errorf("parsing uri %s: %v", uri, err)
}
ext := filepath.Ext(u.Path)
response, err := http.Get(uri)
switch {
@ -51,14 +50,14 @@ func download(n int, root, name, uri string) error {
return fmt.Errorf("downloading file %s: %v", uri, err)
case response.StatusCode != http.StatusOK:
return fmt.Errorf("downloading file %s: status code %d", uri, response.StatusCode)
default:
defer response.Body.Close()
}
defer response.Body.Close()
var r io.Reader = response.Body
ext := filepath.Ext(u.Path)
if ext == "" {
var err error
ext, r, err = guessExt(response.Body)
ext, r, err = guessExt(r)
if err != nil {
return err
}
@ -66,11 +65,10 @@ func download(n int, root, name, uri string) error {
filename := filepath.Join(root, name+strconv.Itoa(n)+ext)
f, err := os.Create(filename)
if err != nil {
return err
if err == nil {
_, err = io.Copy(f, r)
}
_, err = io.Copy(f, r)
return err
}

Loading…
Cancel
Save