summaryrefslogtreecommitdiff
path: root/doClean.go
blob: 8497d020200890aee142ae9d98f659ed8efcbb69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/protobuf/chatpb"
	"go.wit.com/log"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func doClean() {
	log.Info("find all files")
	scanTmp()
}

func scanTmp() {
	var count int
	filepath.WalkDir("/tmp", func(path string, d os.DirEntry, err error) error {
		if err != nil {
			// Handle possible errors, like permission issues
			// fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
			// ignore all these problems
			return err
		}

		/*
			if d.IsDir() {
				// log.Info("path is dir", path)
				return nil
			}
		*/

		_, fname := filepath.Split(path)
		if !strings.HasPrefix(fname, "regex.") {
			return nil
		}
		if count > 100 {
			return log.Errorf("count exceeded")
		}
		if strings.HasPrefix(fname, "regex.gemini-api-response") {
			// log.Info("response file:", fname)
			return nil
		}
		if strings.Contains(fname, "gemini-api-request") {
			// log.Info("response file:", fname)
			if err := cleanGeminiFile(path); err == nil {
				count += 1
				return nil
			} else {
				return nil
			}
		}
		if strings.HasSuffix(fname, ".stats") {
			cleanStatsFile(path)
			return nil
		}
		log.Info("check file:", path)
		return nil
	})
}

func cleanStatsFile(fullname string) {
	log.Info("stats file", fullname)
}

func cleanGeminiFile(fullname string) error {
	_, fname := filepath.Split(fullname)
	if !strings.HasSuffix(fname, ".json") {
		return log.Errorf("not really gemini-api-request .json")
	}
	parts := strings.Split(fname, ".")
	if len(parts) == 5 {
		if parts[2] != "gemini-api-request" {
			return log.Errorf("not really gemini-api-request")
		}
	}
	// log.Info("PARSE FILE:", fullname)
	pb, err := parsePB(fullname)
	if err != nil {
		log.Info("parsePB() %s err %v\n", fullname, err)
		return log.Errorf("parsePB() %s err %v", fullname, err)
	}
	if pb == nil {
		log.Info("parsePB() == nil\n")
		return log.Errorf("parsePB() == nil")
	}
	uuid := parts[1]
	if argv.Clean.Match != "" {
		if !strings.HasPrefix(uuid, argv.Clean.Match) {
			return log.Errorf("uuid %s does not match %s", uuid, argv.Clean.Match)
		}
	}
	if chat := me.chats.FindUuid(uuid); chat != nil {
		log.Info("found uuid in chat", uuid, pb.Model, chat.Uuid)
		statf, err := os.Stat(fullname)
		if err == nil {
			age := statf.ModTime()
			if chat.AddGeminiRequest(fname, age, pb) {
				me.chats.ConfigSave()
			} else {
				log.Info("file was perfect. os.Remove() here", fullname)
				os.Remove(fullname)

			}
		}
		return nil
	}
	id := parts[3]
	if id == "1" {
		log.Info("Insert new chat here", fullname, id)
		statf, err := os.Stat(fullname)
		if err == nil {
			age := statf.ModTime()
			c := new(chatpb.Chat)
			c.Ctime = timestamppb.New(age)
			c.Uuid = uuid
			c.ChatName = "auto clean"
			log.Info("new chat:", c.Uuid, c.Ctime)
			me.chats.Append(c)
			me.chats.ConfigSave()
			okExit("")
		}
	}
	log.Info("gemini JSON file uuid not found", fullname, id)
	return log.Errorf("gemini JSON file uuid %s not found", uuid)
}