From e2027c3eb2264988a7cf325a1ec9e9ec951ac3cb Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Tue, 18 Apr 2017 18:12:58 -0400 Subject: [PATCH] add URL shortcuts for app pre-releases --- controller/Controller.class.php | 1 + controller/action/DownloadActions.class.php | 25 +++-------- lib/thirdparty/Github.class.php | 50 ++++++++++++++++----- lib/tools/OS.class.php | 20 +++++++++ 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/controller/Controller.class.php b/controller/Controller.class.php index d5888186..6ede319f 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -143,6 +143,7 @@ class Controller } } + $router->any('/get/lbry.pre.{ext:c}', 'DownloadActions::executeGetAppPrereleaseRedirect'); $router->any('/get/lbry.{ext:c}', 'DownloadActions::executeGetAppRedirect'); $router->any('/get/lbrynet.{os:c}.zip', 'DownloadActions::executeGetDaemonRedirect'); diff --git a/controller/action/DownloadActions.class.php b/controller/action/DownloadActions.class.php index 8e450c14..b6359abd 100644 --- a/controller/action/DownloadActions.class.php +++ b/controller/action/DownloadActions.class.php @@ -4,26 +4,15 @@ class DownloadActions extends Actions { public static function executeGetAppRedirect(string $ext) { - $uri = null; - switch ($ext) - { - case 'deb': - $uri = GitHub::getAppDownloadUrl(OS::OS_LINUX); - break; - - case 'dmg': - $uri = GitHub::getAppDownloadUrl(OS::OS_OSX); - break; - - case 'msi': // fallthrough - case 'exe': - $uri = GitHub::getAppDownloadUrl(OS::OS_WINDOWS); - break; - } - - return Controller::redirect($uri ?: '/get', 302); + return Controller::redirect(GitHub::getAppDownloadUrl(OS::getOsForExtension($ext)) ?: '/get', 302); } + public static function executeGetAppPrereleaseRedirect(string $ext) + { + return Controller::redirect(GitHub::getAppPrereleaseDownloadUrl(OS::getOsForExtension($ext)) ?: '/get', 302); + } + + public static function executeGetDaemonRedirect(string $os) { $uri = null; diff --git a/lib/thirdparty/Github.class.php b/lib/thirdparty/Github.class.php index 729e3902..89a7e160 100644 --- a/lib/thirdparty/Github.class.php +++ b/lib/thirdparty/Github.class.php @@ -2,27 +2,35 @@ class Github { - public static function getAppDownloadUrl($os, $cache = true) + protected static function findReleaseDownloadUrl(array $release, string $os) { if (!in_array($os, array_keys(OS::getAll()))) { throw new DomainException('Unknown OS'); } + foreach ($release['assets'] as $asset) + { + $ext = substr($asset['name'], -4); + if ( + ($os == OS::OS_LINUX && ($ext == '.deb' || in_array($asset['content_type'], ['application/x-debian-package', 'application/x-deb']))) || + ($os == OS::OS_OSX && ($ext == '.dmg' || in_array($asset['content_type'], ['application/x-diskcopy', 'application/x-apple-diskimage']))) || + ($os == OS::OS_WINDOWS && $ext == '.exe') + ) + { + return $asset['browser_download_url']; + } + } + } + + public static function getAppDownloadUrl($os, $cache = true) + { try { - $releaseData = static::get('/repos/lbryio/lbry-app/releases/latest', $cache); - foreach ($releaseData['assets'] as $asset) + $release = static::get('/repos/lbryio/lbry-app/releases/latest', $cache); + if ($release) { - $ext = substr($asset['name'], -4); - if ( - ($os == OS::OS_LINUX && ($ext == '.deb' || in_array($asset['content_type'], ['application/x-debian-package', 'application/x-deb']))) || - ($os == OS::OS_OSX && ($ext == '.dmg' || in_array($asset['content_type'], ['application/x-diskcopy', 'application/x-apple-diskimage']))) || - ($os == OS::OS_WINDOWS && $ext == '.exe') - ) - { - return $asset['browser_download_url']; - } + return static::findReleaseDownloadUrl($release, $os); } } catch (Exception $e) @@ -32,6 +40,24 @@ class Github return null; } + public static function getAppPrereleaseDownloadUrl($os, $cache = true) + { + try + { + $releases = static::get('/repos/lbryio/lbry-app/releases', $cache); + if (count($releases)) + { + return static::findReleaseDownloadUrl($releases[0], $os); + } + } + catch (Exception $e) + { + } + + return null; + } + + public static function getDaemonReleaseProperty($os, $property, $isAssetProperty = false, $cache = true) { if (!in_array($os, array_keys(OS::getAll()))) diff --git a/lib/tools/OS.class.php b/lib/tools/OS.class.php index 3c14f0cd..ce00a676 100644 --- a/lib/tools/OS.class.php +++ b/lib/tools/OS.class.php @@ -20,4 +20,24 @@ class OS OS::OS_IOS => ['/ios', 'iOS', 'icon-mobile', '_ios'] ]; } + + public static function getOsForExtension($ext) + { + switch ($ext) + { + case 'deb': + return OS::OS_LINUX; + + case 'dmg': + case 'pkg': + return OS::OS_OSX; + + case 'msi': // fallthrough + case 'exe': + return OS::OS_WINDOWS; + + default: + throw new LogicException("Unknown ext $ext"); + } + } } \ No newline at end of file