Skip to content

Commit 36820ac

Browse files
authored
allow keeping the image files (#158)
1 parent 3335362 commit 36820ac

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

camerapp/imageserver/imageserver.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var (
6767
currentFilesLock sync.Mutex
6868
)
6969

70-
func handleImageFiles(dir string) {
70+
func handleImageFiles(dir string, keepFiles bool) {
7171
for {
7272
// Read the directory and look for new .jpg images
7373
direntries, err := ioutil.ReadDir(dir)
@@ -94,20 +94,22 @@ func handleImageFiles(dir string) {
9494
}
9595
currentFilesLock.Unlock()
9696
}
97-
// Check if an image should be deleted
98-
now := time.Now()
99-
currentFilesLock.Lock()
100-
for k, v := range currentFiles {
101-
if now.Sub(v.readTime) > MaxFileAge+MaxFileAgeGracePeriod {
102-
err = os.Remove(k)
103-
check(err)
104-
delete(currentFiles, k)
105-
if k == mostRecentFile {
106-
mostRecentFile = ""
97+
if !keepFiles {
98+
// Check if an image should be deleted
99+
now := time.Now()
100+
currentFilesLock.Lock()
101+
for k, v := range currentFiles {
102+
if now.Sub(v.readTime) > MaxFileAge+MaxFileAgeGracePeriod {
103+
err = os.Remove(path.Join(dir, k))
104+
check(err)
105+
delete(currentFiles, k)
106+
if k == mostRecentFile {
107+
mostRecentFile = ""
108+
}
107109
}
108110
}
111+
currentFilesLock.Unlock()
109112
}
110-
currentFilesLock.Unlock()
111113

112114
time.Sleep(imageReadInterval)
113115
}
@@ -119,12 +121,13 @@ func main() {
119121
// Fetch arguments from command line
120122
port := flag.Uint("p", 40002, "Server Port")
121123
dir := flag.String("d", ".", "Directory to serve images from")
124+
keepFiles := flag.Bool("keep", false, "Keep (do not delete) existing image files")
122125
flag.Parse()
123126

124127
udpConnection, err := appnet.ListenPort(uint16(*port))
125128
check(err)
126129

127-
go handleImageFiles(*dir)
130+
go handleImageFiles(*dir, *keepFiles)
128131

129132
receivePacketBuffer := make([]byte, 2500)
130133
sendPacketBuffer := make([]byte, 2500)

0 commit comments

Comments
 (0)