-
Notifications
You must be signed in to change notification settings - Fork 58
/
thumbnail.js
107 lines (100 loc) · 4.07 KB
/
thumbnail.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var exports = module.exports = {};
var child = require('child_process');
var fs = require('fs');
exports.save = function save(gistId, imageData, token, cb) {
// imageData is base64 encoded. TODO: more efficient transport?
var data = {
id: gistId,
image: imageData,
token: token
};
// 4 steps to add thumbnail:
// 1) clone gist
// 2) write image to same folder
// 3) commit
// 4) push
cleanGist(data, function(err) {
if (err) return cb(err);
gitClone(data, function(err) {
if (err) return cb(err);
writeImage(data, function(err) {
if (err) return cb(err);
gitCommit(data, function(err) {
if (err) return cb(err);
gitPush(data, function(err) {
if (err) return cb(err);
cleanGist(data, cb);
});
});
});
});
});
};
// Big ups to gistup! https://github.com/mbostock/gistup/blob/master/bin/gistup
function gitClone(data, cb) {
// we need to use our token to avoid rate limiting
// https://github.com/blog/1270-easier-builds-and-deployments-using-git-over-https-and-oauth
// TODO: switch to using git init and putting token in pull url (to avoid token being written to disk)
// var url = "https://" + data.token + "@gist.github.com/" + data.id + ".git"
var url = "https://gist.github.com/" + data.id + ".git";
console.log("cloning", url);
child.exec("cd /tmp; git clone " + url, function(error, stdout, stderr) {
if (!error && stderr && stderr.toLowerCase().indexOf("error") >= 0) {
process.stderr.write(stderr);
error = new Error("git clone failed: ", data.id);
} else if (stderr) { process.stdout.write(stderr); }
if (!error && stdout) process.stdout.write(stdout);
cb(error);
});
}
function gitCommit(data, cb) {
console.log("adding and commiting thumbnail", data.id);
var author = '"Building blocks <enjalot+buildingblocks@gmail.com>"';
child.exec("cd /tmp/" + data.id + "; git add thumbnail.png; git commit --author " + author + " -m 'update thumbnail.png'", function(error, stdout, stderr) {
if (!error && stderr && stderr.toLowerCase().indexOf("error") >= 0) {
process.stderr.write(stderr);
error = new Error("git commit failed.", data.id);
} else if (stderr) { process.stdout.write(stderr); }
if (!error && stdout) process.stdout.write(stdout);
cb(error);
});
}
function gitPush(data, cb) {
// figured this out from here: http://stackoverflow.com/questions/14092636/why-doesnt-my-git-auto-update-expect-script-work
console.log("pushing to master", data.id);
// TODO: switch to using token in push url
var filePath = __dirname + "/scripts/gitpush.expect";
child.execFile(filePath, [ "enjalot", data.token ],
{
cwd: "/tmp/" + data.id
}, function(error, stdout, stderr) {
if (!error && stderr && stderr.toLowerCase().indexOf("error") >= 0) {
process.stderr.write(stderr);
error = new Error("git push failed.", data.id);
} else if (stderr) { process.stdout.write(stderr); }
if (!error && stdout) process.stdout.write(stdout);
cb(error);
});
}
function writeImage(data, cb) {
console.log("writing image", data.id);
// http://stackoverflow.com/questions/6926016/nodejs-saving-a-base64-encoded-image-to-disk
// http://stackoverflow.com/questions/5669541/node-js-how-to-save-base64-encoded-images-on-server-as-png-jpg
console.log("DATA IMAGE", data.image.slice(0, 25));
var base64Data = data.image.replace(/^data:image\/png;base64,/, "");
var binaryData = new Buffer(base64Data, 'base64');
fs.writeFile("/tmp/" + data.id + "/thumbnail.png", binaryData, 'binary', function(err) { // ...
if (err) console.log("IMAGE WRITE ERROR", err);
cb(err);
});
}
function cleanGist(data, cb) {
child.exec("cd /tmp; rm -rf " + data.id, function(error, stdout, stderr) {
if (!error && stderr && stderr.toLowerCase().indexOf("error") >= 0) {
process.stderr.write(stderr);
error = new Error("rm -rf failed.", data.id);
} else if (stderr) { process.stdout.write(stderr); }
if (!error && stdout) process.stdout.write(stdout);
cb(error);
});
}