forked from ysmood/nobone-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
52 lines (42 loc) · 1.13 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Watch and sync a local folder with a remote one.
# All the local operations will be repeated on the remote.
#
# This this the remote server.
nobone = require 'nobone'
{ kit, service } = nobone()
local_path = (path) ->
if process.platform == 'win32'
path.replace /\//g, '\\'
else
path.replace /\\/g, '\/'
module.exports = (conf) ->
service.post '/:type/:path', (req, res) ->
type = req.params.type
path = local_path req.params.path
kit.log type.cyan + ': ' + path
data = new Buffer(0)
req.on 'data', (chunk) ->
data = Buffer.concat [data, chunk]
req.on 'end', ->
switch req.params.type
when 'create'
if path[-1..] == '/'
p = kit.mkdirs(path)
else
p = kit.outputFile path, data
when 'modify'
p = kit.outputFile path, data
when 'move'
p = kit.move local_path(data.toString()), path.replace(/\/+$/, '')
when 'delete'
p = kit.remove path
else
res.status(403).end 'unknown_type'
return
p.then ->
res.send 'ok'
.catch (err) ->
kit.err err
res.status(500).end err.stack
service.listen conf.port, ->
kit.log "Listen: ".cyan + conf.port