chore: support ETag for update geo

This commit is contained in:
wwqgtxx
2024-09-22 13:57:57 +08:00
parent 5d242510c8
commit b7cb6774bf
11 changed files with 117 additions and 51 deletions

View File

@@ -3,7 +3,6 @@ package resource
import (
"context"
"os"
"path/filepath"
"time"
types "github.com/metacubex/mihomo/constant/provider"
@@ -13,11 +12,6 @@ import (
"github.com/samber/lo"
)
var (
fileMode os.FileMode = 0o666
dirMode os.FileMode = 0o755
)
type Parser[V any] func([]byte) (V, error)
type Fetcher[V any] struct {
@@ -118,7 +112,7 @@ func (f *Fetcher[V]) loadBuf(buf []byte, hash types.HashType, updateFile bool) (
}
if updateFile {
if err = safeWrite(f.vehicle.Path(), buf); err != nil {
if err = f.vehicle.Write(buf); err != nil {
return lo.Empty[V](), false, err
}
}
@@ -205,18 +199,6 @@ func (f *Fetcher[V]) updateWithLog() {
return
}
func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, dirMode); err != nil {
return err
}
}
return os.WriteFile(path, buf, fileMode)
}
func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
ctx, cancel := context.WithCancel(context.Background())
return &Fetcher[V]{