patch.go 685 B

123456789101112131415161718192021222324252627282930313233343536
  1. package resource
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. types "github.com/metacubex/mihomo/constant/provider"
  6. "github.com/samber/lo"
  7. "os"
  8. "time"
  9. )
  10. func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
  11. now := time.Now()
  12. hash := md5.Sum(buf)
  13. if bytes.Equal(f.hash[:], hash[:]) {
  14. f.UpdatedAt = now
  15. _ = os.Chtimes(f.vehicle.Path(), now, now)
  16. return lo.Empty[V](), true, nil
  17. }
  18. contents, err := f.parser(buf)
  19. if err != nil {
  20. return lo.Empty[V](), false, err
  21. }
  22. if f.vehicle.Type() != types.File {
  23. if err := safeWrite(f.vehicle.Path(), buf); err != nil {
  24. return lo.Empty[V](), false, err
  25. }
  26. }
  27. f.UpdatedAt = now
  28. f.hash = hash
  29. return contents, false, nil
  30. }