aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJohannes Zellner <johannes@nebulon.de>2015-06-27 17:07:42 +0200
committerJohannes Zellner <johannes@nebulon.de>2015-06-27 17:07:42 +0200
commit8c3ae0719e1f7d266ee04d86e7e1c3756745d372 (patch)
treeb5c2c0e9d7d339490c3c8384dcffe264ae67e7de /src
parent08b2ad7f716b3323ab8c168eb8a00a47fda03d66 (diff)
downloadSurfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.tar.gz
Surfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.tar.zst
Surfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.zip
Support recursive put
Diffstat (limited to 'src')
-rw-r--r--src/actions.js34
-rw-r--r--src/files.js27
2 files changed, 16 insertions, 45 deletions
diff --git a/src/actions.js b/src/actions.js
deleted file mode 100644
index 33e47aa..0000000
--- a/src/actions.js
+++ /dev/null
@@ -1,34 +0,0 @@
1'use strict';
2
3exports.put = put;
4exports.get = get;
5exports.del = del;
6
7var superagent = require('superagent'),
8 path = require('path');
9
10var server = 'http://localhost:3000/api/files/';
11
12function put(filePath) {
13 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
14 superagent.put(server + relativeFilePath).attach('file', filePath).end(function (error, result) {
15 if (error) return console.log('Failed', result ? result.body : error);
16 console.log('Success', result.body);
17 });
18}
19
20function get(filePath) {
21 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
22 superagent.get(server + relativeFilePath).end(function (error, result) {
23 if (error) return console.log('Failed', result ? result.body : error);
24 console.log('Success', result.body);
25 });
26}
27
28function del(filePath) {
29 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
30 superagent.del(server + relativeFilePath).end(function (error, result) {
31 if (error) return console.log('Failed', result ? result.body : error);
32 console.log('Success', result.body);
33 });
34}
diff --git a/src/files.js b/src/files.js
index 3812d21..48f91a8 100644
--- a/src/files.js
+++ b/src/files.js
@@ -4,17 +4,22 @@ var fs = require('fs'),
4 path = require('path'), 4 path = require('path'),
5 ejs = require('ejs'), 5 ejs = require('ejs'),
6 rimraf = require('rimraf'), 6 rimraf = require('rimraf'),
7 debug = require('debug')('files'),
7 mkdirp = require('mkdirp'), 8 mkdirp = require('mkdirp'),
8 HttpError = require('connect-lastmile').HttpError, 9 HttpError = require('connect-lastmile').HttpError,
9 HttpSuccess = require('connect-lastmile').HttpSuccess; 10 HttpSuccess = require('connect-lastmile').HttpSuccess;
10 11
11exports = module.exports = { 12var gBasePath;
12 get: get, 13
13 put: put, 14exports = module.exports = function (basePath) {
14 del: del 15 gBasePath = basePath;
15};
16 16
17var FILE_BASE = path.resolve(__dirname, '../files'); 17 return {
18 get: get,
19 put: put,
20 del: del
21 };
22};
18 23
19// http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js 24// http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js
20function copyFile(source, target, cb) { 25function copyFile(source, target, cb) {
@@ -54,9 +59,9 @@ function render(view, options) {
54} 59}
55 60
56function getAbsolutePath(filePath) { 61function getAbsolutePath(filePath) {
57 var absoluteFilePath = path.resolve(FILE_BASE, filePath); 62 var absoluteFilePath = path.resolve(gBasePath, filePath);
58 63
59 if (absoluteFilePath.indexOf(FILE_BASE) !== 0) return null; 64 if (absoluteFilePath.indexOf(gBasePath) !== 0) return null;
60 return absoluteFilePath; 65 return absoluteFilePath;
61} 66}
62 67
@@ -68,7 +73,7 @@ function get(req, res, next) {
68 fs.stat(absoluteFilePath, function (error, result) { 73 fs.stat(absoluteFilePath, function (error, result) {
69 if (error) return next(new HttpError(404, error)); 74 if (error) return next(new HttpError(404, error));
70 75
71 console.log('get', absoluteFilePath); 76 debug('get', absoluteFilePath);
72 77
73 if (result.isFile()) return res.sendfile(absoluteFilePath); 78 if (result.isFile()) return res.sendfile(absoluteFilePath);
74 if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) }); 79 if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) });
@@ -88,7 +93,7 @@ function put(req, res, next) {
88 fs.stat(absoluteFilePath, function (error, result) { 93 fs.stat(absoluteFilePath, function (error, result) {
89 if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); 94 if (error && error.code !== 'ENOENT') return next(new HttpError(500, error));
90 95
91 console.log('put', absoluteFilePath, req.files.file); 96 debug('put', absoluteFilePath, req.files.file);
92 97
93 if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); 98 if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories'));
94 if (!result || result.isFile()) { 99 if (!result || result.isFile()) {
@@ -106,7 +111,7 @@ function del(req, res, next) {
106 var filePath = req.params[0]; 111 var filePath = req.params[0];
107 var absoluteFilePath = getAbsolutePath(filePath); 112 var absoluteFilePath = getAbsolutePath(filePath);
108 if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); 113 if (!absoluteFilePath) return next(new HttpError(404, 'Not found'));
109 if (absoluteFilePath.slice(FILE_BASE.length) === '') return next(new HttpError(403, 'Forbidden')); 114 if (absoluteFilePath.slice(gBasePath.length) === '') return next(new HttpError(403, 'Forbidden'));
110 115
111 fs.stat(absoluteFilePath, function (error, result) { 116 fs.stat(absoluteFilePath, function (error, result) {
112 if (error) return next(new HttpError(404, error)); 117 if (error) return next(new HttpError(404, error));