-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,347 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const router = module.exports = require('express').Router(); | ||
const git = require('../git-tool'); | ||
const store = require('../store').mods.editor; | ||
const path = require('path'); | ||
const promisify = require('../helpers').promisify; | ||
const fs = require('fs'); | ||
const formidable = require('formidable'); | ||
|
||
const safePath = val => decodeURI(val).replace(/|\.\.|\/\//g, ''); | ||
const getRoot = req => Promise.resolve(store.root(req)) | ||
|
||
//create | ||
router.post('/file/*', (req, res) => { | ||
const f = req.query.type == 'file'; | ||
const p = safePath(req.url.slice(5)); | ||
return getRoot(req) | ||
.then(root => promisify(f && fs.writeFile || fs.mkdir)(path.join(root, p), f ? '' : 0o777)) //TODO: check if exists | ||
.then(a => ({id: p})) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) | ||
|
||
//remove | ||
router.delete('/file/*', (req, res) => { | ||
const p = safePath(req.url.slice(5)); | ||
return getRoot(req) | ||
.then(root => promisify(fs.stat)(path.join(root, p)).then(stats => promisify(stats.isDirectory() ? fs.rmdir : fs.unlink)(path.join(root, p)) )) | ||
.then(a => ({id: p})) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) | ||
|
||
//move //TODO check if destenation not exists ? | ||
router.put('/file/*', (req, res) => { | ||
const p = safePath(req.url.slice(5)); | ||
const t = safePath(req.body.to); | ||
return getRoot(req) | ||
.then(root => promisify(fs.rename)(path.join(root, p), path.join(root, t))) | ||
.then(a => ({id: t})) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) | ||
|
||
//TODO:copy | ||
|
||
//list | ||
router.get('/tree', function(req, res) { | ||
var dir = req.query.id == '#' && '/' || req.query.id || ''; | ||
dir = dir.replace(/\.\./g, ''); | ||
return getRoot(req) | ||
.then(root => promisify(fs.readdir)(path.join(root, dir)) | ||
.then(list => list.filter(name => name && (name != '.' || name != '..'))) | ||
.then(list => Promise.all(list.map(name => promisify(fs.stat)(path.join(root, dir, name)) | ||
.then(stats => ({ | ||
children: stats.isDirectory(), | ||
type: stats.isDirectory() ? 'default' : "file", | ||
text: name, | ||
id: path.join(dir, name), | ||
// icon: stats.isDirectory() ? 'jstree-folder' : "jstree-file", | ||
})))) | ||
) | ||
) | ||
.then(list => dir != '/' && list || {text: store.name(req), children: list, id: '/', type: 'default', state: {opened: true, disabled: true}}) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) | ||
|
||
//content | ||
router.get('/file/*', function(req, res) { | ||
const p = safePath(req.url.slice(5)); | ||
return getRoot(req) | ||
.then(root => promisify(fs.stat)(path.join(root, p)).then(stats => !stats.isDirectory() && promisify(fs.readFile)(path.join(root, p)) || '' )) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) | ||
|
||
const parseForm = req => new Promise((resolve, reject) => { | ||
var form = new formidable.IncomingForm(); | ||
form.parse(req, function (err, fields, files) { | ||
return err && reject(err) || resolve([fields, files]); | ||
}) | ||
}) | ||
|
||
router.post('/upload/*', function(req, res) { | ||
const p = safePath(req.url.slice(7)); | ||
return getRoot(req) | ||
.then(root => parseForm(req).then(ff => promisify(fs.rename)(ff[1].data.path, path.join(root, p)).then(a => p) )) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}); | ||
|
||
|
||
//git file TODO: git for multiproject | ||
router.get('/git/*', function(req, res) { | ||
git.Tag() | ||
.then(tag => git.Show(tag, req.originalUrl.replace(/.*git\//, ''))) | ||
.catch(e => res.status(501).send(e.message)) | ||
.then(data => res.send(data)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const sio = require('socket.io'); | ||
const ot = require('ot'); | ||
const store = require('../store').mods.editor; | ||
const promisify = require('../helpers').promisify; | ||
|
||
var ns = {}; | ||
const getFile = file => store.root().then(root => promisify(fs.readFile)(path.join(root, file))) | ||
const setFile = (file, data) => store.root().then(root => promisify(fs.writeFile)(path.join(root, file), data)) | ||
|
||
exports.init = (server, url) => { | ||
const io = new sio(server, {path: url}); | ||
io.on('connection', function(socket) { | ||
socket.on('ns', function (docId) { | ||
if (ns[docId]) | ||
socket.emit('ns', docId); | ||
else { | ||
getFile(docId) | ||
.then(text => { | ||
var doc = ns[docId] = new ot.EditorSocketIOServer(text.toString(), 0, docId) | ||
socket.emit('ns', docId); | ||
io.of(docId) | ||
.on('connection', function(socket) { | ||
function clients(mode) { | ||
socket.broadcast.in(docId).emit('clients', {clients: doc.users, mode: mode}); | ||
} | ||
doc.getClient(socket.id); | ||
doc.addClient(socket); | ||
clients('enter'); | ||
socket.in(docId) | ||
.on('disconnect', function() { | ||
console.log('disconnect'); | ||
delete doc.users[socket.id]; | ||
clients('leave'); | ||
}) | ||
.on('name', function (name) { | ||
doc.setName(socket, name); | ||
console.log('set name') | ||
clients('name'); | ||
}) | ||
.on('operation', function (name) { | ||
console.log('saving...'); | ||
setFile(docId, doc.document); | ||
}); | ||
}) | ||
}) | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.