Compare commits

...

4 Commits

Author SHA1 Message Date
Tyler Stewart
695e594d97 Merge pull request #6 from stewarttylerr/migate-to-moment
feat: migrate to moment from date-fns, fix ls format
2017-03-31 17:14:44 -06:00
Tyler Stewart
97b55fc92c feat: migrate to moment from date-fns, fix ls format
Date-fns is great, but too early for use
2017-03-31 17:12:57 -06:00
Tyler Stewart
577066850b fix: improve getting current directory 2017-03-30 12:26:04 -06:00
Tyler Stewart
0ec989cf1e docs: update login event for new root option 2017-03-29 10:20:22 -06:00
6 changed files with 49 additions and 19 deletions

View File

@@ -89,10 +89,13 @@ ftpServer.listen()
- __password__
- Password provided in the `PASS` command
- Only provided if `anonymous` is set to `false`
- __resolve ({fs, cwd, blacklist, whitelist})__
- __resolve ({fs, root, cwd, blacklist, whitelist})__
- __fs__ _[optional]_
- Optional file system class for connection to use
- See [File System](#file-system) for implementation details
- __root__ _[optional]_
- If `fs` not provided, will set the root directory for the connection
- The user cannot traverse lower than this directory
- __cwd__ _[optional]_
- If `fs` not provided, will set the starting directory for the connection
- __blacklist__ _[optional]_

View File

@@ -47,9 +47,9 @@
},
"dependencies": {
"bunyan": "^1.8.9",
"date-fns": "^1.28.2",
"lodash": "^4.17.4",
"minimist-string": "^1.0.2",
"moment": "^2.18.1",
"uuid": "^3.0.1",
"when": "^3.7.8"
},

View File

@@ -1,5 +1,5 @@
const when = require('when');
const format = require('date-fns/format');
const moment = require('moment');
module.exports = {
directive: 'MDTM',
@@ -9,7 +9,7 @@ module.exports = {
return when.try(this.fs.get.bind(this.fs), command._[1])
.then(fileStat => {
const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS');
const modificationTime = moment.utc(fileStat.mtime).format('YYYYMMDDHHmmss.SSS');
return this.reply(213, modificationTime);
})
.catch(err => {

View File

@@ -13,8 +13,8 @@ class FileSystem {
cwd = '/'
} = {}) {
this.connection = connection;
this.cwd = cwd;
this.root = root;
this.cwd = nodePath.resolve(cwd);
this.root = nodePath.resolve(root);
}
_resolvePath(path) {
@@ -60,7 +60,7 @@ class FileSystem {
if (!stat.isDirectory()) throw new errors.FileSystemError('Not a valid directory');
})
.then(() => {
this.cwd = path.replace(new RegExp(`^${this.root}`), '') || '/';
this.cwd = path.substring(this.root.length) || '/';
return this.currentDirectory();
});
}

View File

@@ -1,5 +1,5 @@
const _ = require('lodash');
const dateFns = require('date-fns');
const moment = require('moment');
const errors = require('../errors');
module.exports = function (fileStat, format = 'ls') {
@@ -16,6 +16,10 @@ module.exports = function (fileStat, format = 'ls') {
};
function ls(fileStat) {
const now = moment.utc();
const mtime = moment.utc(new Date(fileStat.mtime));
const dateFormat = now.diff(mtime, 'months') < 6 ? 'MMM DD HH:mm' : 'MMM DD YYYY';
return [
fileStat.mode !== null
? [
@@ -35,7 +39,7 @@ function ls(fileStat) {
fileStat.uid,
fileStat.gid,
_.padStart(fileStat.size, 12),
_.padStart(dateFns.format(fileStat.mtime, 'MMM DD HH:mm'), 12),
_.padStart(mtime.format(dateFormat), 12),
fileStat.name
].join(' ');
}
@@ -44,7 +48,7 @@ function ep(fileStat) {
const facts = [
fileStat.dev && fileStat.ino ? `i${fileStat.dev.toString(16)}.${fileStat.ino.toString(16)}` : null,
fileStat.size ? `s${fileStat.size}` : null,
fileStat.mtime ? `m${dateFns.format(dateFns.parse(fileStat.mtime), 'X')}` : null,
fileStat.mtime ? `m${moment.utc(new Date(fileStat.mtime)).format('X')}` : null,
fileStat.mode ? `up${fileStat.mode.toString(8).substr(fileStat.mode.toString(8).length - 3)}` : null,
fileStat.isDirectory() ? 'r' : '/'
].join(',');

View File

@@ -1,5 +1,4 @@
const {expect} = require('chai');
const dateFns = require('date-fns');
const fileStat = require('../../src/helpers/file-stat');
const errors = require('../../src/errors');
@@ -17,24 +16,48 @@ describe('helpers // file-stat', function () {
size: 527,
blksize: 4096,
blocks: 8,
atime: 'Mon, 10 Oct 2011 23:24:11 GMT',
mtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
ctime: 'Mon, 10 Oct 2011 23:24:11 GMT',
birthtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
atime: 'Mon, 10 Oct 2017 23:24:11 GMT',
mtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
ctime: 'Mon, 10 Oct 2017 23:24:11 GMT',
birthtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
isDirectory: () => false
};
describe.skip('format - ls //', function () {
const STAT_OLD = {
name: 'test2',
dev: 2114,
ino: 48064969,
mode: 33188,
nlink: 1,
uid: 84,
gid: 101,
rdev: 0,
size: 530,
blksize: 4096,
blocks: 8,
atime: 'Mon, 10 Oct 2011 14:05:12 GMT',
mtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
ctime: 'Mon, 10 Oct 2011 14:05:12 GMT',
birthtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
isDirectory: () => false
};
describe('format - ls //', function () {
it('formats correctly', () => {
const format = fileStat(STAT, 'ls');
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 17:24 test1');
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 23:24 test1');
});
it('formats correctly for files over 6 months old', () => {
const format = fileStat(STAT_OLD, 'ls');
expect(format).to.equal('-rwxrw-r-- 1 84 101 530 Oct 10 2011 test2');
});
});
describe.skip('format - ep //', function () {
describe('format - ep //', function () {
it('formats correctly', () => {
const format = fileStat(STAT, 'ep');
expect(format).to.equal('+i842.2dd69c9,s527,m1318289051,up644,/ test1');
expect(format).to.equal('+i842.2dd69c9,s527,m1507677851,up644,/ test1');
});
});