always link to latest download urls from github

This commit is contained in:
Alex Grintsvayg 2016-06-13 13:38:02 -04:00
parent fc0a874a23
commit 16eeb77e9d
5 changed files with 200 additions and 4 deletions

View file

@ -67,8 +67,10 @@ class Controller
case '/dl/lbry_setup.sh':
return ['internal/dl-not-supported', ['_no_layout' => true]];
case '/lbry-osx-latest.dmg':
// THIS ROUTE IS DEPRECATED. IT WILL BE REMOVED SOON.
return static::redirect('https://github.com/lbryio/lbry/releases/download/v0.2.5/lbry.0.2.5.dmg', 307);
case '/lbry-linux-latest.deb':
// THIS ROUTE IS DEPRECATED. IT WILL BE REMOVED SOON.
return static::redirect('https://github.com/lbryio/lbry/releases/download/v0.2.5/lbry_0.2.5_amd64.deb', 307);
case '/art':
return static::redirect('/what');

View file

@ -37,7 +37,7 @@ class DownloadActions extends Actions
'osTitle' => $osTitle,
'osIcon' => $osIcon,
'downloadHtml' => View::exists('download/' . $partial) ?
View::render('download/' . $partial) :
View::render('download/' . $partial, ['downloadUrl' => static::getDownloadUrl($os)]) :
false
]];
}
@ -89,6 +89,54 @@ class DownloadActions extends Actions
return null;
}
protected static function getDownloadUrl($os, $useCache = true)
{
if (!in_array($os, array_keys(static::getOses())))
{
throw new DomainException('Unknown OS');
}
$apc = $useCache && extension_loaded('apc') && ini_get('apc.enabled');
$key = 'lbry_release_data';
$releaseData = null;
if ($apc)
{
$releaseData = apc_fetch($key);
}
if (!$releaseData)
{
try
{
$releaseData = json_decode(Curl::get('https://api.github.com/repos/lbryio/lbry/releases/latest', [], ['user_agent' => 'LBRY']), true);
if ($apc)
{
apc_store($key, $releaseData, 600); // cache for 10 min
}
}
catch (Exception $e)
{
}
}
if (!$releaseData)
{
return null;
}
foreach($releaseData['assets'] as $asset)
{
if ($os == static::OS_LINUX && $asset['content_type'] == 'application/x-debian-package' ||
$os == static::OS_OSX && $asset['content_type'] == 'application/x-diskcopy')
{
return $asset['browser_download_url'];
}
}
return null;
}
// protected static function validateDownloadAccess()
// {
// $seshionKey = 'has-get-access';

144
lib/tools/Curl.class.php Normal file
View file

@ -0,0 +1,144 @@
<?php
class Curl
{
const GET = 'GET';
const POST = 'POST';
public static function get($url, $params = [], $options = [])
{
list ($status, $headers, $body) = static::doCurl(static::GET, $url, $params, $options);
return $body;
}
public static function post($url, $params = [], $options = [])
{
list ($status, $headers, $body) = static::doCurl(static::POST, $url, $params, $options);
return $body;
}
public static function doCurl($method, $url, $params = [], $options = [])
{
$defaults = [
'headers' => [],
'verify' => true,
'timeout' => 5,
'follow_redirects' => true,
'user_agent' => null,
'proxy' => null,
'password' => null,
'cookie' => null,
'json_post' => false,
];
$invalid = array_diff_key($options, $defaults);
if ($invalid)
{
throw new DomainException('Invalid curl options: ' . join(', ', array_keys($invalid)));
}
$options = array_merge($defaults, $options);
if (!in_array($method, [static::GET, static::POST]))
{
throw new DomainException('Invalid method: ' . $method);
}
if ($options['headers'] && $options['headers'] !== array_values($options['headers'])) // associative array
{
throw new DomainException('Headers must not be an associative array. Its a simple array with values of the form "Header: value"');
}
$ch = curl_init();
if ($method == static::GET && $params)
{
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params);
}
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $options['headers'],
CURLOPT_RETURNTRANSFER => true,
// CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => $options['follow_redirects'],
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $options['timeout'],
CURLOPT_SSL_VERIFYPEER => $options['verify'],
// CURLOPT_SSL_VERIFYHOST => $options['verify'] ? 2 : 0, // php doc says to always keep this at 2 in production environments
CURLOPT_USERAGENT => $options['user_agent'],
]);
if ($method == static::POST)
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options['json_post'] ? json_encode($params) : http_build_query($params));
}
if ($options['proxy'])
{
curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
if ($options['password'])
{
curl_setopt($ch, CURLOPT_USERPWD, $options['password']);
}
if ($options['cookie'])
{
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
$startingResponse = false;
$headers = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $h) use (&$headers, &$startingResponse)
{
$value = trim($h);
if ($value === '')
{
$startingResponse = true;
}
elseif ($startingResponse)
{
$startingResponse = false;
$headers = [$value];
}
else
{
$headers[] = $value;
}
return strlen($h);
});
$responseContent = curl_exec($ch);
$statusCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch))
{
throw new CurlException($ch);
}
curl_close($ch);
return [$statusCode, $headers, $responseContent];
}
}
class CurlException extends Exception
{
protected $errno, $error, $info, $handle;
public function __construct($curlHandle, Exception $previous = null)
{
$this->handle = $curlHandle;
$this->errno = curl_errno($curlHandle);
$this->error = curl_error($curlHandle);
$this->info = curl_getinfo($curlHandle);
parent::__construct($this->error, $this->errno, $previous);
}
}

View file

@ -1,6 +1,7 @@
<div class="text-center">
<p>
<a id="linux-download" class="btn-alt" download href="//lbry.io/lbry-linux-latest.deb"
<a id="linux-download" class="btn-alt" <?php echo $downloadUrl ? 'download' : '' ?>
href="<?php echo $downloadUrl ?: 'https://github.com/lbryio/lbry/releases' ?>"
<?php /*
data-facebook-track-id="XXXXX"
data-twitter-track-id="XXXXX"
@ -12,7 +13,7 @@
</p>
<div class="meta">
Works with Ubuntu, Debian, or any distro with <code>apt</code> or <code>dpkg</code>.
Prefer to build from source? Go <a href="//github.com/lbryio/lbry" class="link-primary">here</a>.
Prefer to build from source? Go <a href="https://github.com/lbryio/lbry" class="link-primary">here</a>.
</div>
</div>

View file

@ -1,6 +1,7 @@
<div class="text-center">
<p>
<a class="btn-alt" href="//lbry.io/lbry-osx-latest.dmg"
<a class="btn-alt" <?php echo $downloadUrl ? 'download' : '' ?>
href="<?php echo $downloadUrl ?: 'https://github.com/lbryio/lbry/releases' ?>"
<?php /*
data-facebook-track-id="XXXXX"
data-twitter-track-id="XXXXX"