67
67
currentFilesLock sync.Mutex
68
68
)
69
69
70
- func handleImageFiles (dir string ) {
70
+ func handleImageFiles (dir string , keepFiles bool ) {
71
71
for {
72
72
// Read the directory and look for new .jpg images
73
73
direntries , err := ioutil .ReadDir (dir )
@@ -94,20 +94,22 @@ func handleImageFiles(dir string) {
94
94
}
95
95
currentFilesLock .Unlock ()
96
96
}
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
+ }
107
109
}
108
110
}
111
+ currentFilesLock .Unlock ()
109
112
}
110
- currentFilesLock .Unlock ()
111
113
112
114
time .Sleep (imageReadInterval )
113
115
}
@@ -119,12 +121,13 @@ func main() {
119
121
// Fetch arguments from command line
120
122
port := flag .Uint ("p" , 40002 , "Server Port" )
121
123
dir := flag .String ("d" , "." , "Directory to serve images from" )
124
+ keepFiles := flag .Bool ("keep" , false , "Keep (do not delete) existing image files" )
122
125
flag .Parse ()
123
126
124
127
udpConnection , err := appnet .ListenPort (uint16 (* port ))
125
128
check (err )
126
129
127
- go handleImageFiles (* dir )
130
+ go handleImageFiles (* dir , * keepFiles )
128
131
129
132
receivePacketBuffer := make ([]byte , 2500 )
130
133
sendPacketBuffer := make ([]byte , 2500 )
0 commit comments