forked from shy2850/node-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsd.js
More file actions
48 lines (44 loc) · 1.55 KB
/
psd.js
File metadata and controls
48 lines (44 loc) · 1.55 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
"use strict";
var url = require('url'),
mime = require('mime'),
fs = require('fs');
function saveAllLayers( psd, path, resp){
try{
fs.mkdir( path.replace(/([^\\\/]*?)\.png$/i,'$1') ,function(){
psd.layers.forEach(function(t, i){
t.image.saveAsPng( path.replace(/([^\\\/]*?)\.png$/i,'$1/' + i + '.' + t.legacyName.replace(/\W+/g,'_') + '.png') );
});
});
var str = JSON.stringify( psd.tree().export(), undefined, 2 ); //JSON.stringify(psd.layers, undefined, 4);
fs.writeFile( path.replace(/([^\\\/]*?)\.png$/i,'$1/layers.json'), str);
}catch(e){
resp.writeHead(500, {"Content-Type": "text/html"});
resp.end( e.stack.toString().replace(/\n/g,"<br>") );
}
}
function getPng(path, pngPath, resp){
var psd;
fs.readFile(pngPath, function(err, data){
if(err){
require('psd').open(path).then(function(p){
psd = p;
return p.image.saveAsPng( pngPath );
}).then(function(){
setTimeout(function(){
saveAllLayers( psd, pngPath, resp);
},0);
getPng(path, pngPath, resp);
});
}else{
resp.writeHead(200, {
"Content-Type": mime.get('png')
});
resp.end(data);
}
});
}
exports.execute = function(req, resp, root){
var path = root + decodeURI( url.parse( req.url ).query),
pngPath = path.replace(/([^\\\/]*?)\.psd$/i,'$1.png');
getPng(path, pngPath, resp);
};