Improvements

master
Jan-Lukas Else 2020-04-20 23:04:01 +02:00
parent 580724e9f9
commit 8a72be6fc2
6 changed files with 10 additions and 13 deletions

2
cdn.go
View File

@ -18,7 +18,7 @@ type BunnyCdn struct {
var bunnyCdnUrl = "https://bunnycdn.com" var bunnyCdnUrl = "https://bunnycdn.com"
func (cdn *BunnyCdn) Purge(url string) { func (cdn *BunnyCdn) Purge(url string) {
client := &http.Client{} client := http.DefaultClient
req, _ := http.NewRequest("POST", bunnyCdnUrl+"/api/purge?url="+url, nil) req, _ := http.NewRequest("POST", bunnyCdnUrl+"/api/purge?url="+url, nil)
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")

View File

@ -51,6 +51,7 @@ func (t *Tinify) Compress(url string) (location string, err error) {
return return
} }
defer func() { defer func() {
_ = file.Close()
_ = os.Remove(file.Name()) _ = os.Remove(file.Name())
}() }()
e = s.ToFile(file.Name()) e = s.ToFile(file.Name())

View File

@ -40,6 +40,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Failed to get file")) _, _ = w.Write([]byte("Failed to get file"))
return return
} }
defer func() { _ = file.Close() }()
hashFile, _, _ := r.FormFile("file") hashFile, _, _ := r.FormFile("file")
defer func() { _ = hashFile.Close() }() defer func() { _ = hashFile.Close() }()
fileName, err := getSHA256(hashFile) fileName, err := getSHA256(hashFile)

View File

@ -27,7 +27,7 @@ type BunnyCdnStorage struct {
var bunnyCdnStorageUrl = "https://storage.bunnycdn.com" var bunnyCdnStorageUrl = "https://storage.bunnycdn.com"
func (b *BunnyCdnStorage) Upload(fileName string, file multipart.File) (location string, err error) { func (b *BunnyCdnStorage) Upload(fileName string, file multipart.File) (location string, err error) {
client := &http.Client{} client := http.DefaultClient
req, _ := http.NewRequest(http.MethodPut, bunnyCdnStorageUrl+"/"+url.PathEscape(b.storageZoneName)+"/"+url.PathEscape("/"+fileName), file) req, _ := http.NewRequest(http.MethodPut, bunnyCdnStorageUrl+"/"+url.PathEscape(b.storageZoneName)+"/"+url.PathEscape("/"+fileName), file)
req.Header.Add("AccessKey", b.key) req.Header.Add("AccessKey", b.key)
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
) )
@ -34,7 +33,7 @@ func checkAccess(token string) (bool, error) {
return false, errors.New("token string is empty") return false, errors.New("token string is empty")
} }
// form the request to check the token // form the request to check the token
client := &http.Client{} client := http.DefaultClient
req, err := http.NewRequest("GET", indieAuthTokenUrl, nil) req, err := http.NewRequest("GET", indieAuthTokenUrl, nil)
if err != nil { if err != nil {
return false, errors.New("error making the request for checking token access") return false, errors.New("error making the request for checking token access")
@ -46,14 +45,10 @@ func checkAccess(token string) (bool, error) {
if err != nil { if err != nil {
return false, errors.New("error sending the request for checking token access") return false, errors.New("error sending the request for checking token access")
} }
defer res.Body.Close()
// parse the response // parse the response
body, err := ioutil.ReadAll(res.Body) indieAuthRes := &IndieAuthRes{}
if err != nil { err = json.NewDecoder(res.Body).Decode(&indieAuthRes)
return false, errors.New("error parsing the response for checking token access") res.Body.Close()
}
var indieAuthRes = new(IndieAuthRes)
err = json.Unmarshal(body, &indieAuthRes)
if err != nil { if err != nil {
return false, errors.New("Error parsing the response into json for checking token access " + err.Error()) return false, errors.New("Error parsing the response into json for checking token access " + err.Error())
} }

View File

@ -154,7 +154,7 @@ func HandleWebmention(w http.ResponseWriter, r *http.Request) {
} }
func responseCodeForSource(source string) (int, error) { func responseCodeForSource(source string) (int, error) {
client := &http.Client{} client := http.DefaultClient
resp, err := client.Get(source) resp, err := client.Get(source)
if err != nil || resp == nil { if err != nil || resp == nil {
return 0, err return 0, err