niujiuru 1 miesiąc temu
rodzic
commit
85b8e6d3f3
1 zmienionych plików z 17 dodań i 1 usunięć
  1. 17 1
      mqtt_reporter/photo.go

+ 17 - 1
mqtt_reporter/photo.go

@@ -88,6 +88,22 @@ func (r *MQTTReporter) uploadPhotoToServers(imgFile string) {
 	}
 }
 
+func (r *MQTTReporter) countOldPhotos(dir string) (int, error) {
+	files, err := os.ReadDir(dir)
+	if err != nil {
+		return 0, err
+	}
+
+	count := 0
+	for _, f := range files {
+		if !f.IsDir() && filepath.Ext(f.Name()) == ".jpg" {
+			count++
+		}
+	}
+
+	return count, nil
+}
+
 func (r *MQTTReporter) pruneOldPhotos(dir string, maxCount int) error {
 	files, err := os.ReadDir(dir)
 	if err != nil {
@@ -102,7 +118,7 @@ func (r *MQTTReporter) pruneOldPhotos(dir string, maxCount int) error {
 	var jpgFiles []fileInfo
 
 	for _, f := range files { // 遍历目录, 找出所有jpg文件
-		if !f.IsDir() && filepath.Ext(f.Name()) != ".jpg" {
+		if f.IsDir() || filepath.Ext(f.Name()) != ".jpg" {
 			continue
 		}
 		info, err := f.Info()