Skip to content

Commit

Permalink
Fix download geo resources
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecoderpro authored and nekohasekai committed Nov 5, 2023
1 parent 11629a9 commit 9f01d5c
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions route/router_geo_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
defer saveFile.Close()

httpClient := &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
Expand All @@ -182,7 +176,16 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
return err
}
defer response.Body.Close()

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
_, err = io.Copy(saveFile, response.Body)
saveFile.Close()
if err != nil {
filemanager.Remove(r.ctx, savePath)
}
return err
}

Expand All @@ -209,12 +212,6 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
defer saveFile.Close()

httpClient := &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
Expand All @@ -234,7 +231,16 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
return err
}
defer response.Body.Close()

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
_, err = io.Copy(saveFile, response.Body)
saveFile.Close()
if err != nil {
filemanager.Remove(r.ctx, savePath)
}
return err
}

Expand Down

0 comments on commit 9f01d5c

Please sign in to comment.