-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.js
119 lines (94 loc) · 2.99 KB
/
space.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
var path = require('path');
var fs = require('fs');
var child_process = require('child_process');
var sizeOf = require('image-size');
var extend = require('node.extend');
var im = require('imagemagick');
var Promise = require('promise');
var rootPath = process.env.SPACE_ARCH_PATH;
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
function tile(questId, sourceImage, testing) {
if(testing === true) {
return new Promise(function(res, rej) {
setTimeout(function() {
res({
width: 100,
height: 100,
imgName: sourceImage,
maxZoom: 4
});
}, 5000);
});
}
var createtiles = path.join(__dirname, './createtiles.sh');
process.chdir(rootPath);
console.log('test');
try {
fs.mkdirSync('./store/quest_{questId}/tiles'.supplant({
questId: questId
}), '775');
}
catch(e) {}
var imgPath = path.resolve('./store/quest_{questId}/images/{sourceImage}'.supplant({
sourceImage: sourceImage,
questId: questId
}));
var tilesPath = path.resolve('./store/quest_{questId}/tiles/{imgName}'.supplant({
imgName: sourceImage,
questId: questId
}));
var imgSize = sizeOf(imgPath);
Math.log2 = Math.log2 || function(x) {
return Math.log(x) / 0.6931471805599453;
};
var maxZoom = parseInt(Math.log2(Math.max(imgSize.width, imgSize.height) / 256) + 1)
var info = extend(imgSize, {
imgName: sourceImage,
maxZoom: maxZoom
});
return new Promise(function(res, rej) {
var cmd = '{createtiles} "{imgPath}" "{tilesPath}" {maxZoom}'.supplant({
createtiles: createtiles,
imgPath: imgPath,
tilesPath: tilesPath,
maxZoom: maxZoom
});
var p = child_process.exec(cmd);
console.log(cmd);
p.on('close', function() {
res(info);
});
});
}
function getTilesPath(questId, imageName) {
return path.relative(rootPath, path.join(rootPath, 'store', 'quest_' + questId, 'tiles', imageName));
}
function getImagePath(questId, imageName) {
return path.relative(rootPath, path.join(rootPath, 'store', 'quest_' + questId, 'images', imageName));
}
function getThumbPath(questId, imageName) {
return path.relative(rootPath, path.join(rootPath, 'store', 'quest_' + questId, 'images', imageName + '.thumb.jpg'));
}
function createThumb(questId, imageName) {
process.chdir(rootPath);
var image = path.resolve(getImagePath(questId, imageName));
var thumb = path.resolve(getImagePath(questId, imageName) + '.thumb.jpg');
return new Promise(function(res, rej) {
im.convert([image, '-thumbnail', '512x512', thumb], function(err, out) {
if (err) throw err;
res(thumb);
});
});
}
module.exports.tile = tile;
module.exports.getTilesPath = getTilesPath;
module.exports.getThumbPath = getThumbPath;
module.exports.getImagePath = getImagePath;
module.exports.createThumb = createThumb;