diff --git a/lib/nuts.js b/lib/nuts.js index 1307c35..a371dd8 100644 --- a/lib/nuts.js +++ b/lib/nuts.js @@ -141,12 +141,8 @@ Nuts.prototype.onDownload = function(req, res, next) { if (!filename) { // Detect platform from useragent if (!platform) { - if (req.useragent.isMac) platform = platforms.OSX; - if (req.useragent.isWindows) platform = platforms.WINDOWS; - if (req.useragent.isLinux) platform = platforms.LINUX; - if (req.useragent.isLinux64) platform = platforms.LINUX_64; + platform = platforms.detectPlatformByUserAgent(req.useragent) } - if (!platform) { res.status(400).send('No platform specified and impossible to detect one'); return; diff --git a/lib/utils/platforms.js b/lib/utils/platforms.js index 5ee5f79..fde3b46 100644 --- a/lib/utils/platforms.js +++ b/lib/utils/platforms.js @@ -18,7 +18,8 @@ var platforms = { WINDOWS_32: 'windows_32', WINDOWS_64: 'windows_64', - detect: detectPlatform + detect: detectPlatform, + detectPlatformByUserAgent: detectPlatformByUserAgent }; // Reduce a platfrom id to its type @@ -71,6 +72,15 @@ function detectPlatform(platform) { return _.compact([prefix, suffix]).join('_'); } +function detectPlatformByUserAgent(useragent) { + if (useragent.isMac) return platforms.OSX; + else if (useragent.source.indexOf("WOW64") !== -1 || useragent.source.indexOf("Win64") !== -1) return platforms.WINDOWS_64; + else if (useragent.isWindows) return platforms.WINDOWS; + else if (useragent.isLinux) return platforms.LINUX; + else if (useragent.isLinux64) return platforms.LINUX_64; + else return null +} + function hasSuffix(str, suffix) { return str.slice(str.length-suffix.length) === suffix; } @@ -131,6 +141,7 @@ function resolveForVersion(version, platformID, opts) { module.exports = platforms; module.exports.detect = detectPlatform; +module.exports.detectPlatformByUserAgent = detectPlatformByUserAgent; module.exports.satisfies = satisfiesPlatform; module.exports.toType = platformToType; module.exports.resolve = resolveForVersion; diff --git a/test/platforms.js b/test/platforms.js index 7aadaec..10050f0 100644 --- a/test/platforms.js +++ b/test/platforms.js @@ -1,5 +1,6 @@ require('should'); var platforms = require('../lib/utils/platforms'); +var useragent = require('express-useragent'); describe('Platforms', function() { @@ -16,6 +17,14 @@ describe('Platforms', function() { platforms.detect('RELEASES').should.be.exactly(platforms.WINDOWS_32); }); + it('should detect windows_64', function() { + platforms.detect('MyApp-x64.exe').should.be.exactly(platforms.WINDOWS_64); + var chrome = useragent.parse('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36') + platforms.detectPlatformByUserAgent(chrome).should.be.exactly(platforms.WINDOWS_64); + var edge = useragent.parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586') + platforms.detectPlatformByUserAgent(edge).should.be.exactly(platforms.WINDOWS_64); + }); + it('should detect linux', function() { platforms.detect('enterprise-amd64.tar.gz').should.be.exactly(platforms.LINUX_64); platforms.detect('enterprise-amd64.tgz').should.be.exactly(platforms.LINUX_64);