-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrecordingList.go
89 lines (72 loc) · 2 KB
/
recordingList.go
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/mux"
)
type RecordingList struct {
Folder string
}
func getFiles(folder string) []string {
files, err := os.ReadDir(folder)
if err != nil {
log.Fatal(err)
}
var recordings []string
for _, f := range files {
extension := filepath.Ext(strings.ToLower(f.Name()))
name := strings.TrimSuffix(filepath.Base(f.Name()), filepath.Ext(f.Name()))
if extension == ".jpg" {
s := strings.Split(f.Name(), "-")
newFolder := fmt.Sprintf("%s-%s/", s[0], s[1])
recordings = append(recordings, newFolder+name)
}
}
return recordings
}
func getAllFiles(folder string) []string {
files, err := os.ReadDir(folder)
if err != nil {
log.Fatal(err)
}
var recordings []string
for _, f := range files {
if f.IsDir() && f.Name() != "deleteme" && f.Name() != "raw" {
recordings = append(recordings, getFiles(folder+f.Name())...)
}
}
return recordings
}
func (rec *RecordingList) handleRecordingList(w http.ResponseWriter, r *http.Request) {
out, _ := json.Marshal(getAllFiles(rec.Folder))
w.Header().Set("Content-Type", "application/json")
w.Write(out)
}
func (rec *RecordingList) handleDeleteRecording(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
videoID := vars["videoID"]
os.MkdirAll(rec.Folder+"deleteme/", 0777)
s := strings.Split(videoID, "-")
newFolder := fmt.Sprintf("%s-%s/", s[0], s[1])
os.Rename(rec.Folder+newFolder+videoID+".mp4", rec.Folder+"deleteme/"+videoID+".mp4")
os.Rename(rec.Folder+newFolder+videoID+".jpg", rec.Folder+"deleteme/"+videoID+".jpg")
}
func (rec *RecordingList) handleDestroyRecording(w http.ResponseWriter, r *http.Request) {
files, err := os.ReadDir(rec.Folder + "deleteme/")
if err != nil {
return
}
for _, f := range files {
s := strings.Split(f.Name(), "_")
videoTime, _ := time.Parse("2006-01-02-1504", s[0])
if time.Now().Add(-time.Hour).After(videoTime) {
os.Remove(rec.Folder + "deleteme/" + f.Name())
}
}
}