file: fix file.read and add tests
This commit also changes the initial file in volume from README to README.md. Side note: By default, superagent only buffers response for text/* and form data. So when sending across README, the file is just an octet-stream and the response in not received in res.text. This can be fixed by calling buffer(false) in superagent request. Renaming the file to README.md side steps this problem because .md files have the mime type text/x-markdown.
This commit is contained in:
+3
-2
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "YellowTent",
|
||||
"name": "yellowtent",
|
||||
"description": "Yellow tent",
|
||||
"version": "0.0.1",
|
||||
"author": {
|
||||
@@ -21,7 +21,8 @@
|
||||
"express": "*",
|
||||
"superagent": "*",
|
||||
"wrench": "*",
|
||||
"debug": "*"
|
||||
"debug": "*",
|
||||
"mime": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "*",
|
||||
|
||||
+4
-3
@@ -6,14 +6,15 @@ var exec = require('child_process').exec,
|
||||
mkdirp = require('mkdirp'),
|
||||
fs = require('fs'),
|
||||
assert = require('assert'),
|
||||
crypto = require('crypto');
|
||||
crypto = require('crypto'),
|
||||
debug = require('debug')('repo.js');
|
||||
|
||||
exports = module.exports = Repo;
|
||||
|
||||
// creates a repo. before you do anything
|
||||
function Repo(config) {
|
||||
this.gitDir = config.root + '/.git';
|
||||
this.checkoutDir = config.root;
|
||||
this.gitDir = config.rootDir + '/.git';
|
||||
this.checkoutDir = config.rootDir;
|
||||
}
|
||||
|
||||
// run arbitrary git command on this repo
|
||||
|
||||
+7
-11
@@ -2,31 +2,27 @@
|
||||
|
||||
var fs = require('fs'),
|
||||
HttpError = require('../httperror'),
|
||||
syncer = require('../syncer');
|
||||
syncer = require('../syncer'),
|
||||
mime = require('mime');
|
||||
|
||||
exports = module.exports = {
|
||||
initialize: initialize,
|
||||
listing: listing,
|
||||
read: read,
|
||||
update: update
|
||||
};
|
||||
|
||||
function initialize(config) {
|
||||
}
|
||||
|
||||
function listing(req, res, next) {
|
||||
res.send(sync.index.json());
|
||||
}
|
||||
|
||||
function read(req, res, next) {
|
||||
var filePath = req.params[0];
|
||||
|
||||
var file = req.repo.createReadStream(filePath);
|
||||
file.on('open', function () {
|
||||
// not setting the Content-Length explicitly sends the data using chunked encoding
|
||||
res.writeHead(200, { 'Content-Type' : mime.lookup(filePath) });
|
||||
file.pipe(res);
|
||||
});
|
||||
file.on('error', function (err) {
|
||||
if (err.code == 'ENOENT' || err.code == 'ENOTDIR') return next(new HttpError(404, 'Not found'));
|
||||
return next(new HttpError(500, 'Stream error:' + err));
|
||||
});
|
||||
file.pipe(res);
|
||||
}
|
||||
|
||||
function update(req, res, next) {
|
||||
|
||||
@@ -114,10 +114,10 @@ function createVolume(req, res, next) {
|
||||
}
|
||||
|
||||
// ## move this to repo
|
||||
var repo = new Repo({ root: volumeMountPoint });
|
||||
var repo = new Repo({ rootDir: volumeMountPoint });
|
||||
repo.create({ name: 'nobody', email: 'somebody@like.me' }, function (error) {
|
||||
if (error) return next(new HttpError(500, 'Error creating repo in volume'));
|
||||
repo.addFile('README', { contents: 'Say something useful here' }, function (error, commit) {
|
||||
repo.addFile('README.md', { contents: 'README' }, function (error, commit) {
|
||||
if (error) return next(new HttpError(500, 'Error adding README: ' + error));
|
||||
repos[req.body.name] = repo;
|
||||
res.send(201);
|
||||
|
||||
@@ -114,7 +114,6 @@ app.configure(function () {
|
||||
|
||||
app.post('/api/v1/sync/:volume/diff', routes.sync.diff);
|
||||
|
||||
app.get('/api/v1/file/dirIndex', routes.file.listing);
|
||||
app.get('/api/v1/file/:volume/*', routes.file.read);
|
||||
app.post('/api/v1/file/:volume/*', routes.file.update);
|
||||
|
||||
@@ -152,7 +151,6 @@ function initialize(callback) {
|
||||
|
||||
routes.sync.initialize(config);
|
||||
|
||||
// routes.file.initialize(config, sync);
|
||||
routes.volume.initialize(config, callback);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ describe('volume', function () {
|
||||
.end(function (err, res) {
|
||||
var foundReadme = false;
|
||||
res.body.forEach(function (entry) {
|
||||
if (entry.filename == 'README') foundReadme = true;
|
||||
if (entry.filename == 'README.md') foundReadme = true;
|
||||
});
|
||||
expect(foundReadme == true).to.be.ok();
|
||||
done(err);
|
||||
@@ -103,3 +103,15 @@ describe('volume', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('file', function () {
|
||||
it('read', function (done) {
|
||||
request.get(SERVER_URL + '/api/v1/file/' + TESTVOLUME + '/README.md')
|
||||
.set('Authorization', AUTH)
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode == 200).to.be.ok();
|
||||
expect(res.text == 'README').to.be.ok();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user