1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-24 17:44:27 +00:00

Fix to close opened io resources as soon as not needed (#8839) (#8846)

* Fix to close opened io resources as soon as not needed

* Remove unneeded err checks
This commit is contained in:
Lauris BH 2019-11-05 22:14:56 +02:00 committed by GitHub
parent 26457782c1
commit 68b7f9f3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -252,27 +252,30 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
} }
// download attachment // download attachment
resp, err := http.Get(asset.URL) err = func() error {
resp, err := http.Get(asset.URL)
if err != nil {
return err
}
defer resp.Body.Close()
localPath := attach.LocalPath()
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
return fmt.Errorf("MkdirAll: %v", err)
}
fw, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("Create: %v", err)
}
defer fw.Close()
_, err = io.Copy(fw, resp.Body)
return err
}()
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close()
localPath := attach.LocalPath()
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
return fmt.Errorf("MkdirAll: %v", err)
}
fw, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("Create: %v", err)
}
defer fw.Close()
if _, err := io.Copy(fw, resp.Body); err != nil {
return err
}
rel.Attachments = append(rel.Attachments, &attach) rel.Attachments = append(rel.Attachments, &attach)
} }
@ -468,21 +471,24 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
} }
// download patch file // download patch file
resp, err := http.Get(pr.PatchURL) err := func() error {
if err != nil { resp, err := http.Get(pr.PatchURL)
return nil, err if err != nil {
} return err
defer resp.Body.Close() }
pullDir := filepath.Join(g.repo.RepoPath(), "pulls") defer resp.Body.Close()
if err = os.MkdirAll(pullDir, os.ModePerm); err != nil { pullDir := filepath.Join(g.repo.RepoPath(), "pulls")
return nil, err if err = os.MkdirAll(pullDir, os.ModePerm); err != nil {
} return err
f, err := os.Create(filepath.Join(pullDir, fmt.Sprintf("%d.patch", pr.Number))) }
if err != nil { f, err := os.Create(filepath.Join(pullDir, fmt.Sprintf("%d.patch", pr.Number)))
return nil, err if err != nil {
} return err
defer f.Close() }
_, err = io.Copy(f, resp.Body) defer f.Close()
_, err = io.Copy(f, resp.Body)
return err
}()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -496,8 +502,8 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer p.Close()
_, err = p.WriteString(pr.Head.SHA) _, err = p.WriteString(pr.Head.SHA)
p.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -531,8 +537,8 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer b.Close()
_, err = b.WriteString(pr.Head.SHA) _, err = b.WriteString(pr.Head.SHA)
b.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }