@@ -49,7 +49,7 @@ const (
4949 cptvGlob = "*.cptv"
5050 failedUploadsFolder = "failed-uploads"
5151 rebootDelay = time .Second * 5
52- apiVersion = 7
52+ apiVersion = 8
5353)
5454
5555type ManagementAPI struct {
@@ -132,12 +132,18 @@ func (api *ManagementAPI) GetRecording(w http.ResponseWriter, r *http.Request) {
132132 cptvPath := getRecordingPath (cptvName , api .cptvDir )
133133 if cptvPath == "" {
134134 w .WriteHeader (http .StatusBadRequest )
135- io .WriteString (w , "cptv file not found\n " )
135+ io .WriteString (w , "file not found\n " )
136136 return
137137 }
138138
139139 w .Header ().Set ("Content-Disposition" , fmt .Sprintf (`attachment; filename="%s"` , cptvName ))
140- w .Header ().Set ("Content-Type" , "application/x-cptv" )
140+
141+ ext := filepath .Ext (cptvName )
142+ if ext == ".cptv" {
143+ w .Header ().Set ("Content-Type" , "application/x-cptv" )
144+ } else {
145+ w .Header ().Set ("Content-Type" , "application/json" )
146+ }
141147 f , err := os .Open (cptvPath )
142148 if err != nil {
143149 w .WriteHeader (http .StatusInternalServerError )
@@ -152,13 +158,19 @@ func (api *ManagementAPI) GetRecording(w http.ResponseWriter, r *http.Request) {
152158// DeleteRecording deletes the given cptv file
153159func (api * ManagementAPI ) DeleteRecording (w http.ResponseWriter , r * http.Request ) {
154160 cptvName := mux .Vars (r )["id" ]
155- log .Printf ("delete cptv '%s'" , cptvName )
156161 recPath := getRecordingPath (cptvName , api .cptvDir )
157162 if recPath == "" {
158163 w .WriteHeader (http .StatusOK )
159164 io .WriteString (w , "cptv file not found\n " )
160165 return
161166 }
167+
168+ metaFile := strings .TrimSuffix (recPath , filepath .Ext (recPath )) + ".txt"
169+ if _ , err := os .Stat (metaFile ); ! os .IsNotExist (err ) {
170+ log .Printf ("deleting meta '%s'" , metaFile )
171+ os .Remove (metaFile )
172+ }
173+ log .Printf ("delete cptv '%s'" , recPath )
162174 err := os .Remove (recPath )
163175 if os .IsNotExist (err ) {
164176 w .WriteHeader (http .StatusOK )
@@ -189,6 +201,21 @@ func (api *ManagementAPI) TakeSnapshot(w http.ResponseWriter, r *http.Request) {
189201 }
190202}
191203
204+ // TakeSnapshotRecording will request a new snapshot recording to be taken by thermal-recorder
205+ func (api * ManagementAPI ) TakeSnapshotRecording (w http.ResponseWriter , r * http.Request ) {
206+ conn , err := dbus .SystemBus ()
207+ if err != nil {
208+ w .WriteHeader (http .StatusInternalServerError )
209+ return
210+ }
211+ recorder := conn .Object ("org.cacophony.thermalrecorder" , "/org/cacophony/thermalrecorder" )
212+ err = recorder .Call ("org.cacophony.thermalrecorder.TakeTestRecording" , 0 ).Err
213+ if err != nil {
214+ w .WriteHeader (http .StatusInternalServerError )
215+ return
216+ }
217+ }
218+
192219// Reregister can change the devices name and group
193220func (api * ManagementAPI ) Reregister (w http.ResponseWriter , r * http.Request ) {
194221 group := r .FormValue ("group" )
@@ -374,21 +401,11 @@ func getCptvNames(dir string) []string {
374401 return names
375402}
376403
377- func getRecordingPath (cptv , dir string ) string {
404+ func getRecordingPath (file , dir string ) string {
378405 // Check that given file is a cptv file on the device.
379- isCptvFile := false
380- for _ , name := range getCptvNames (dir ) {
381- if name == cptv {
382- isCptvFile = true
383- break
384- }
385- }
386- if ! isCptvFile {
387- return ""
388- }
389406 paths := []string {
390- filepath .Join (dir , cptv ),
391- filepath .Join (dir , failedUploadsFolder , cptv ),
407+ filepath .Join (dir , file ),
408+ filepath .Join (dir , failedUploadsFolder , file ),
392409 }
393410 for _ , path := range paths {
394411 if _ , err := os .Stat (path ); ! os .IsNotExist (err ) {
0 commit comments