From 8140af01aa64de7c74ac9dfd3a0a1ddd71423921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 19 Jul 2023 21:01:06 +0800 Subject: [PATCH] Fix download geo resources --- route/router_geo_resources.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/route/router_geo_resources.go b/route/router_geo_resources.go index 55631724..d4b8d8b6 100644 --- a/route/router_geo_resources.go +++ b/route/router_geo_resources.go @@ -52,13 +52,15 @@ func (r *Router) prepareGeoIPDatabase() error { geoPath = foundPath } } - geoPath = filemanager.BasePath(r.ctx, geoPath) - if rw.FileExists(geoPath) { - geoReader, codes, err := geoip.Open(geoPath) - if err == nil { - r.logger.Info("loaded geoip database: ", len(codes), " codes") - r.geoIPReader = geoReader - return nil + if !rw.FileExists(geoPath) { + geoPath = filemanager.BasePath(r.ctx, geoPath) + } + if stat, err := os.Stat(geoPath); err == nil { + if stat.IsDir() { + return E.New("geoip path is a directory: ", geoPath) + } + if stat.Size() == 0 { + os.Remove(geoPath) } } if !rw.FileExists(geoPath) { @@ -96,7 +98,17 @@ func (r *Router) prepareGeositeDatabase() error { geoPath = foundPath } } - geoPath = filemanager.BasePath(r.ctx, geoPath) + if !rw.FileExists(geoPath) { + geoPath = filemanager.BasePath(r.ctx, geoPath) + } + if stat, err := os.Stat(geoPath); err == nil { + if stat.IsDir() { + return E.New("geoip path is a directory: ", geoPath) + } + if stat.Size() == 0 { + os.Remove(geoPath) + } + } if !rw.FileExists(geoPath) { r.logger.Warn("geosite database not exists: ", geoPath) var err error @@ -107,7 +119,6 @@ func (r *Router) prepareGeositeDatabase() error { } r.logger.Error("download geosite database: ", err) os.Remove(geoPath) - // time.Sleep(10 * time.Second) } if err != nil { return err