Skip to content

Commit 5dcd1ca

Browse files
authored
Merge pull request #109 from TheCacophonyProject/update-api
update device api for metadata
2 parents 50775b5 + dbafe16 commit 5dcd1ca

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

api/api.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
153159
func (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)
@@ -389,21 +401,11 @@ func getCptvNames(dir string) []string {
389401
return names
390402
}
391403

392-
func getRecordingPath(cptv, dir string) string {
404+
func getRecordingPath(file, dir string) string {
393405
// Check that given file is a cptv file on the device.
394-
isCptvFile := false
395-
for _, name := range getCptvNames(dir) {
396-
if name == cptv {
397-
isCptvFile = true
398-
break
399-
}
400-
}
401-
if !isCptvFile {
402-
return ""
403-
}
404406
paths := []string{
405-
filepath.Join(dir, cptv),
406-
filepath.Join(dir, failedUploadsFolder, cptv),
407+
filepath.Join(dir, file),
408+
filepath.Join(dir, failedUploadsFolder, file),
407409
}
408410
for _, path := range paths {
409411
if _, err := os.Stat(path); !os.IsNotExist(err) {

0 commit comments

Comments
 (0)