-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.js
155 lines (147 loc) · 5.94 KB
/
model.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
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
var MIME = require('./mime');
var RedisClient = require('redis-client-0.3.5');
var redis = RedisClient.createClient();
var FiletypeMapping = { mkv:'Video',
avi:'Video',
mpeg:'Video',
mpeg2:'Video',
mpg:'Video',
mp4:'Video',
divx:'Video',
xvid:'Video',
mp2:'Video',
wmv:'Video',
ogv:'Video',
mov:'Video',
mks:'Video',
asf:'Video',
svg:'Text', // iframe ;-)
nfo:'Text',
js:'Text',
css:'Text',
txt:'Text',
jpg:'Image',
jpeg:'Image',
bmp:'Image',
png:'Image',
ogg:'Audio',
mp3:'Audio',
wav:'Audio',
flac:'Audio',
wma:'Audio',
'3gp':'Audio',
aac:'Audio',
wma:'Audio',
flac:'Audio',
mka:'Audio'};
redis.select(1);
module.exports = {
putTrackers: function(infoHex, urls, cb) {
if (urls.length == 0) {
cb();
} else {
var url = urls.pop();
module.exports.putTracker(infoHex, url, function(error) {
if (error)
cb(error);
else
module.exports.putTrackers(infoHex, urls, cb);
});
}
},
putTracker: function(infoHex, url, cb) {
redis.sadd("t:" + infoHex, url, cb);
},
getTrackers: function(infoHex, cb) {
redis.smembers("t:" + infoHex, function(error, urls) {
if (error)
cb(error);
else if (!urls)
cb('Not found');
else
cb(null, urls);
});
},
putFileinfo: function(infoHex, list, cb) {
var data = JSON.stringify(list);
redis.set("f:" + infoHex, data, cb);
},
getFileinfo: function(infoHex, cb) {
redis.get("f:" + infoHex, function(error, data) {
if (error)
cb(error);
else if (!data)
cb('Not found');
else {
var list = JSON.parse(data.toString());
cb(null, list);
}
});
},
parseTreeByFiles: function( fileList, infohex ) {
var result = {};
var fSizeLabels = [ ' byte', 'KB', 'MB', 'GB', 'TB' ];
fileList.forEach(function(f) {
var path = f.name.split('/');
var fType = f.name.match(/\.([^.]+)$/gi).toString();
var fSize = parseInt(f.length);
for(i = 0; i < fSizeLabels.length; i += 1)
if ((fSize > 1024) && fSizeLabels.length >= i)
fSize = fSize/1024;
else {
fSize = (Math.round(fSize*100)/100)+''+fSizeLabels[i];
break;
}
fType = fType.substring(1,fType.length).toLowerCase();
fType = (FiletypeMapping[fType] || 'unknown');
var attrs = {'kind':fType,
'path':infohex+'/'+f.name,
'mime':MIME.fileType(f.name),
'type':'file',
'size':fSize }
result[path[0]] = (result[path[0]] || {});
if (path.length > 1) { // Directory
result[path[0]]['type'] = 'directory';
result[path[0]]['files'] = (result[path[0]]['files'] || {});
var scndPart = path.slice(1,path.size).join('/');
result[path[0]]['files'][scndPart] = (result[path[0]]['files'][scndPart] || {});
for(k in attrs)
result[path[0]]['files'][scndPart][k] = attrs[k];
} else { // File
for(k in attrs)
result[path[0]][k] = attrs[k];
}
});
return result;
},
getRecommendations: function(cb) {
function walkRecommendations(infoHexes, results, cb) {
if (infoHexes.length < 1)
cb(null, results);
else {
var infoHex = infoHexes.shift();
module.exports.getFileinfo(infoHex, function(error, fileinfo) {
if (fileinfo)
results.push({ infoHex: infoHex,
name: fileinfo.name.toString() || 'Torrent file'
});
walkRecommendations(infoHexes, results, cb);
});
}
}
redis.get("recommendations", function(error, data) {
var infoHexes;
try {
infoHexes = JSON.parse(data.toString());
} catch (e) {
error = e.message;
}
if (error)
cb(error);
else if (infoHexes && infoHexes.shift) {
walkRecommendations(infoHexes, [], cb);
} else
cb(null, []);
});
}
};