Fix file closing

This commit is contained in:
Timmy Welch 2024-10-31 21:12:53 -05:00
parent 840e97cff0
commit 7ede0dee72
2 changed files with 13 additions and 5 deletions

View File

@ -538,10 +538,10 @@ func (s *Server) reader(workerID int, done func(i int)) {
panic(err)
}
i, format, err := image.Decode(bufio.NewReader(file))
file.Close()
if err != nil {
continue // skip this image
}
file.Close()
im := ch.Im{
Im: i,
@ -759,16 +759,21 @@ func downloadProcessor(opts Opts, imagePaths chan cv.Download, server Server) {
continue
}
file, err := os.Open(path.Dest)
file, err := os.OpenFile(path.Dest, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
i, format, err := image.Decode(bufio.NewReader(file))
if err != nil {
file.Close()
continue // skip this image
}
if opts.truncateHashedImages {
file.Truncate(0)
file.Seek(0, io.SeekStart)
err = file.Truncate(0)
if err != nil {
log.Printf("Failed to truncate %#v: %v", path.Dest, err)
}
}
file.Close()

View File

@ -272,6 +272,7 @@ func (c *CVDownloader) updateIssues() {
}
resp, err, cancelDownloadCTX := Get(c.Context, URI.String())
if err != nil {
_ = resp.Body.Close()
cancelDownloadCTX()
if retry(URI.String(), err) {
continue
@ -281,11 +282,13 @@ func (c *CVDownloader) updateIssues() {
if resp.StatusCode != 200 {
cancelDownloadCTX()
if retry(URI.String(), nil) {
_ = resp.Body.Close()
continue
}
log.Println("Failed to download this page, we'll wait for an hour to see if it clears up")
select {
case <-c.Context.Done(): // allows us to return immediately even during a timeout
_ = resp.Body.Close()
return
case <-time.After(1 * time.Hour):
}
@ -296,6 +299,8 @@ func (c *CVDownloader) updateIssues() {
}
body := io.TeeReader(resp.Body, file)
err = json.NewDecoder(bufio.NewReader(body)).Decode(issue)
_ = resp.Body.Close()
_ = file.Close()
if err != nil || issue.Offset != offset {
os.Remove(filepath.Join(c.JSONPath, "cv-"+strconv.Itoa(offset)+".json"))
cancelDownloadCTX()
@ -304,8 +309,6 @@ func (c *CVDownloader) updateIssues() {
}
return
}
_ = resp.Body.Close()
_ = file.Close()
cancelDownloadCTX()
if issue.NumberOfTotalResults > c.totalResults {
c.totalResults = issue.NumberOfTotalResults