-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
160 lines (141 loc) · 4.32 KB
/
app.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Module dependencies.
*/
var express = require('express'),
jinjs = require('jinjs'),
gm = require('gm'),
imageMagick = gm.subClass({ imageMagick: true }),
lua = require('./luaparser'),
path = require('path'),
crypto = require('crypto'),
fs = require('fs'),
request = require('request');
// start the app
var app = module.exports = express.createServer();
var baseurl = "https://raw.github.com/kyleconroy/hawkthorne-journey/master/src/"
, characters = [
'abed',
'annie',
'britta',
'buddy',
'chang',
'dean',
'fatneil',
'guzman',
'ian',
'jeff',
'leonard',
'pierce',
'rich',
'shirley',
'troy',
'vaughn',
'vicedean',
'vicki',
];
// Configuration
app.configure(function(){
app.set('views', __dirname + '/templates');
app.set('view engine', 'jinjs');
app.set("view options", { jinjs_pre_compile: function (str) { return parse_pwilang(str); } });
app.set("view options", { layout: false });
app.register('.html', jinjs);
app.use('/static',express.static(__dirname + '/static'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/favicon.ico', function (req,res,next) { res.redirect('/static/favicon.ico'); } );
// Routes
app.get('/big/:path', function (req, res, next) {
var _path = decodeURIComponent( req.params.path ) || false;
if( _path.match( /^https?:\/\// ) ) {
var md5 = crypto.createHash('md5').update(_path).digest('hex'),
ext = _path.substr(_path.lastIndexOf('.') + 1),
cached_file = 'static/cache/' + md5 + '.' + ext,
_cached_file = 'static/cache/_' + md5 + '.' + ext;
if( path.existsSync( cached_file ) ) {
console.log(' found ' + cached_file );
res.redirect( '/' + cached_file );
} else {
console.log(' didnt find ' + cached_file );
// first, we use request to download a local cache of the file
var littleStream = fs.createWriteStream( _cached_file );
console.log(' downloading little version of ' + _path );
request( _path ).pipe( littleStream );
littleStream.on('close',function() {
console.log(' starting imageMagick on local copy ' + _cached_file);
imageMagick( _cached_file )
.quality(100)
.antialias(false)
.size( function( err, value ) {
if( err ) {
res.json(err,404);
} else {
this.scale( value.width * 10, value.height * 10 );
this.stream(function (err, stdout, stderr) {
stderr.setEncoding('utf8');
stderr.on('data',function(data){console.log('stderr',data)});
if (err) {
res.json(err,404);
} else {
var bigStream = fs.createWriteStream( cached_file );
stdout.pipe( bigStream );
stdout.on('end', function() {
console.log(' writing ' + cached_file + ' to cache ');
res.redirect( '/' + cached_file );
});
}
});
}
}
);
});
}
} else {
res.json( "LOL whut?", 404 );
}
});
app.get('/lua/:character', lua);
app.get('/:character/:in_url?', function(req, res) {
var character = req.params.character;
var in_url = req.params.in_url || '';
if( characters.indexOf( character.toLowerCase() ) === -1 ) {
res.send( "I don't know who that character is", 404 );
} else {
url = decodeURIComponent( in_url );
if( url && !url.match( /^https?:\/\//i ) && !url.match( /^data:/i ) ) {
url = "http://" + url;
}
res.render('index.html', {
source: url,
characters: characters,
character: character,
original: baseurl + 'images/characters/' + character + "/base.png"
} );
}
});
app.get('/', function( req, res ) {
res.redirect('/abed');
});
app.post('/*', function( req, res ) {
var character = req.body.character || 'abed',
costume = req.body.url || false,
url = '/' + character;
if( costume ) url += '/' + encodeURIComponent( costume );
res.redirect( url );
});
app.get('*', function( req,res ) { res.send(404); } );
app.dynamicHelpers({
url_for: function() { return function( dir, filename ) { return '/' + dir + '/' + filename; }; }
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});