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