-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
222 lines (188 loc) · 5.37 KB
/
Copy pathserver.js
File metadata and controls
222 lines (188 loc) · 5.37 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var express = require('express'),
app = express(),
fs = require('fs'),
path = require('path'),
exec = require('child_process').exec,
imageinfo = require('imageinfo');
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
});
var isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
/*var tiles = [
[
{img: 'img/grass.png'},
{img: 'img/mountains.png'},
{img: 'img/cave-20.png'},
{img: 'img/cave-21.png'},
{img: 'img/cave-22.png'}
]
];*/
var world = null;
var splitImage = function(path) {
var cmd = 'convert ' + path + ' -crop 16x16 -filter Point -resize x32 +antialias ' + path;
exec(cmd, function(err, stdout, stderr) {
if (err) {
if (err.message.indexOf('Invalid Parameter') !== -1) {
throw 'Must install imagemagick';
} else {
throw err;
}
//console.log(err.message);
//throw err;
}
console.log('cropped');
});
};
var getRelativePath = function(path) {
var pieces = path.split('\\');
var index = pieces.indexOf('public');
var relativePath = pieces.slice(index).join('\\');
//var relativePath = pieces[pieces.length-2] + '\\' + pieces[pieces.length-1];
return relativePath;
};
var tryMakeFolder = function(folderPath, callback) {
fs.stat(folderPath, function(err, stats) {
var exists = false;
if (err && err.code !== 'ENOENT') {
throw err;
} else if (stats && stats.isDirectory()) {
exists = true;
}
if (!exists) {
fs.mkdir(folderPath, function(err) {
if (err) throw err;
callback();
});
} else {
callback();
}
});
};
var saveFile = function(req, callback) {
var targetFolder = path.resolve('./public/img/' + req.body.fileName);
tryMakeFolder(targetFolder, function() {
var tempPath = req.files.file.path,
targetPath = targetFolder + '\\' + req.body.fileName + '.png';
// if (path.extname(req.files.file.name).toLowerCase() === '.png') {
//console.log(req.files.file)
if (req.files.file.headers['content-type'] === 'image/png') {
fs.rename(tempPath, targetPath, function(err) {
if (err) throw err;
console.log("Upload completed!");
var relativePath = getRelativePath(targetPath);
console.log(relativePath);
splitImage(relativePath);
callback();
});
} else {
fs.unlink(tempPath, function (err) {
if (err) throw err;
console.error("Only .png files are allowed!");
callback();
});
}
});
};
var getSprites = function(directory, type, callback) {
fs.readdir(directory, function(err, files) {
if (err) throw err;
var images = files.filter(function(file) {
return file.split('.').pop() === type;
});
//console.log(JSON.stringify(images, null, 2));
var tiles = images.sort(function (a, b) {
var aNum = parseInt(a.split('-').pop().split('.')[0]);
var bNum = parseInt(b.split('-').pop().split('.')[0]);
return bNum - aNum;
});
var source = tiles.pop();
var folder = directory.split('/').pop();
tiles = tiles.reverse().map(function (tile) {
return {img: 'img/' + folder + '/' + tile};
});
if (!source || !tiles || tiles.length === 0) {
return callback([]);
}
fs.readFile(directory + '/' + source, function (err, data) {
if (err) throw err;
info = imageinfo(data);
/*console.log("Data is type:", info.mimeType);
console.log(" Size:", data.length, "bytes");*/
var tilesPerRow = info.width / 16;
var arrangedTiles = [];
tiles.forEach(function(tile, i) {
var row = Math.floor(i / tilesPerRow);
var column = i - row * tilesPerRow;
if (column === 0) {
arrangedTiles[row] = [];
}
arrangedTiles[row].push(tile);
});
callback(arrangedTiles);
});
});
};
app.get('/tiles', function(req, res) {
getSprites('./public/img/cave', 'png', function (tiles) {
res.send(tiles);
});
});
app.post('/world', function(req, res) {
world = req.body;
res.send('success');
});
app.get('/world', function(req, res) {
res.send(world);
});
app.post('/upload', function (req, res) {
res.setHeader('Content-Type', 'text/html');
if (req.files.length === 0 || req.files.file.size === 0) {
res.send({ msg: 'No file uploaded at ' + new Date().toString() });
} else {
var file = req.files.file;
saveFile(req, function() {
res.send({ msg: '<b>"' + file.name + '"</b> uploaded to the server at ' + new Date().toString() });
});
}
});
app.get('/tilesets', function (req, res) {
fs.readdir('./public/img', function (err, files) {
if (err) throw err;
var folders = files.filter(function (file) {
return file.split('.').length === 1;
}).map(function (file) {
return {
img: 'img/' + file + '/' + file + '.png',
name: file
};
});
var pending = folders.length;
folders.forEach(function(folder) {
getSprites('./public/img/' + folder.name, 'png', function (tiles) {
folder.tiles = tiles;
if (!--pending) res.send(folders);
});
});
//res.send(folders);
});
});
app.get('/stat', function (req, res) {
getSprites('./uploads', 'png', function (tiles) {
console.log(JSON.stringify(tiles, null, 2));
});
});
app.get('/read', function (req, res) {
fs.readFile('./uploads/cave.png', function (err, data) {
if (err) throw err;
info = imageinfo(data);
/*console.log("Data is type:", info.mimeType);
console.log(" Size:", data.length, "bytes");*/
var tilesPerRow = info.width / 16;
console.log("There are", tilesPerRow, "tiles per row");
});
});
app.listen(9999);
console.log('listening on port 9999...');