Skip to content
Open
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
7 changes: 2 additions & 5 deletions fileserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ var bodyParser = require('body-parser');
var fileDriver = require('./fsDriver.js');
var url = require('url');
var mime = require('mime');
var path = require('path');
var mw = require('dat-middleware');
var flow = require('middleware-flow');
var morgan = require('morgan');
var error = require('debug')('rest-fs:fileserver');

var fileserver = function(app) {
var fileserver = function(app, opts) {
if (!app) {
throw new Error('express app required');
}

fileDriver = fileDriver(opts);
app.set('etag', 'strong');
app.use(require('express-domain-middleware'));
app.use(bodyParser.json());
Expand Down
73 changes: 31 additions & 42 deletions fsDriver.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
// fsDriver
// use fs engine to server files
var fs = require('fs');
var findit = require('findit');
var path = require('path');
var mv = require('mv');
var rm = require('rimraf');
var error = require('debug')('rest-fs:fsDriver');

module.exports = function({fs = require('fs'), basePath = ''} = {}) {
// returns array of files and dir. trailing slash determines type.
var listAll = function(args, cb) {
var dirPath = args.dirPath;
var finder = findit(dirPath);
var files = [];

finder.on('directory', function (dir, stat, stop) {
files.push(path.join(dir, '/'));
});

finder.on('file', function (file, stat) {
files.push(file);
});
var dirPath = basePath + args.dirPath;
const result = [];
const files = [dirPath];
do {
const filepath = files.pop();
const stat = fs.lstatSync(filepath);
if (stat.isDirectory()) {
fs
.readdirSync(filepath)
.forEach(f => files.push(path.join(filepath, f)));
} else if (stat.isFile()) {
result.push(path.relative(reqDir, filepath));
}
} while (files.length !== 0);

finder.on('end', function () {
cb(null, files);
});
cb(null, result);
};

// returns array of files and dir. trailing slash determines type.
var list = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;
var filesList = [];
var cnt = 0;
fs.readdir(dirPath, function (err, files) {
Expand Down Expand Up @@ -66,7 +64,7 @@ var list = function(args, cb) {
read file from filepath
*/
var readFile = function(args, cb) {
var filePath = args.filePath;
var filePath = basePath + args.filePath;
var encoding = args.encoding;

fs.readFile(filePath, encoding, cb);
Expand All @@ -76,7 +74,7 @@ var readFile = function(args, cb) {
mkdir
*/
var mkdir = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;
var mode = args.mode;

fs.mkdir(dirPath, mode, cb);
Expand All @@ -86,11 +84,11 @@ var mkdir = function(args, cb) {
delete directory
*/
var rmdir = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;
var clobber = args.clobber;

if (clobber) {
return rm(dirPath, cb);
return fs.unlink(dirPath, cb);
}
return fs.rmdir(dirPath, cb);
};
Expand All @@ -99,7 +97,7 @@ var rmdir = function(args, cb) {
writeFile
*/
var writeFile = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;
var data = args.data;
var options = args.options;

Expand All @@ -110,7 +108,7 @@ var writeFile = function(args, cb) {
write file with stream
*/
var writeFileStream = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;
var stream = args.stream;
var options = args.options;
var file = fs.createWriteStream(dirPath, options);
Expand All @@ -126,7 +124,7 @@ var writeFileStream = function(args, cb) {
delete file
*/
var unlink = function(args, cb) {
var dirPath = args.dirPath;
var dirPath = basePath + args.dirPath;

fs.unlink(dirPath, cb);
};
Expand All @@ -135,8 +133,8 @@ var unlink = function(args, cb) {
move file
*/
var move = function (args, cb) {
var oldPath = args.dirPath;
var newPath = args.newPath;
var oldPath = basePath + args.dirPath;
var newPath = basePath + args.newPath;
var opts = args.options;
// have to remove trailing slaches
if(oldPath.substr(-1) == '/') {
Expand All @@ -160,12 +158,12 @@ var move = function (args, cb) {

if (opts.clobber) {
// also work around bug for clobber in dir
return rm(newPath, function(err) {
return fs.unlink(newPath, function(err) {
if (err) { return cb(err); }
mv(oldPath, newPath, opts, cb);
fs.rename(oldPath, newPath, cb);
});
}
return mv(oldPath, newPath, opts, cb);
return fs.rename(oldPath, newPath, cb);
});
};

Expand All @@ -180,14 +178,5 @@ var stat = function (args, cb) {
cb(null, stats);
});
};

module.exports.listAll = listAll;
module.exports.list = list;
module.exports.readFile = readFile;
module.exports.mkdir = mkdir;
module.exports.rmdir = rmdir;
module.exports.writeFile = writeFile;
module.exports.unlink = unlink;
module.exports.move = move;
module.exports.writeFileStream = writeFileStream;
module.exports.stat = stat;
return {listAll,list,readFile,mkdir,rmdir,writeFile,unlink,move,writeFileStream,stat};
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ var express = require('express');
var app = express();
var fileserver = require('./fileserver.js');

fileserver(app);
fileserver(app, {fs: require('fs'), basePath: ''});

app.listen(3000);