Remove useless body discard

This commit is contained in:
Jan-Lukas Else 2021-11-02 18:38:08 +01:00
parent a3f2a5090b
commit 41511df0e5
11 changed files with 3 additions and 37 deletions

View File

@ -9,7 +9,6 @@ import (
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
@ -255,13 +254,11 @@ func (a *goBlog) apGetRemoteActor(iri string) (*asPerson, int, error) {
}
defer resp.Body.Close()
if !apRequestIsSuccess(resp.StatusCode) {
_, _ = io.Copy(io.Discard, resp.Body)
return nil, resp.StatusCode, nil
}
actor := &asPerson{}
err = json.NewDecoder(resp.Body).Decode(actor)
if err != nil {
_, _ = io.Copy(io.Discard, resp.Body)
return nil, 0, err
}
return actor, 0, nil

View File

@ -122,8 +122,6 @@ func (a *goBlog) apSendSigned(blogIri, to string, activity []byte) error {
if !apRequestIsSuccess(resp.StatusCode) {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("signed request failed with status %d: %s", resp.StatusCode, string(body))
} else {
_, _ = io.Copy(io.Discard, resp.Body)
}
return nil
}

View File

@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"sort"
@ -77,10 +76,7 @@ func (a *goBlog) getBlogrollOutlines(blog string) ([]*opml.Outline, error) {
if err != nil {
return nil, err
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
}()
defer res.Body.Close()
if code := res.StatusCode; code < 200 || 300 <= code {
return nil, fmt.Errorf("opml request not successful, status code: %d", code)
}

View File

@ -98,7 +98,6 @@ func (a *goBlog) checkLinks(w io.Writer, posts ...*post) error {
if err != nil {
return nil, err
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
// Cache result
sm.Store(link.Second, resp.StatusCode)

5
geo.go
View File

@ -55,10 +55,7 @@ func (a *goBlog) photonReverse(lat, lon float64, lang string) ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response status code: %v", resp.StatusCode)
}

View File

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"time"
@ -27,7 +26,6 @@ func (a *goBlog) healthcheck() bool {
return false
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
return resp.StatusCode == 200
}

View File

@ -81,12 +81,10 @@ func (sp *shortpixel) compress(url string, upload mediaStorageSaveFunc, hc httpC
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, resp.Body)
return "", fmt.Errorf("shortpixel failed to compress image, status code %d", resp.StatusCode)
}
tmpFile, err := os.CreateTemp("", "tiny-*."+fileExtension)
if err != nil {
_, _ = io.Copy(io.Discard, resp.Body)
return "", err
}
defer func() {
@ -94,7 +92,6 @@ func (sp *shortpixel) compress(url string, upload mediaStorageSaveFunc, hc httpC
_ = os.Remove(tmpFile.Name())
}()
if _, err = io.Copy(tmpFile, resp.Body); err != nil {
_, _ = io.Copy(io.Discard, resp.Body)
return "", err
}
fileName, err := getSHA256(tmpFile)
@ -133,7 +130,6 @@ func (tf *tinify) compress(url string, upload mediaStorageSaveFunc, hc httpClien
return "", err
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("failed to compress image, status code %d", resp.StatusCode)
}
@ -161,12 +157,10 @@ func (tf *tinify) compress(url string, upload mediaStorageSaveFunc, hc httpClien
}
defer downloadResp.Body.Close()
if downloadResp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, downloadResp.Body)
return "", fmt.Errorf("tinify failed to resize image, status code %d", downloadResp.StatusCode)
}
tmpFile, err := os.CreateTemp("", "tiny-*."+fileExtension)
if err != nil {
_, _ = io.Copy(io.Discard, downloadResp.Body)
return "", err
}
defer func() {
@ -174,7 +168,6 @@ func (tf *tinify) compress(url string, upload mediaStorageSaveFunc, hc httpClien
_ = os.Remove(tmpFile.Name())
}()
if _, err = io.Copy(tmpFile, downloadResp.Body); err != nil {
_, _ = io.Copy(io.Discard, downloadResp.Body)
return "", err
}
fileName, err := getSHA256(tmpFile)
@ -208,12 +201,10 @@ func (cf *cloudflare) compress(url string, upload mediaStorageSaveFunc, hc httpC
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, resp.Body)
return "", fmt.Errorf("cloudflare failed to compress image, status code %d", resp.StatusCode)
}
tmpFile, err := os.CreateTemp("", "tiny-*."+fileExtension)
if err != nil {
_, _ = io.Copy(io.Discard, resp.Body)
return "", err
}
defer func() {
@ -221,7 +212,6 @@ func (cf *cloudflare) compress(url string, upload mediaStorageSaveFunc, hc httpC
_ = os.Remove(tmpFile.Name())
}()
if _, err = io.Copy(tmpFile, resp.Body); err != nil {
_, _ = io.Copy(io.Discard, resp.Body)
return "", err
}
fileName, err := getSHA256(tmpFile)

View File

@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
@ -75,7 +74,6 @@ func (a *goBlog) send(tg *configTelegram, message, mode string) error {
return err
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send Telegram message, status code %d", resp.StatusCode)
}

5
tts.go
View File

@ -161,10 +161,7 @@ func (a *goBlog) downloadTTSAudio(ctx context.Context, lang, text, outputFile st
if err != nil {
return err
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("TTS: got status: %s, text: %s", res.Status, text)
}

View File

@ -84,7 +84,6 @@ func (a *goBlog) sendWebmention(endpoint, source, target string) error {
return err
}
defer res.Body.Close()
_, _ = io.Copy(io.Discard, res.Body)
if code := res.StatusCode; code < 200 || 300 <= code {
return fmt.Errorf("response error: %v", res.StatusCode)
}
@ -104,12 +103,10 @@ func (a *goBlog) discoverEndpoint(urlStr string) string {
}
defer resp.Body.Close()
if code := resp.StatusCode; code < 200 || 300 <= code {
_, _ = io.Copy(io.Discard, resp.Body)
return ""
}
endpoint, err := extractEndpoint(resp)
if err != nil || endpoint == "" {
_, _ = io.Copy(io.Discard, resp.Body)
return ""
}
if urls, err := resolveURLReferences(urlStr, endpoint); err == nil && len(urls) > 0 && urls[0] != "" {

View File

@ -96,7 +96,6 @@ func (a *goBlog) verifyMention(m *mention) error {
}
}
err = m.verifyReader(resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
if err != nil {
_, err := a.db.exec("delete from webmentions where source = @source and target = @target", sql.Named("source", m.Source), sql.Named("target", m.Target))