Skip to content

Commit 8140af0

Browse files
committed
Fix download geo resources
1 parent 98bf696 commit 8140af0

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

route/router_geo_resources.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ func (r *Router) prepareGeoIPDatabase() error {
5252
geoPath = foundPath
5353
}
5454
}
55-
geoPath = filemanager.BasePath(r.ctx, geoPath)
56-
if rw.FileExists(geoPath) {
57-
geoReader, codes, err := geoip.Open(geoPath)
58-
if err == nil {
59-
r.logger.Info("loaded geoip database: ", len(codes), " codes")
60-
r.geoIPReader = geoReader
61-
return nil
55+
if !rw.FileExists(geoPath) {
56+
geoPath = filemanager.BasePath(r.ctx, geoPath)
57+
}
58+
if stat, err := os.Stat(geoPath); err == nil {
59+
if stat.IsDir() {
60+
return E.New("geoip path is a directory: ", geoPath)
61+
}
62+
if stat.Size() == 0 {
63+
os.Remove(geoPath)
6264
}
6365
}
6466
if !rw.FileExists(geoPath) {
@@ -96,7 +98,17 @@ func (r *Router) prepareGeositeDatabase() error {
9698
geoPath = foundPath
9799
}
98100
}
99-
geoPath = filemanager.BasePath(r.ctx, geoPath)
101+
if !rw.FileExists(geoPath) {
102+
geoPath = filemanager.BasePath(r.ctx, geoPath)
103+
}
104+
if stat, err := os.Stat(geoPath); err == nil {
105+
if stat.IsDir() {
106+
return E.New("geoip path is a directory: ", geoPath)
107+
}
108+
if stat.Size() == 0 {
109+
os.Remove(geoPath)
110+
}
111+
}
100112
if !rw.FileExists(geoPath) {
101113
r.logger.Warn("geosite database not exists: ", geoPath)
102114
var err error
@@ -107,7 +119,6 @@ func (r *Router) prepareGeositeDatabase() error {
107119
}
108120
r.logger.Error("download geosite database: ", err)
109121
os.Remove(geoPath)
110-
// time.Sleep(10 * time.Second)
111122
}
112123
if err != nil {
113124
return err

0 commit comments

Comments
 (0)