-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
124 lines (97 loc) · 3.51 KB
/
server.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
const express = require('express')
const fs = require('fs')
const fileUpload = require('express-fileupload')
const prettyBytes = require('pretty-bytes');
const path = require('path')
const { compositeImages, resizeImage } = require('./services/Composite')
const { getDirectories, getFiles, validFile } = require('./utils/files')
const CreationCounter = require('./utils/CreationCounter')
const PORT = process.env.PORT || 3500
const app = express()
app.use(fileUpload({
limits: { fileSize: 100 * 1024 * 1024 },
}));
app.use('/result', express.static(path.join(__dirname, 'out')))
app.use('/', express.static(path.join(__dirname, 'public')))
/*
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'upload.html'))
})
*/
app.post('/upload', function (req, res) {
let files = []
let uploadPath;
let token = req.headers.token;
const layers = req.files
if (layers.length < 2) {
return res.status(400).send('Use at least 2 layers, Each layer should have at least 1 image.');
}
Object.keys(layers).forEach(layer_index => {
if (Object.keys(layer_index).length < 1)
return res.status(400).send('Use at least 2 layers, Each layer should have at least 1 image.');
Object.keys(layers[layer_index]).forEach(file_index => {
let file = layers[layer_index][file_index];
if (!validFile(file)) {
return;
}
file.layerName = 'layer' + layer_index;
files.push(file)
})
})
let fileCount = 0;
files.forEach(file => {
if (!fs.existsSync(`./input/${token}`)) {
fs.mkdirSync(`./input/${token}`);
}
if (!fs.existsSync(`./input/${token}/${file.layerName}`)) {
fs.mkdirSync(`./input/${token}/${file.layerName}`);
}
let simagePath = `input/${token}/${file.layerName}/${fileCount++}.png2`
uploadPath = `${__dirname}/${simagePath}`;
file.mv(uploadPath, function (err) {
if (err) {
return res.status(500).send(err);
}
resizeImage(simagePath, 250, 250);
});
});
return res.json({
status: 'success', message: Object.keys(files).toString()
})
})
app.get('/generate', (req, res) => {
const token = req.headers.token;
let nftQuantity = req.headers.nftq;
if (!fs.existsSync(`./out/${token}`)) {
fs.mkdirSync(`./out/${token}`);
}
let nftCount = 0;
let possibleNFTs = 1;
//Count possible NFTs
let layersNames = getDirectories(`./input/${token}`)
let layers = [];
let maxsArray = [];
layersNames.forEach(layerName => {
let layerFiles = getFiles(`./input/${token}/${layerName}`)
layers.push(layerFiles)
maxsArray.push(layerFiles.length - 1)
possibleNFTs *= layerFiles.length
});
nftQuantity = nftQuantity < possibleNFTs ? nftQuantity : possibleNFTs;
let cCounter = new CreationCounter(maxsArray)
let imagesRes = '';
while (nftCount < nftQuantity) {
let imageArray = []
for (let lyi = 0; lyi < layers.length; lyi++) {
imageArray.push(`input/${token}/${layersNames[lyi]}/${layers[lyi][cCounter.current[lyi]]}`)
}
compositeImages(imageArray, `out/${token}/${nftCount}`)
cCounter.increment()
imagesRes += `<img src="result/${token}/${nftCount}.png" width="250" height="250"> </br>`
nftCount++
}
res.send(imagesRes)
})
app.listen(PORT, () =>
console.log(`Server running on port ${PORT}`)
)