Created
January 26, 2015 20:37
-
-
Save nickgartmann/b3f2632cd77315a9ce13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require( 'fs' ), | |
Encoder = require( 'node-html-encoder' ).Encoder, | |
MachineGroupService = require( '../../core/services/MachineGroupService.js' ), | |
ApplicationService = require( '../../core/services/ApplicationService.js' ), | |
path = require( 'path' ), | |
walk = require('../utilities/DirectoryWalker.js'), | |
ApplicationService = require( '../../core/services/ApplicationService.js' ), | |
appService = new ApplicationService(), | |
uaParser = require('ua-parser'); | |
function matchUserAgent(userAgent, browserSpec) { | |
if(browserSpec.os !== userAgent.os.family) { | |
return false; | |
} | |
else if(browserSpec.browser != userAgent.ua.family) { | |
return false; | |
} | |
if(browserSpec.version > userAgent.ua.major) { | |
return false; | |
} | |
return true; | |
} | |
function matchesAnyUserAgent(useragents, useragent) { | |
var t = false; | |
useragents.forEach(function(agent) { | |
if(matchUserAgent(useragent, agent)) { | |
t = true; | |
} | |
}); | |
return t; | |
} | |
// | |
// Route Methods | |
// | |
exports.index = function( req, res ) { | |
// Set application to the value of the query string | |
var application = req.query[ 'app' ]; | |
if ( application == null && req.params.app && req.params.app.indexOf( '.' ) < 0 ) { | |
application = req.params.app; | |
} | |
// Application was in the query string or route | |
// Use the application name to find the Application ID | |
if ( application != null ) { | |
var serv = new ApplicationService(); | |
serv.getByRoute( application, function( result ) { | |
if ( result != null ) { | |
// Save the cookie for the next go around | |
res.cookie( 'app', result.id ); | |
_renderIndex( result.id, req, res ); | |
} else { | |
_getAppIDFromCookieOrMachine( req, function( app ) { | |
if ( app == null ) { | |
app = 1; | |
_renderIndex( app, req, res ); | |
} else { | |
// Save the cookie for the next go around | |
res.cookie( 'app', app ); | |
_renderIndex( app, req, res ); | |
} | |
}); | |
} | |
}); | |
} else { | |
// No Query string given so pull it out of | |
// the cookie. If not provided use the machines | |
// default by looking it up via IP Address | |
_getAppIDFromCookieOrMachine( req, function( app ) { | |
if ( app == null ) { | |
_renderIndex( 1, req, res ); | |
} else { | |
// Save the cookie for the next go around | |
res.cookie( 'app', app ); | |
_renderIndex( app, req, res ); | |
} | |
}); | |
} | |
} | |
exports.resource = function( req, res ) { | |
res.setHeader( 'Content-Type', 'text/javascript' ); | |
res.setHeader( 'Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0' ); | |
var body = 'var resx = []; function ResourceKey( key ){ var res = resx[key]; return (res == null) ? key : res; }'; | |
_determineLanguageCode( req, res, function( lang ) { | |
var cultures = lang.split('-'); | |
if ( cultures.length > 1 ){ | |
_readResource( cultures[0], function( languageKeys ) { | |
body += languageKeys; | |
_readResource( cultures[0] + '-' + cultures[1], function( cultureKeys ) { | |
body += cultureKeys; | |
res.setHeader( 'Content-Length', body.length ); | |
res.end( body ); | |
}); | |
}); | |
} else { | |
_readResource( cultures[0], function( keyValues2 ) { | |
body += keyValues2; | |
res.setHeader( 'Content-Length', body.length ); | |
res.end( body ); | |
}); | |
} | |
}); | |
} | |
exports.albumImage = function( req, res ) { | |
var filePath = path.normalize( __dirname + '/../public/img/meta/albums/' + req.params.id ); | |
path.exists( filePath, function( exists ) { | |
if ( !exists ) { | |
filePath = path.normalize( __dirname + '/../public/img/meta/untitled.jpg' ); | |
} | |
res.download( filePath ); | |
}); | |
} | |
exports.videoImage = function( req, res ) { | |
var filePath = path.normalize( __dirname + '/../public/img/meta/video/' + req.params.id ); | |
path.exists( filePath, function( exists ) { | |
if ( !exists ) { | |
filePath = path.normalize( __dirname + '/../public/img/meta/untitledVideo.jpg' ); | |
} | |
res.download( filePath ); | |
}); | |
} | |
exports.downloadFile = function( req, res ) { | |
res.download( req.params.id ); | |
} | |
// | |
// Helper Methods | |
// | |
function _determineLanguageCode( req, res, callback ) { | |
var lang = req.cookies.lang; | |
// Language Code was not defined in the cookie | |
if ( lang == null ) { | |
var path = __dirname + '/..'; | |
if( global.clientPath ) { | |
path = global.clientPath; | |
} | |
path += "/resources/resource.json"; | |
var data = fs.readFileSync( path ); | |
if ( data != null ) { | |
// Load the Default Language Code | |
var file = JSON.parse( data ); | |
lang = file.default; | |
} | |
if ( lang == null ) { | |
// Default Language Code was not defined, or | |
// the resource.json could not be parsed | |
// default to Browser specified language code | |
lang = req.headers[ 'accept-language' ].split( ';' )[0].split( ',' )[0]; | |
} | |
if ( lang == null ) { | |
// Last resort is to default to english | |
lang = 'en-us'; | |
} | |
} | |
res.cookie( 'lang', lang ); | |
callback && callback( lang ); | |
} | |
function _isRequestMobile( req ) { | |
var ua = req.headers[ 'user-agent' ]; | |
if ( /mobile/i.test( ua ) ) { | |
if( /iPad/.test( ua ) ) { | |
return false; | |
} | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
function _getAppIDFromCookieOrMachine( req, callback ){ | |
// Query string wasn't provided for the application | |
// Use the cookie | |
if ( req.cookies.app != null ) { | |
callback && callback( req.cookies.app ); | |
} else { | |
var serv = new MachineGroupService(); | |
serv.getByIPAddress( req.connection.remoteAddress, function( machine ){ | |
// Cookie was empty or not set previously, | |
// Get the machines default application | |
if ( machine != null ) { | |
callback && callback( machine.application ); | |
} else { | |
// Machine doesn't have a default application | |
callback && callback( null ); | |
} | |
}); | |
} | |
} | |
function _determineMobileOrDesktop( req, res, defaultRoute, callback ) { | |
// Let QueryString Route override mobile/desktop | |
var route = req.query[ 'route' ]; | |
if ( route != null ) { | |
res.cookie( 'route', route ); | |
} | |
// If QueryString wasn't set get the cookie value | |
if ( route == null ){ | |
route = req.cookies.route; | |
} | |
if (route == null) { | |
route = defaultRoute; | |
} | |
// If there was no cookie, determine if it's mobile | |
if ( route == null ) { | |
if ( _isRequestMobile( req )) { | |
route = 'mobile'; | |
} else { | |
route = 'desktop'; | |
} | |
} | |
if ( route.toLowerCase() == 'desktop' ) { | |
route = ''; | |
} | |
callback( route ); | |
} | |
function _renderIndex( application, req, res ) { | |
var model = {}; | |
model.application = application; | |
model.unsupportedBrowser = false; | |
var userAgent = uaParser.parse(req.headers['user-agent']); | |
var lang = req.query[ 'lang' ]; | |
if ( lang != null ) res.cookie( 'lang', lang ); | |
var serv = new ApplicationService(); | |
serv.getByID( application, function( result ) { | |
var defaultRoute = result.defaultDevice; | |
_determineMobileOrDesktop( req, res, defaultRoute, function( route ){ | |
(global.supportedBrowsers || []).forEach(function(browser) { | |
if(!matchesAnyUserAgent(global.supportedBrowsers, userAgent)) { | |
model.unsupportedBrowser = true; | |
model.unsupportedBrowserTimeout = global.unsupportedBrowserTimeout; | |
model.unsupportedBrowserMessage = global.unsupportedBrowserMessage || "You are using an unsupported browser."; | |
} else { | |
model.unsupportedBrowser = false; | |
} | |
}); | |
var path = __dirname + '/..'; | |
if( global.clientPath ) { | |
path = global.clientPath; | |
} | |
path += '/resources/resource.json'; | |
var data = fs.readFileSync( path ); | |
if ( data != null ) { | |
var file = JSON.parse( data ); | |
model.maps = file.languages; | |
res.render( 'index' + route, model ); | |
} | |
else { | |
res.render( 'index' + route, model ); | |
} | |
}); | |
}); | |
} | |
function _readResource( lang, callback ){ | |
var body = ''; | |
var path = __dirname + '/..'; | |
if( global.clientPath ) { | |
path = global.clientPath; | |
} | |
path += '/resources/resource.' + lang.toLowerCase() + '.json'; | |
fs.readFile( path, function( err, data ){ | |
if ( !err ) { | |
var file = JSON.parse( data ), | |
encoder = new Encoder( 'entity' ); | |
Object.keys( file ).forEach(function( key ){ | |
body += 'resx[ "' + key + '" ] = "' + encoder.htmlEncode( file[key] ) + '";'; | |
}); | |
callback && callback( body ); | |
} else { | |
callback && callback( body ); | |
} | |
}); | |
} | |
function getExtension(filename) { | |
var i = filename.lastIndexOf('.'); | |
return (i < 0) ? '' : filename.substr(i); | |
} | |
exports.slideshow = function(req, res) { | |
if ( !global.userUploadPath ) { | |
res.render( 'slideshow', { images: [] } ); | |
return; | |
} | |
// pull upload images and pass as model | |
walk(global.userUploadPath, function( err, results ){ | |
if ( err || !results ) { | |
res.render( 'slideshow', { images: [] } ); | |
return; | |
} | |
var extensions = [ '.png', '.bmp', '.gif', '.jpg', '.jpeg' ]; | |
var images = []; | |
var i = 0; | |
for(;i < results.length; i++){ | |
// only push if the file extension is in extension types | |
if( extensions.indexOf( getExtension( results[i].toLowerCase() ) ) > -1 ) { | |
images.push("'/upload/" + results[i].substring(global.userUploadPath.length + 1).split("'").join("%27").split("/").join("%2F") + "'"); | |
} | |
} | |
res.render( 'slideshow', { images: images } ); | |
}); | |
} | |
exports.uploadedPicture = function(req, res){ | |
var filePath = global.userUploadPath + req.params.picture; | |
res.download( filePath ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment