2017-04-07 01:29:05 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-12 18:40:58 +02:00
|
|
|
const db = require('../models/db');
|
|
|
|
const config = require('config');
|
|
|
|
const phantom = require('node-phantom-simple');
|
|
|
|
const os = require('os');
|
2017-04-07 01:29:05 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
// type = "pdf" or "png"
|
|
|
|
takeScreenshot: function(space,type,on_success,on_error) {
|
|
|
|
var spaceId = space._id;
|
|
|
|
var space_url = config.get("endpoint")+"/api/spaces/"+spaceId+"/html";
|
|
|
|
|
2018-04-12 18:40:58 +02:00
|
|
|
var export_path = os.tmpdir()+"/"+spaceId+"."+type;
|
2017-04-07 01:29:05 +02:00
|
|
|
|
|
|
|
var timeout = 5000;
|
|
|
|
if (type=="pdf") timeout = 30000;
|
|
|
|
|
|
|
|
space_url += "?api_token="+config.get("phantom_api_secret");
|
|
|
|
|
|
|
|
console.log("[space-screenshot] url: "+space_url);
|
|
|
|
console.log("[space-screenshot] export_path: "+export_path);
|
|
|
|
|
|
|
|
var on_success_called = false;
|
|
|
|
|
|
|
|
var on_exit = function(exit_code) {
|
|
|
|
if (exit_code>0) {
|
2018-04-12 18:40:58 +02:00
|
|
|
console.error("phantom abnormal exit for url "+space_url);
|
2017-04-07 01:29:05 +02:00
|
|
|
if (!on_success_called && on_error) {
|
|
|
|
on_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
phantom.create({ path: require('phantomjs-prebuilt').path }, function (err, browser) {
|
2018-04-12 18:40:58 +02:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
} else {
|
2017-04-07 11:55:07 +02:00
|
|
|
return browser.createPage(function (err, page) {
|
|
|
|
console.log("page created, opening ",space_url);
|
2017-04-07 01:29:05 +02:00
|
|
|
|
2017-04-07 11:55:07 +02:00
|
|
|
if (type=="pdf") {
|
|
|
|
var psz = {
|
2018-04-12 18:40:58 +02:00
|
|
|
width: space.width+"px",
|
|
|
|
height: space.height+"px"
|
2017-04-07 11:55:07 +02:00
|
|
|
};
|
|
|
|
page.set('paperSize', psz);
|
|
|
|
}
|
2017-04-07 01:29:05 +02:00
|
|
|
|
2017-04-07 11:55:07 +02:00
|
|
|
page.set('settings.resourceTimeout',timeout);
|
|
|
|
page.set('settings.javascriptEnabled',false);
|
2017-04-07 01:29:05 +02:00
|
|
|
|
2017-04-07 11:55:07 +02:00
|
|
|
return page.open(space_url, function (err,status) {
|
|
|
|
page.render(export_path, function() {
|
|
|
|
on_success_called = true;
|
|
|
|
if (on_success) {
|
|
|
|
on_success(export_path);
|
|
|
|
}
|
|
|
|
page.close();
|
|
|
|
browser.exit();
|
|
|
|
});
|
2017-04-07 01:29:05 +02:00
|
|
|
});
|
2017-04-07 11:55:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:29:05 +02:00
|
|
|
}, {
|
|
|
|
onExit: on_exit
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|