fix download with etag
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package db_updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
@@ -12,11 +13,13 @@ import (
|
||||
func download(url string) bool {
|
||||
log.Info().Str("url", url).Msg("Downloading DB")
|
||||
filename := configs.GeoDbFolder + path.Base(url)
|
||||
filenameEtag := filename + ".etag"
|
||||
|
||||
etag, _ := readFile(filenameEtag)
|
||||
|
||||
client := resty.New()
|
||||
resp, err := client.R().
|
||||
SetOutput(filename).
|
||||
SetHeader("If-None-Match", readETag(filename)).
|
||||
SetHeader("If-None-Match", etag).
|
||||
Get(url)
|
||||
|
||||
if err != nil {
|
||||
@@ -26,32 +29,35 @@ func download(url string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if resp.StatusCode() == http.StatusOK {
|
||||
writeETag(filename, resp.Header().Get("Etag"))
|
||||
} else {
|
||||
//log.Printf("No update needed: %v", resp.Status())
|
||||
switch resp.StatusCode() {
|
||||
case http.StatusNotModified:
|
||||
fmt.Printf("No update needed: %v\n", resp.Status())
|
||||
return true
|
||||
case http.StatusOK:
|
||||
_ = writeFileSByte(filename, resp.Body())
|
||||
_ = writeFileString(filenameEtag, resp.Header().Get("Etag"))
|
||||
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("filename", filename).
|
||||
Msg("Downloaded DB")
|
||||
return true
|
||||
}
|
||||
|
||||
// readETag reads the ETag from the file
|
||||
func readETag(baseFile string) string {
|
||||
data, err := os.ReadFile(baseFile + ".etag")
|
||||
func readFile(file string) (string, error) {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return ""
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
return string(data)
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// writeETag writes the ETag to the file
|
||||
func writeETag(baseFile, etag string) {
|
||||
err := os.WriteFile(baseFile+".etag", []byte(etag), 0644)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to write ETag")
|
||||
func writeFileString(file, content string) error {
|
||||
return writeFileSByte(file, []byte(content))
|
||||
}
|
||||
|
||||
func writeFileSByte(file string, content []byte) error {
|
||||
return os.WriteFile(file, content, 0644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user