|
@@ -1,6 +1,5 @@
|
|
|
var debug = require('debug')('ylt:screenshotHandler');
|
|
|
var Jimp = require('jimp');
|
|
|
-var tmp = require('temporary');
|
|
|
var Q = require('q');
|
|
|
var fs = require('fs');
|
|
|
var path = require('path');
|
|
@@ -8,44 +7,22 @@ var path = require('path');
|
|
|
|
|
|
var screenshotHandler = function() {
|
|
|
|
|
|
- this.getScreenshotTempFile = function() {
|
|
|
-
|
|
|
- var screenshotTmpFolder = new tmp.Dir();
|
|
|
- var tmpFilePath = path.join(screenshotTmpFolder.path, 'screenshot.png');
|
|
|
- var that = this;
|
|
|
-
|
|
|
- return {
|
|
|
-
|
|
|
- getTmpFolder: function() {
|
|
|
- return screenshotTmpFolder;
|
|
|
- },
|
|
|
-
|
|
|
- getTmpFilePath: function() {
|
|
|
- return tmpFilePath;
|
|
|
- },
|
|
|
-
|
|
|
- toThumbnail: function(width) {
|
|
|
- return that.optimize(tmpFilePath, width);
|
|
|
- },
|
|
|
-
|
|
|
- deleteTmpFile: function() {
|
|
|
- return that.deleteTmpFileAndFolder(tmpFilePath, screenshotTmpFolder);
|
|
|
- }
|
|
|
- };
|
|
|
- };
|
|
|
+ var tmpFolderPath = 'tmp';
|
|
|
+ var tmpFolderFullPath = path.join(__dirname, '..', tmpFolderPath);
|
|
|
+ var tmpFileName = 'temp-screenshot.png';
|
|
|
+ var tmpFileFullPath = path.join(tmpFolderFullPath, tmpFileName);
|
|
|
|
|
|
|
|
|
- this.optimize = function(imagePath, width) {
|
|
|
+ this.findAndOptimizeScreenshot = function(width) {
|
|
|
var that = this;
|
|
|
|
|
|
debug('Starting screenshot transformation');
|
|
|
|
|
|
- return this.openImage(imagePath)
|
|
|
+ return this.openImage(tmpFileFullPath)
|
|
|
|
|
|
.then(function(image) {
|
|
|
-
|
|
|
+ that.deleteTmpFile(tmpFileFullPath);
|
|
|
return that.resizeImage(image, width);
|
|
|
-
|
|
|
})
|
|
|
|
|
|
.then(this.addWhiteBackground)
|
|
@@ -147,24 +124,48 @@ var screenshotHandler = function() {
|
|
|
};
|
|
|
|
|
|
|
|
|
- this.deleteTmpFileAndFolder = function(tmpFilePath, screenshotTmpFolder) {
|
|
|
+ this.deleteTmpFile = function(tmpFilePath) {
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
fs.unlink(tmpFilePath, function (err) {
|
|
|
if (err) {
|
|
|
- debug('Screenshot file not found, could not be deleted. But it is not a problem.');
|
|
|
+ debug('Screenshot temporary file not found, could not be deleted. But it is not a problem.');
|
|
|
} else {
|
|
|
- debug('Screenshot file deleted.');
|
|
|
+ debug('Screenshot temporary file deleted.');
|
|
|
}
|
|
|
|
|
|
- screenshotTmpFolder.rmdir();
|
|
|
- debug('Screenshot temp folder deleted');
|
|
|
-
|
|
|
deferred.resolve();
|
|
|
});
|
|
|
|
|
|
return deferred.promise;
|
|
|
};
|
|
|
+
|
|
|
+ // Create a /tmp folder on the project's root directory
|
|
|
+ this.createTmpScreenshotFolder = function() {
|
|
|
+ var deferred = Q.defer();
|
|
|
+
|
|
|
+ // Create the folder if it doesn't exist
|
|
|
+ fs.exists(tmpFolderFullPath, function(exists) {
|
|
|
+ if (exists) {
|
|
|
+ deferred.resolve();
|
|
|
+ } else {
|
|
|
+ debug('Creating the tmp image folder', tmpFolderFullPath);
|
|
|
+ fs.mkdir(tmpFolderFullPath, function(err) {
|
|
|
+ if (err) {
|
|
|
+ deferred.reject(err);
|
|
|
+ } else {
|
|
|
+ deferred.resolve();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return deferred.promise;
|
|
|
+ };
|
|
|
+
|
|
|
+ this.getTmpFileRelativePath = function() {
|
|
|
+ return tmpFolderPath + '/' + tmpFileName;
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
module.exports = new screenshotHandler();
|