-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
94 lines (77 loc) · 2.21 KB
/
util.js
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
'use strict';
var crypto = require('crypto');
var config = require('./musicConfig');
var cache = {};
const IndexProperty = 'INDEX';
const OffsetProperty = 'OFFSET';
function getSecure(url, expires, secret){
var input= expires + url + " " + secret;
var binaryHash = crypto.createHash("md5").update(input).digest();
var base64Value = new Buffer(binaryHash).toString('base64');
return base64Value.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g,'_');
}
function play(req, resp, index, offset, behavior){
var next= config.files[index].path;
var now = new Date();
var expires = new Date(now.getTime() + 35*60000);
expires = expires.getTime();
var md5=getSecure(next, expires,config.secret);
var stream = {
"url": config.serverURL + next + "?expires=" + expires + "&md5=" + md5,
"token": "token_"+index,
"offsetInMilliseconds": offset
};
resp.audioPlayerPlayStream(behavior, stream);
}
function getNext(device) {
var idx = getDeviceProperty(device,IndexProperty);
idx = idx + 1;
if(idx < config.files.length)
return idx;
return 0;
}
function getPrevious(device) {
var idx= getDeviceProperty(device,IndexProperty);
idx = idx - 1;
if(idx >= 0)
return idx;
return config.files.length -1;
}
function getIndex(device) {
return getDeviceProperty(device, IndexProperty);
}
function setIndex(device, i) {
setDeviceProperty(device,IndexProperty,i);
}
function getOffset(device) {
return getDeviceProperty(device,OffsetProperty);
}
function setOffset(device, offset) {
setDeviceProperty(device,OffsetProperty,offset);
}
function setDeviceProperty(device, property, value) {
var deviceData = cache[device];
if(!deviceData) {
deviceData={};
cache[device] = deviceData;
}
if(typeof value === "number") {
deviceData[property] = parseInt(value);
} else {
deviceData[property] = value;
}
}
function getDeviceProperty(device, property) {
var deviceData = cache[device];
if(!deviceData) return null;
return deviceData[property];
}
module.exports = {
play : play,
getNext : getNext,
getPrevious : getPrevious,
getOffset : getOffset,
setOffset : setOffset,
getIndex : getIndex,
setIndex : setIndex
}