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

no attachment for html #444

Merged
merged 1 commit into from
Sep 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use attachment in wrong way before...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

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);
}
95 changes: 86 additions & 9 deletions test/controllers/web/dist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,81 @@ describe('controllers/web/dist.test.js', function () {
});

describe('GET /dist/ files', function () {
it('should redirect to nfs url', function (done) {
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);
});
});
});