add URL shortcuts for app pre-releases

This commit is contained in:
Jeremy Kauffman 2017-04-18 18:12:58 -04:00
parent 8b9fe3297d
commit e2027c3eb2
4 changed files with 66 additions and 30 deletions

View file

@ -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');

View file

@ -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;

View file

@ -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())))

View file

@ -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");
}
}
}