Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/nuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion lib/utils/platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
9 changes: 9 additions & 0 deletions test/platforms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('should');
var platforms = require('../lib/utils/platforms');
var useragent = require('express-useragent');

describe('Platforms', function() {

Expand All @@ -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);
Expand Down