forked from gaelmotte/evoke13
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasset.js
160 lines (126 loc) · 4.67 KB
/
asset.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//var git = require("gift")
var fs = require("fs")
var Cookies = require("cookies")
module.exports.init = function(app)
{
app.get("/data/project/:projectId/assets", function(req, res, next)
{
var projectName = req.params.projectId
fs.readdir(app.get("dataRoot") + projectName, function(err, files)
{
if (err) return next(err)
var assets = files.filter(function(file)
{
var stats = fs.statSync(app.get("dataRoot") + projectName + "/" + file)
return stats.isFile()
})
res.json(assets)
})
})
app.post("/data/project/:projectId/assets", function(req, res, next)
{
var projectName = req.params.projectId
var assetName = req.body.name
var assetType = req.body.type
if (/^[-\w.]+$/.test(assetName) == false)
{
return next(new Error("Asset names should match /^[-\\w.]+$/"))
}
var templatePath = __dirname + "/data_templates/" + assetType + ".tpl"
var assetPath = app.get("dataRoot") + projectName + "/" + assetName + "." + assetType
fs.exists(assetPath, function(exists)
{
if (exists) return next(new Error("Asset already exists"))
fs.readFile(templatePath, function(err, templateData)
{
if (err) return next(err)
fs.writeFile(assetPath, templateData, function(err)
{
if (err) return next(err)
/*var repo = git(app.get("dataRoot") + projectName)
repo.add(assetName + "." + assetType, function(err)
{
if (err) return next(err)
var cookies = new Cookies(req, res)
var authorString = cookies.get("name") + " <" + cookies.get("email") + ">"
repo.commit("added new " + assetType, {author: authorString}, function(err)
{
if (err) return next(err)
res.send(200, "Asset created")
})
})*/
res.send(200, "Asset created")
})
})
})
})
app.get("/data/project/:projectId/asset/:assetId", function(req, res, next)
{
var projectId = req.params.projectId
var assetId = req.params.assetId
var assetPath = app.get("dataRoot") + projectId + "/" + assetId
fs.readFile(assetPath, function(err, assetData)
{
if (err) return next(err)
res.type("text/plain")
res.send(200, assetData)
})
})
app.put("/data/project/:projectId/asset/:assetId", function(req, res, next)
{
var projectId = req.params.projectId
var assetId = req.params.assetId
var assetPath = app.get("dataRoot") + projectId + "/" + assetId
fs.writeFile(assetPath, req.body.assetData, function(err)
{
if (err) return next(err)
/*var repo = git(app.get("dataRoot") + projectId)
repo.add(assetPath, function(err)
{
if (err) return next(err)
var cookies = new Cookies(req, res)
var authorString = cookies.get("name") + " <" + cookies.get("email") + ">"
repo.commit("updated " + assetId, {author: authorString}, function(err)
{
if (err) return next(err)
res.send(200, "Asset udpated")
})
})*/
res.send(200, "Asset updated")
})
})
app.post("/data/project/:projectId/asset/:assetId", function(req, res, next)
{
var projectId = req.params.projectId
var assetId = req.params.assetId
if (typeof(req.body.rename) !== "string") return next(new Error("Unsupported operation"))
if (/^[-\w.]+$/.test(req.body.rename) == false)
{
return next(new Error("Asset names should match /^[-\\w.]+$/"))
}
var assetPath = app.get("dataRoot") + projectId + "/" + assetId
var newAssetPath = app.get("dataRoot") + projectId + "/" + req.body.rename
if (assetPath == newAssetPath) return next(new Error("New name is the same as old name"))
fs.rename(assetPath, newAssetPath, function(err)
{
if (err) return next(err)
/*var repo = git(app.get("dataRoot") + projectId)
repo.add(newAssetPath, function(err)
{
if (err) return next(err)
repo.remove(assetPath, function(err)
{
if (err) return next(err)
var cookies = new Cookies(req, res)
var authorString = cookies.get("name") + " <" + cookies.get("email") + ">"
repo.commit("renamed " + assetId + " to " + req.body.rename, {author: authorString}, function(err)
{
if (err) return next(err)
res.send(200, "Asset renamed")
})
})
})*/
res.send(200, "Asset renamed")
})
})
}