Skip to content

Commit

Permalink
manually compiling es6
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Jan 7, 2015
1 parent 4e45433 commit d721d6d
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SRC = $(wildcard lib/*.js)
DEST = $(SRC:lib/%.js=build/%.js)

build: $(DEST)
build/%.js: lib/%.js
mkdir -p $(@D)
./node_modules/.bin/6to5 -b generators $< -o $@

clean:
rm -rf build
47 changes: 47 additions & 0 deletions build/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";

/**
* Module dependencies
*/

var system = require("system");


// arguments
var params = JSON.parse(system.args[1]);

var page = require("webpage").create();

// features
var window = params.window;
var disable = params.disable;
var crop = params.crop;

if (window) {
page.viewportSize = params.window;
}

if (crop) {
page.clipRect = {
top: crop.y,
left: crop.x,
width: crop.width,
height: crop.height
};
}

if (disable) {
if (disable.javascript) {
page.settings.javascriptEnabled = false;
}

if (disable.images) {
page.settings.loadImages = false;
}
}

var url = decodeURIComponent(params.url);
page.open(url, function () {
page.render(params.destPath);
phantom.exit();
});
171 changes: 171 additions & 0 deletions build/thumbbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"use strict";

/**
* Module dependencies
*/

var request = require("co-request");
var Image = require("magician");
var exec = require("co-exec");

var util = require("./util");


/**
* Thumbbot
*/

var Thumbbot = function Thumbbot(path) {
this.path = path;
this.options = {};
};

Thumbbot.prototype.format = function (format) {
this.options.format = format;

return this;
};

Thumbbot.prototype.width = function (width) {
if (!this.options.resize) this.options.resize = {};

this.options.resize.width = width;

return this;
};

Thumbbot.prototype.height = function (height) {
if (!this.options.resize) this.options.resize = {};

this.options.resize.height = height;

return this;
};

Thumbbot.prototype.resize = function (width, height) {
this.options.resize = { width: width, height: height };

return this;
};

Thumbbot.prototype.window = function (width, height) {
this.options.window = { width: width, height: height };

return this;
};

Thumbbot.prototype.seek = function (position) {
this.options.seek = position;

return this;
};

Thumbbot.prototype.crop = function (x, y, width, height) {
this.options.crop = { x: x, y: y, width: width, height: height };

return this;
};

Thumbbot.prototype.disable = function (feature) {
if (!this.options.disable) this.options.disable = {};

this.options.disable[feature] = true;
};

Thumbbot.prototype.type = function () {
var extension = util.parseExtension(this.path);

switch (extension) {
case "mp4":
case "3gp":
case "mov":
case "avi":
return "video";

case "jpeg":
case "jpg":
case "png":
case "gif":
case "tiff":
return "image";

default:
return "page";
}
};

Thumbbot.prototype.saveVideo = function (destPath) {
var seek = this.options.seek || "00:00:01";

var command = "ffmpeg -i \"" + this.path + "\" -ss \"" + seek + "\" -vframes 1 \"" + destPath + "\"";

return exec(command);
};

Thumbbot.prototype.savePage = function (destPath) {
var options = Object.assign({}, this.options);

options.url = encodeURIComponent(this.path);
options.destPath = destPath;
options = JSON.stringify(options);

var command = ["phantomjs", "--disk-cache=true", "--ignore-ssl-errors=true", "--web-security=false", "--ssl-protocol=TLSv1", __dirname + "/render.js", "'" + options + "'"].join(" ");

return exec(command);
};

Thumbbot.prototype.saveImage = function (destPath) {
var image = new Image(this.path);

var options = this.options;
var _resize = options.resize;
var _crop = options.crop;


if (_resize) {
image.resize(_resize.width, _resize.height);
image.fit(options.fit || "clip");
}

if (_crop) {
image.crop(_crop.x, _crop.y, _crop.width, _crop.height);
}

return image.save(destPath);
};

Thumbbot.prototype.save = function* (destPath) {
var _type = this.type();

if (!destPath) {
destPath = util.tmpPath(this.options.format);
}

switch (_type) {
case "video":
yield this.saveVideo(destPath);
break;

case "image":
yield this.saveImage(destPath);
break;

case "page":
yield this.savePage(destPath);
break;

default:
throw new Error("Uknown file type given to Thumbbot");
}

return new Image(destPath);
};




/**
* Module exports
*/

module.exports = Thumbbot;
31 changes: 31 additions & 0 deletions build/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

/**
* Module exports
*/

var exports = module.exports;

exports.clone = clone;
exports.tmpPath = tmpPath;
exports.parseExtension = parseExtension;

/**
* Utilities
*/

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}

function tmpPath(format) {
if (!format) format = "jpg";

var filename = Math.random().toString(36).substring(7);

return "/tmp/" + filename + "." + format;
}

function parseExtension(path) {
return /\.([a-z0-9]{3,5})$/i.exec(path)[1];
}
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
require('6to5/register')({
blacklist: ['generators']
});

module.exports = require('./lib/thumbbot');
module.exports = require('./build/thumbbot');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thumbbot",
"version": "0.4.0",
"version": "0.4.1",
"description": "Thumbnails for video, images and web pages.",
"keywords": [
"thumb",
Expand Down Expand Up @@ -37,12 +37,12 @@
"node": ">= 0.11.x"
},
"dependencies": {
"6to5": "^2.4.8",
"co-exec": "^1.1.0",
"co-request": "^0.2.0",
"magician": "^0.2.0"
},
"devDependencies": {
"6to5": "^2.7.2",
"chai": "^1.10.0",
"co": "^4.0.0",
"mocha": "^2.0.1",
Expand Down

0 comments on commit d721d6d

Please sign in to comment.