forked from yavdr/vdr-plugin-restfulapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletedrecordings.cpp
More file actions
128 lines (99 loc) · 3.62 KB
/
Copy pathdeletedrecordings.cpp
File metadata and controls
128 lines (99 loc) · 3.62 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "deletedrecordings.h"
using namespace std;
namespace {
bool hasDeletedRecordingExtension(const string& path)
{
return path.length() >= 4 && path.compare(path.length() - 4, 4, ".del") == 0;
}
}
void DeletedRecordingsResponder::reply(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler::addHeader(reply);
if (request.method() == "OPTIONS") {
reply.addHeader("Allow", "GET, POST");
reply.httpReturn(200, "OK");
return;
}
if ((int)request.url().find("/recordings/deleted/restore") == 0) {
if (request.method() == "POST") {
restoreDeletedRecording(out, request, reply);
} else {
reply.httpReturn(501, "Only POST method is supported by the /recordings/deleted/restore service.");
}
return;
}
if ((int)request.url().find("/recordings/deleted") == 0) {
if (request.method() == "GET") {
showDeletedRecordings(out, request, reply);
} else {
reply.httpReturn(501, "Only GET method is supported by the /recordings/deleted service.");
}
return;
}
reply.httpReturn(403, "Service not found");
}
void DeletedRecordingsResponder::showDeletedRecordings(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler q("/recordings/deleted", request);
if (!q.isFormat(".json")) {
reply.httpReturn(502, "Resources are only available as .json for the /recordings/deleted service.");
return;
}
#if VDRVERSNUM >= 20708
bool read_marks = q.getOptionAsString("marks") == "true";
int start_filter = q.getOptionAsInt("start");
int limit_filter = q.getOptionAsInt("limit");
reply.addHeader("Content-Type", CT_JSON);
RecordingList* recordingList = (RecordingList*)new JsonRecordingList(&out, read_marks);
if (start_filter >= 0 && limit_filter >= 1) {
recordingList->activateLimit(start_filter, limit_filter);
}
recordingList->init();
LOCK_DELETEDRECORDINGS_READ;
const cRecordings& deletedRecordings = *DeletedRecordings;
for (int i = 0; i < deletedRecordings.Count(); i++) {
recordingList->addRecording(deletedRecordings.Get(i), i, NULL, "");
}
recordingList->setTotal(deletedRecordings.Count());
recordingList->finish();
delete recordingList;
#else
reply.httpReturn(501, "Deleted recordings require VDR 2.7.8 or newer.");
#endif
}
void DeletedRecordingsResponder::restoreDeletedRecording(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler q("/recordings/deleted/restore", request);
string recordingFile = q.getBodyAsString("file");
reply.addHeader("Content-Type", "text/plain; charset=utf-8");
if (recordingFile.empty()) {
reply.httpReturn(400, "Missing file name!");
return;
}
if (!hasDeletedRecordingExtension(recordingFile)) {
reply.httpReturn(400, "Deleted recording path must end with .del!");
return;
}
#if VDRVERSNUM >= 20708
LOCK_RECORDINGS_WRITE;
LOCK_DELETEDRECORDINGS_WRITE;
cRecordings& recordings = *Recordings;
cRecordings& deletedRecordings = *DeletedRecordings;
cRecording* recording = deletedRecordings.GetByName(recordingFile.c_str());
if (recording == NULL) {
reply.httpReturn(404, "Deleted recording not found!");
return;
}
esyslog("restfulapi: restore deleted recording %s", recording->FileName());
if (!recording->Undelete()) {
reply.httpReturn(409, "Recording could not be restored!");
return;
}
deletedRecordings.Del(recording, false);
recordings.Add(recording);
cVideoDiskUsage::ForceCheck();
reply.httpReturn(200, "Recording restored!");
#else
reply.httpReturn(501, "Restoring deleted recordings requires VDR 2.7.8 or newer.");
#endif
}