Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
no attachment for html
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 15, 2014
1 parent e1e6b6b commit 67469f3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 14 deletions.
2 changes: 1 addition & 1 deletion controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ exports.download = function *(next) {
this.length = dist.size;
}
this.type = mime.lookup(dist.key);
this.attachment = filename;
this.attachment(filename);
this.etag = dist.shasum;

this.body = yield* downloadAsReadStream(dist.key);
Expand Down
22 changes: 19 additions & 3 deletions controllers/web/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

var debug = require('debug')('cnpmjs.org:controllers:web:dist');
var mime = require('mime');
var urlparse = require('url').parse;
var Dist = require('../../proxy/dist');
var config = require('../../config');
var downloadAsReadStream = require('../utils').downloadAsReadStream;
Expand Down Expand Up @@ -61,24 +62,39 @@ exports.list = function* (next) {
});
};

function* download(next) {
function* download(next) {
var fullname = this.params[0];
var info = yield* Dist.getfile(fullname);
debug('download %s got %j', fullname, info);
if (!info || !info.url) {
return yield* next;
}

if (/\.(html|js|css|json|txt)$/.test(fullname)) {
if (info.url.indexOf('http') === 0) {
info.url = urlparse(info.url).path;
}
return yield* pipe.call(this, info, false);
}

if (info.url.indexOf('http') === 0) {
return this.redirect(info.url);
}
yield* pipe.call(this, info, true);
}

function* pipe(info, attachment) {
debug('pipe %j, attachment: %s', info, attachment);
// download it from nfs
if (typeof info.size === 'number' && info.size > 0) {
this.length = info.size;
}
this.type = mime.lookup(info.url);
this.attachment = info.name;
this.etag = info.sha1;
if (attachment) {
this.attachment(info.name);
}
if (info.sha1) {
this.etag = info.sha1;
}
this.body = yield* downloadAsReadStream(info.url);
}
97 changes: 87 additions & 10 deletions test/controllers/web/dist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,82 @@ describe('controllers/web/dist.test.js', function () {
});
});

describe('GET /dist/ files', function () {
it('should redirect to nfs url', function (done) {
describe.only('GET /dist/ files', function () {
it('should pipe txt', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: 'foo.txt', size: 1024, date: '02-May-2014 00:54',
url: 'http://mock.com/dist/v0.10.28/SHASUMS.txt'
};
});
fs.writeFileSync(nfs._getpath('/dist/v0.10.28/SHASUMS.txt'), '6eff580cc8460741155d42ef1ef537961194443f');

request(app)
.get('/dist/v0.10.28/SHASUMS.txt')
.expect(302)
.expect('Location', 'http://mock.com/dist/v0.10.28/SHASUMS.txt', done);
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, function (err, res) {
should.not.exist(err);
should.not.exist(res.headers['Content-Disposition']);
done();
});
});

it('should GET /dist/npm-versions.txt redirect to nfs url', function (done) {
it('should pipe html', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: 'foo.html', size: 1024, date: '02-May-2014 00:54',
url: 'http://mock.com/dist/v0.10.28/foo.html'
};
});
fs.writeFileSync(nfs._getpath('/dist/v0.10.28/foo.html'), '<p>hi</p>');

request(app)
.get('/dist/v0.10.28/foo.html')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('<p>hi</p>')
.expect(200, function (err, res) {
should.not.exist(err);
should.not.exist(res.headers['Content-Disposition']);
done();
});
});

it('should pipe json', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: 'foo.json', date: '02-May-2014 00:54',
url: 'http://mock.com/dist/v0.10.28/foo.json'
};
});
fs.writeFileSync(nfs._getpath('/dist/v0.10.28/foo.json'), '{}');

request(app)
.get('/dist/v0.10.28/foo.json')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect('{}')
.expect(200, function (err, res) {
should.not.exist(err);
should.not.exist(res.headers['Content-Disposition']);
done();
});
});

it('should GET /dist/npm-versions.tgz redirect to nfs url', function (done) {
mm(Dist, 'getfile', function* (fullname) {
fullname.should.equal('/npm-versions.txt');
fullname.should.equal('/npm-versions.tgz');
return {
name: 'npm-versions.txt', size: 1024, date: '02-May-2014 00:54',
url: 'http://mock.com/dist/npm-versions.txt'
url: 'http://mock.com/dist/npm-versions.tgz'
};
});

request(app)
.get('/dist/npm-versions.txt')
.get('/dist/npm-versions.tgz')
.expect(302)
.expect('Location', 'http://mock.com/dist/npm-versions.txt', done);
.expect('Location', 'http://mock.com/dist/npm-versions.tgz', done);
});

it('should download nfs file and send it', function (done) {
it('should download nfs txt file and send it', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: 'foo.txt',
Expand All @@ -135,5 +180,37 @@ describe('controllers/web/dist.test.js', function () {
.expect(200)
.expect(/6eff580cc8460741155d42ef1ef537961194443f/, done);
});

it('should download nfs tgz file and send it', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: 'foo.tgz',
size: 1264,
date: '02-May-2014 00:54',
url: '/dist/v0.10.28/foo.tgz'
};
});
fs.writeFileSync(nfs._getpath('/dist/v0.10.28/foo.tgz'), '6eff580cc8460741155d42ef1ef537961194443f');
request(app)
.get('/dist/v0.10.28/foo.tgz')
.expect('Content-Disposition', 'attachment; filename="foo.tgz"')
.expect(200, done);
});

it.skip('should download nfs no-ascii attachment file name', function (done) {
mm(Dist, 'getfile', function* () {
return {
name: '中文名.tgz',
size: 1264,
date: '02-May-2014 00:54',
url: '/dist/v0.10.28/foo.tgz'
};
});
fs.writeFileSync(nfs._getpath('/dist/v0.10.28/foo.tgz'), '6eff580cc8460741155d42ef1ef537961194443f');
request(app)
.get('/dist/v0.10.28/foo.tgz')
.expect('Content-Disposition', 'attachment; filename="%E4%B8%AD%E6%96%87%E5%90%8D.tgz"')
.expect(200, done);
});
});
});

0 comments on commit 67469f3

Please sign in to comment.