-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
169 lines (149 loc) · 5.21 KB
/
app.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
161
162
163
164
165
166
167
168
169
const http = require('http');
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mkdirp = require('mkdirp');
const path = require('path');
var fs = require('fs');
var Promise = require('promise');
const { port } = require('./config');
var AdmZip = require('adm-zip');
const { createCanvas, loadImage } = require('canvas')
const app = express();
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(express.static('public'))
app.set('view engine', 'ejs');
app.use(fileUpload());
app.listen(port, () => {
console.log(`Server running at port:${port}`);
});
app.post("/upload", function (req, res) {
console.log('upload started');
if (!req.files || Object.keys(req.files).length === 0) {
console.log('No files were uploaded.');
return res.status(400).send('No files were uploaded.');
}
let zipFile = req.files.file;
var folder = path.join('public/uploads/') + Date.now();
createDirectory(folder)
.then(result => {
let filePath = folder + '/' + zipFile.name;
console.log(filePath);
return moveFile(zipFile, filePath);
}).then(result => {
return res.status(200).send(JSON.stringify({
message: 'File uploaded successfully',
path: result
}, null, 3));
}).catch(err => {
return res.status(500).send(err.message);
})
});
app.post("/convert", function (req, res) {
console.log(req.body.path);
let filePath = req.body.path;
let parentFolder = filePath.substring(0, filePath.lastIndexOf('/'));
let unzippedFolder = path.join(parentFolder, 'unzipped');
unzipFlags(req.body.path, unzippedFolder)
.then(result => {
return drawFlagsOnCanvas(req.body.path, unzippedFolder, parseInt(req.body.width), parseInt(req.body.height));
})
.then(result => {
console.log('created canvas');
return createSpriteImage(result, unzippedFolder);
})
.then(result => {
console.log('created sprite');
return res.status(200).send(result);
}).catch(err => {
return res.status(500).send('Failed ' + err.message);
})
});
function findCommon(arr) {
var max = 1,
m = [],
val = arr[0],
i, x;
for (i = 0; i < arr.length; i++) {
x = arr[i]
if (m[x]) {
++m[x] > max && (max = m[i], val = x);
} else {
m[x] = 1;
}
} return val;
}
function createDirectory(directoryPath) {
return new Promise(function (resolve, reject) {
mkdirp(directoryPath, function (err) {
if (err) {
return reject(err)
}
resolve();
});
})
}
function moveFile(source, destination) {
return new Promise(function (resolve, reject) {
source.mv(destination, function (err) {
if (err) {
console.log('moving file failed' + err);
return reject(err)
}
resolve(destination);
});
})
}
function unzipFlags(zipFile, destination) {
return new Promise(function (resolve, reject) {
mkdirp(destination, null);
var zip = new AdmZip(zipFile);
zip.extractAllTo(destination, true);
resolve(destination);
});
}
function drawFlagsOnCanvas(filePath, unzippedFolder, width, height) {
console.log(filePath);
console.log(width);
console.log(height);
return new Promise(function (resolve, reject) {
console.log('drawing canvas')
const canvas = createCanvas(width * 27, height * 27);
const ctx = canvas.getContext('2d');
var zip = new AdmZip(filePath);
console.log('got the entries')
var zipEntries = zip.getEntries();
zipEntries.forEach(function (zipEntry) {
let position = 1;
loadImage(path.join(unzippedFolder, zipEntry.name)).then((image) => {
let name = zipEntry.name.replace(".png", "");
let xPosition = name.charCodeAt(0) - 96;
let yPosition = name.charCodeAt(1) - 96;
ctx.drawImage(image, xPosition * width, yPosition * height, width, height)
position++;
});
});
fs.unlink(filePath, result => {
console.log('deleted zip file');
resolve(canvas);
})
});
}
function createSpriteImage(canvas, unzippedFolder) {
return new Promise(function (resolve, reject) {
loadImage(path.join(unzippedFolder, "ad.png")).then((image) => {
let folder = unzippedFolder.substring(0, unzippedFolder.lastIndexOf('/'));
let spriteImagePath = path.join(folder, "flags-sprite.png")
const out = fs.createWriteStream(spriteImagePath)
const stream = canvas.createPNGStream()
stream.pipe(out)
out.on('finish', () => {
console.log('The PNG file was created.');
fs.rmdir(unzippedFolder, {recursive:true}, result => {
resolve(`${spriteImagePath.replace('public/', '')}`);
});
});
})
});
}