Fix download geo resources

This commit is contained in:
johnthecoderpro 2023-11-05 15:31:42 +08:00 committed by 世界
parent 11629a931b
commit 9f01d5c5b4
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -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,
@ -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
}
@ -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,
@ -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
}