This repository was archived by the owner on Oct 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 2.06 KB
/
index.js
File metadata and controls
66 lines (53 loc) · 2.06 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
exports = module.exports = render
var photoshop = require('photoshop')
var Readable = require('readable-stream').Readable
var url = require('url')
var fs = require('fs')
var request = require('request')
function render(config){
var stream = new Readable({objectMode:true})
function _error(error){
stream.emit('error', error);
stream.push(null)
}
stream._read = function(size){
stream._read = function(){}
if (!(config && config.uri)) return _error(Error('missing uri GET parameter'));
downloadImage(config.uri, function(error, _path){
if (error) return _error(error);
photoshop.includeFakeDOM().invoke(jsx_placeSmartObject, [_path, config.uri, config.name || config.uri], function(error, layerRef){
if (error) return _error(error);
stream.push(layerRef.identifier.toString())
stream.push(null)
})
})
}
return stream
}
var path = require('path')
function downloadImage(_url, callback){
request.head(_url, function(error, response, body){
if (error) return callback(error);
var ext = response.headers['content-type'] && response.headers['content-type'].split('/')[1]
if (!ext) ext = path.extname(response.req.pathname).replace('.','');
if (!ext) ext = 'png';
var _path = process.env.TMPDIR + '/TemporaryItems/' + encodeURIComponent(_url) + '.' + ext
var stream = request(_url)
stream.pipe(fs.createWriteStream(_path))
stream.on('end', function(){ callback(null, _path) })
stream.on('error', callback)
})
}
function jsx_placeSmartObject(sourcePath, sourceURI, layerName){
if (app.documents.length === 0) PSFakeDOM.makeDocument();
return FakeDocument.$0().doWriteTransaction(function(doc){
PSFakeDOM.makeLayer()
PSFakeDOM.newPlacedLayer()
var layerRef = PSFakeDOM.layerRefForActiveLayer()
PSFakeDOM.setLayer_source(layerRef, sourcePath)
PSFakeDOM.setLayer_sourceMeta(layerRef, sourceURI)
app.activeDocument.activeLayer.name = layerName
return layerRef
}, layerName || 'Insert Layer')
}
if (!module.parent) require('./test');