forked from nestorInc/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_api.rb
261 lines (240 loc) · 8.94 KB
/
json_api.rb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env ruby
require 'json'
require 'rev'
require 'http.rb'
require 'display.rb'
require 'upload.rb'
require 'fileutils'
class JsonManager < HttpNode
MSG_LVL_DEBUG = 1
MSG_LVL_INFO = 2
MSG_LVL_WARNING = 3
MSG_LVL_ERROR = 4
MSG_LVL_FATAL = 5
def initialize(list, library, conf_upload, conf_encode)
@list = list;
@library = library;
@upload_dir = conf_upload && conf_upload["dst_folder"] || "uploads";
@source_dir = conf_encode && conf_encode["source_dir"] || "../../musik/sorted/";
super();
end
def on_request(s, req)
ch = @list[s.user];
rep = HttpResponse.new(req.proto, 200, "OK");
res = "";
#debug("IN: " + req.data);
if(ch == nil)
res = JsonManager.create_message(JsonManager::MSG_LVL_WARNING,
"Unknown channel #{s.user}");
else
res = parse(req.data, ch, s.user, s.sid);
end
rep.setData(res, "application/json");
s.write(rep.to_s);
end
def JsonManager.add_message(resp, lvl, code, msg)
msg = {
:level => lvl,
:message => msg
};
msg[:code] = code if(code);
resp[:messages] ||= [];
resp[:messages].push(msg);
end
def JsonManager.create_message(lvl, code = nil, msg)
resp = {};
JsonManager.add_message(resp, lvl, code, msg);
str = JSON.generate(resp);
#debug("OUT: " + str);
str;
end
private
def parse(req, ch, user, sid)
resp = { :timestamp => Time.now.to_i() };
if(req == nil)
JsonManager.add_message(resp, MSG_LVL_ERROR, "JSON request not found", "Json request not found");
else
begin
json = JSON.parse(req);
timestamp = json.delete("timestamp") || 0;
json.each { |type, value|
case(type)
when "search"
parse_search(resp, value);
when "action"
parse_action(resp, ch, user, value, sid);
else
JsonManager.add_message(resp, MSG_LVL_ERROR, "unknown command #{type}", "Unknown command #{type}");
end
}
# refresh
resp[:channel_infos] = ch.to_client();
resp[:current_song] = ch.getCurrentSongInfo();
if(timestamp <= ch.queue.timestamp)
resp[:play_queue] = ch.queue.to_client(@library);
end
rescue JSON::ParserError => e
JsonManager.add_message(resp, MSG_LVL_ERROR, "fail to parse request", "Exception when parsing json request, #{e}");
error("Exception when parsing json request, #{e}");
end
end
str = JSON.generate(resp);
#debug("OUT: " + str);
str;
end
def forward_action(resp, req, ch, user, sid)
resp ||= {};
resp[:timestamp] = Time.now.to_i();
case(req["name"])
when "next"
ch.next();
when "previous"
ch.previous();
when "add_to_play_queue"
ch.queue.add(req["play_queue_index"], req["mid"])
when "shuffle_play_queue"
ch.queue.shuffle();
when "get_user_informations"
result = @library.get_user_informations( sid )
if( result != nil )
resp [:account] = {
:nickname => user,
:token => result[0],
:home => result[1],
:sid => sid,
:user_agent => result[2],
:ip => result[3]
};
else
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "Cannot retrieve personnal informations for user :#{user}, token :#{sid}");
end
when "change_user_password"
result = @library.change_user_password( user, sid, req["nickname"], req["old_password"], req["new_password"], req["new_password2"] )
if( result )
JsonManager.add_message(resp, MSG_LVL_INFO, nil, "#{user}'s password successfully changed");
else
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "You cannot change #{user}'s password");
end
when "create_user"
res=nil
[email protected]_new_user( req["nickname"], req["password"], 0 ) if(req["nickname"] != "")
if(res==nil)
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "user #{req["nickname"]} not created (already exists or invalid)");
else
resp[:account_created]={}
JsonManager.add_message(resp, MSG_LVL_INFO, nil, "user #{req["nickname"]} created (but not validated)");
JsonManager.add_message(resp, MSG_LVL_INFO, nil, "Wait the administrator validation");
end
when "validate_user"
@library.validate_user( req["nickname"] )
JsonManager.add_message(resp, MSG_LVL_INFO, nil, "user #{req["nickname"]} validated");
when "add_search_to_play_queue"
result = @library.secure_request("mid",
CGI::unescape(req["search_value"]),
req["search_comparison"],
req["search_field"],
req["order_by"],
req["first_result"],
req["result_count"]);
if(req["play_queue_position"] == "head")
ch.queue.add(0, result);
elsif( req["play_queue_position"] == "tail" )
ch.queue.add(nil, result);
else # randomly
result.each { |song|
ch.queue.add(rand(ch.queue.list.length-1) , song);
}
end
when "remove_from_play_queue"
ch.queue.del(req["play_queue_index"]);
when "move_in_play_queue"
ch.queue.move(req["play_queue_index"], req["new_play_queue_index"]);
when "get_uploaded_files"
#TODO only if https sessions or send a 403
files = UploadManager.getUploadedFiles(@upload_dir, user);
resp [:uploaded_files] = {
:nb_files => files.size,
:files => files
};
when "update_uploaded_file"
action_response = UploadManager.updateUploadedFiles(@upload_dir, user,
req, resp);
resp [:uploaded_files] = {
:action_response => action_response
};
when "validate_uploaded_file"
action_response = UploadManager.validateUploadedFiles(@source_dir, @upload_dir, user,
req, resp);
resp [:uploaded_files] = {
:action_response => action_response
};
when "delete_uploaded_file"
action_response = UploadManager.deleteUploadedFiles(@upload_dir, user,
req, resp);
resp [:uploaded_files] = {
:action_response => action_response
};
when "select_plugin"
ch.set_plugin(req["plugin_name"]);
else
error("Unknown action #{req["name"]}", true, $error_file);
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "Unknown action #{req["name"]}");
end
end
def parse_action(resp, ch, user, req, sid)
begin
if( req.kind_of?(Array) )
req.each { |currentAction|
# Warning multi action should merge responses
forward_action(resp, currentAction, ch, user, sid );
}
else
forward_action(resp, req, ch, user, sid);
end
rescue => e
error("Error when parsing action query #{e.backtrace}: #{e.message} (#{e.class})");
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "Error when parsing action query");
end
end
def parse_search(resp, req)
begin
result = @library.secure_request(req["select_fields"],
CGI::unescapeHTML(req["search_value"]),
req["search_comparison"],
req["search_field"],
req["order_by"],
req["first_result"],
req["result_count"]);
songs = result.map { |song|
{
:mid => song.mid,
:artist => song.artist,
:title => song.title,
:album => song.album,
:track => song.track,
:genre => song.genre,
:duration => song.duration
}
}
resp [:search_results] = {
:identifier => req["identifier"],
:select => req["select"],
:select_fields => req["select_fields"],
:search_value => CGI::unescapeHTML(req["search_value"]),
:search_comparison => req["search_comparison"],
:search_field => req["search_field"],
:result_count => req["result_count"],
:order_by => req["order_by"],
:first_result => req["first_result"],
:total_results => @library.get_total(req["search_field"],
req["search_comparison"],
req["search_value"]),
:results => songs
};
rescue => e
error("Error when parsing search query #{e.backtrace}: #{e.message} (#{e.class})");
JsonManager.add_message(resp, MSG_LVL_ERROR, nil, "Error when parsing search query");
resp;
end
end
end