Jelajahi Sumber

优化照片补录逻辑,加强容错性

niujiuru 2 minggu lalu
induk
melakukan
cd8b074f0c
2 mengubah file dengan 12 tambahan dan 7 penghapusan
  1. 0 6
      mqtt_reporter/photo.go
  2. 12 1
      mqtt_reporter/reporter.go

+ 0 - 6
mqtt_reporter/photo.go

@@ -157,12 +157,6 @@ func (r *MQTTReporter) pruneHistoryPhotos(dir string, maxCount int) error {
 			continue
 		}
 
-		path := filepath.Join(dir, f.Name())
-		if !strings.HasPrefix(f.Name(), "history-") {
-			os.Remove(path)
-			continue
-		}
-
 		info, err := f.Info()
 		if err != nil {
 			continue

+ 12 - 1
mqtt_reporter/reporter.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"io/fs"
+	"os"
 	"path/filepath"
 	"strings"
 	"sync"
@@ -162,12 +163,22 @@ func (r *MQTTReporter) startYFKJDataBackfill(server MQTTServer) {
 		baseapp.Logger.Infof("[%s] MQTT服务器 %s ++照片补录开始++", MODULE_NAME, server.Name)
 		nums2 := 0
 		filepath.WalkDir(baseapp.IMG_DIR, func(path string, d fs.DirEntry, walkErr error) error {
-			if walkErr != nil || d.IsDir() || filepath.Ext(path) != ".jpg" || !strings.HasPrefix(filepath.Base(path), "history-") {
+			if walkErr != nil || d.IsDir() || filepath.Ext(path) != ".jpg" {
 				return nil
 			} else if r.ctx.Err() != nil {
 				return context.Canceled
 			}
 
+			if !strings.HasPrefix(filepath.Base(path), "history-") {
+				newPath := filepath.Join(filepath.Dir(path), "history-"+filepath.Base(path))
+				if err := os.Rename(path, newPath); err != nil {
+					baseapp.Logger.Errorf("[%s] 本地照片文件重命名失败, 无法标记为补录照片: %s -> %s, 错误: %v!!",
+						MODULE_NAME, path, newPath, err)
+					return nil
+				}
+				path = newPath
+			}
+
 			r.uploadPhotoToImagServers(path, true) // 补录上传照片文件
 			nums2++