Skip to content

Commit c92520d

Browse files
committed
FIX: subtitles Binary safe
FIX: Upload for newer versions of node
1 parent 21563f5 commit c92520d

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ results
1414

1515
node_modules
1616
npm-debug.log
17+
/test/justified.srt

lib/endpoints.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ ep.prototype.get = function(action, params, cb) {
3434

3535
//console.log(this.options);
3636

37-
var data = '';
37+
var data = [];
3838
var req = http.request(this.options);
3939
req.on('response', function (response) {
4040

4141
//to avoid problems with subtitle with a chatset different than utf-8
42-
response.setEncoding('binary');
4342

4443
response.on('data', function (chunk) {
45-
data += chunk;
44+
if(!!chunk && chunk.length > 0){
45+
data.push(chunk);
46+
}
4647
}).on('end', function(){
47-
cb(null, this.statusCode, data);
48+
debugger;
49+
cb(null, this.statusCode, Buffer.concat(data));
4850
});
4951
}).on('error', function(e) {
5052
cb(e);
@@ -77,17 +79,19 @@ ep.prototype.post = function(action, file, params, cb) {
7779
}).on('end', function(){
7880
cb(null, this.statusCode, data);
7981
});
82+
req.end();
8083
}).on('error', function(e) {
8184
cb(e);
85+
req.end();
8286
});
83-
req.end();
8487
};
8588

8689

8790
/** protocol methods */
8891
ep.prototype.available_languages = function(cb){
8992
this.get('languages', {}, function(err, status, res) {
9093
if(err) return cb(err);
94+
res = res.toString();
9195

9296
if(status == 400) return cb('Bad Request');
9397

@@ -101,6 +105,7 @@ ep.prototype.search_subtitles = function(hash, versions, cb){
101105

102106
this.get('search', params, function(err, status, res){
103107
if(err) return cb(err);
108+
res = res.toString();
104109

105110
if(status == 400) return cb('Bad Request');
106111
if(status == 404) return cb(null, ''); // no subtitle found
@@ -117,7 +122,7 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
117122
if(status == 400) return cb('Bad Request');
118123
if(status == 404) return cb(null, ''); // no subtitle found
119124

120-
fs.writeFile(path, res, function(err) {
125+
fs.writeFile(path, res, {encoding: 'binary'}, function(err) {
121126
if(err) return cb(err);
122127

123128
cb(null, path);

test/justified-FR-ANSI.srt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1
2+
00:00:05,000 --> 00:00:15,000
3+
Attention: Ceci est une légende de test.
4+
5+
2
6+
00:00:25,000 --> 00:00:40,000
7+
SubDB - une base de données libre de sous-titres.
8+
http://thesubdb.com
9+

test/test.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,26 @@ describe('Subdb', function() {
7373

7474
describe('#download_subtitle', function(){
7575
it('should download subtitle', function(done) {
76-
var path = process.cwd()+'/test/justified.srt';
76+
var path = process.cwd()+'/test/justified.srt',
77+
properlyEncoded = process.cwd() + '/test/justified-FR-ANSI.srt'
78+
;
79+
80+
var bufferExpectedFile = fs.readFileSync(properlyEncoded);
81+
7782
fs.unlink(path, function() {
7883
var subdb = new SubDb();
7984
subdb.api.download_subtitle('edc1981d6459c6111fe36205b4aff6c2', 'fr', path, function(err, res){
8085
if(err) return done(err);
8186

82-
fs.stat(res, function(err, stat) {
83-
if(err) return done(err);
87+
var bufferActualFile = fs.readFileSync(path);
88+
89+
assert.equal(bufferActualFile.length, bufferExpectedFile.length);
8490

85-
assert.equal(stat.size, 179);
91+
for(var i = 0; i < bufferActualFile.length; i++){
92+
assert.equal(bufferActualFile[i], bufferExpectedFile[i]);
93+
}
8694

87-
done();
88-
});
95+
done();
8996
});
9097
});
9198
});

0 commit comments

Comments
 (0)