Files

219 lines
7.2 KiB
PHP
Raw Permalink Normal View History

2016-04-25 07:54:42 -04:00
<?php
class DownloadActions extends Actions
{
2018-09-17 14:28:25 -04:00
//bad, fix me!
2021-05-14 20:10:54 +08:00
public const ANDROID_STORE_URL = 'https://play.google.com/store/apps/details?id=io.lbry.browser';
public const IOS_STORE_URL = 'https://apps.apple.com/us/app/odysee/id1539444143';
2018-09-17 14:28:25 -04:00
2018-12-03 17:31:33 -05:00
public static function executeDownloadPrereleaseAsset(string $repo, string $ext)
{
2018-12-03 17:31:33 -05:00
return static::executeDownloadReleaseAsset($repo, $ext, true);
}
2018-12-03 17:31:33 -05:00
public static function executeDownloadReleaseAsset(string $repo, string $ext, bool $allowPrerelease = false)
2018-05-21 11:49:15 -04:00
{
return Controller::redirect(Github::getRepoReleaseUrl($repo, $ext, $allowPrerelease) ?: '/get', 302);
}
2019-09-13 15:07:46 -04:00
public static function executeDownloadSnapshot(string $type)
{
if (!in_array($type, ['blockchain', 'wallet'])) {
2021-05-14 20:10:54 +08:00
return ['page/404'];
2019-09-13 15:07:46 -04:00
}
$bucketName = "snapshots.lbry.com";
$bucket = S3::getBucket($bucketName, "$type/");
if (!count($bucket)) {
2021-05-14 20:10:54 +08:00
return ['page/404'];
2019-09-13 15:07:46 -04:00
}
2019-09-13 17:02:54 -04:00
krsort($bucket);
2019-09-13 15:07:46 -04:00
2019-10-02 10:08:24 -04:00
return Controller::redirect("https://$bucketName/" . array_keys($bucket)[0], 302);
2019-09-13 15:07:46 -04:00
}
2018-09-17 14:28:25 -04:00
/*
* this is a quick fix to add android, prob not proper design
*/
public static function getGetTemplateParams($os)
{
2021-05-14 20:10:54 +08:00
$osChoices = OS::getAll();
list($uri, $osTitle, $osIcon, $title) = $osChoices[$os];
$params = [
2020-01-22 12:13:37 -05:00
'preferredExt' => $os === Os::OS_LINUX ? 'AppImage' : '',
2021-02-16 12:58:36 -05:00
'title' => $title,
2018-09-17 14:28:25 -04:00
'osTitle' => $osTitle,
'osIcon' => $osIcon,
2019-08-27 11:05:40 -04:00
'osScreenshotSrc' => 'https://spee.ch/b/desktop-035-og.jpeg',
2018-09-17 14:28:25 -04:00
'os' => $os
];
2021-05-14 20:10:54 +08:00
if ($os === OS::OS_ANDROID) {
$params['downloadUrl'] = static::ANDROID_STORE_URL;
$params['osScreenshotSrc'] = 'https://spee.ch/@lbry:3f/android-08-homepage.gif';
} elseif ($os === OS::OS_IOS) {
$params['downloadUrl'] = static::IOS_STORE_URL;
$params['osScreenshotSrc'] = 'https://spee.ch/odyseeiosimage.png';
} else {
$asset = Github::getRepoAsset(Github::REPO_LBRY_DESKTOP, $os, $params['preferredExt']);
2021-05-14 20:10:54 +08:00
$params['downloadUrl'] = $asset ? $asset['browser_download_url'] : null;
}
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
return $params;
2018-09-17 14:28:25 -04:00
}
2018-05-21 11:49:15 -04:00
public static function executeGet()
{
$osChoices = OS::getAll();
$os = static::guessOS();
2018-09-17 14:28:25 -04:00
if (isset($os) && isset($osChoices[$os]) && !Request::getParam('showall')) {
return ['download/get', static::getGetTemplateParams($os)];
2018-05-21 11:49:15 -04:00
} else {
return ['download/get-no-os'];
}
}
public static function prepareListPartial(array $vars)
{
return $vars + ['osChoices' => isset($vars['excludeOs']) ?
2016-10-12 11:37:13 -04:00
array_diff_key(OS::getAll(), [$vars['excludeOs'] => null]) :
OS::getAll()
2016-04-25 07:54:42 -04:00
];
}
2018-05-21 11:49:15 -04:00
protected static function guessOs()
{
2018-05-21 11:49:15 -04:00
//if exact OS is requested, use that
$uri = Request::getRelativeUri();
foreach (OS::getAll() as $os => $osChoice) {
if ($osChoice[0] == $uri) {
return $os;
}
}
2018-05-21 11:49:15 -04:00
if (Request::isRobot()) {
return null;
}
2017-03-14 18:48:18 -04:00
2018-05-21 11:49:15 -04:00
//otherwise guess from UA
$ua = Request::getUserAgent();
if (stripos($ua, 'OS X') !== false) {
return strpos($ua, 'iPhone') !== false || stripos($ua, 'iPad') !== false ? OS::OS_IOS : OS::OS_OSX;
}
if (stripos($ua, 'Linux') !== false || strpos($ua, 'X11') !== false) {
return strpos($ua, 'Android') !== false ? OS::OS_ANDROID : OS::OS_LINUX;
}
if (stripos($ua, 'Windows') !== false) {
return OS::OS_WINDOWS;
2017-03-14 18:48:18 -04:00
}
}
2018-05-21 11:49:15 -04:00
protected static function getEmailParam()
{
$email = Request::getParam('e');
2018-04-09 21:46:04 -04:00
2018-05-21 11:49:15 -04:00
if (!$email) {
$encoded = Request::getParam('ec');
if ($encoded) {
$email = Smaz::decode(Encoding::base64DecodeUrlsafe($encoded), Smaz::CODEBOOK_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email = null;
}
}
}
2018-04-09 21:46:04 -04:00
2018-05-21 11:49:15 -04:00
return $email;
}
2018-04-09 21:46:04 -04:00
2018-05-21 11:49:15 -04:00
public static function prepareDownloadButtonPartial(array $vars)
{
$osChoices = OS::getAll();
2018-04-09 21:46:04 -04:00
2018-05-21 11:49:15 -04:00
$os = static::guessOS();
if ($os && isset($osChoices[$os])) {
2020-01-22 12:13:37 -05:00
list($uri, $osTitle, $osIcon, $oldButtonLabel, $analyticsLabel) = $osChoices[$os];
2018-05-21 11:49:15 -04:00
2021-02-16 12:58:36 -05:00
if ($os === OS::OS_ANDROID) {
2021-05-14 20:10:54 +08:00
$asset = ['browser_download_url' => static::ANDROID_STORE_URL];
} elseif ($os === OS::OS_IOS) {
$asset = ['browser_download_url' => static::IOS_STORE_URL];
2021-02-16 12:58:36 -05:00
} else {
$asset = Github::getRepoAsset(Github::REPO_LBRY_DESKTOP, $os, $vars['preferredExt'] ?? '');
2018-09-17 14:28:25 -04:00
}
$assetUrl = $asset['browser_download_url'] ?? null;
2021-02-16 12:58:36 -05:00
$buttonLabel = __('Download for %os%', ['%os%' => $osTitle]);
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
if (isset($vars['preferredExt']) && $vars['preferredExt']) {
$buttonLabel = __('Download .%ext%', ['%ext%' => $vars['preferredExt']]);
}
2020-01-22 12:13:37 -05:00
2021-05-14 20:10:54 +08:00
$vars += [
2018-09-17 14:28:25 -04:00
'analyticsLabel' => $analyticsLabel,
'buttonLabel' => $buttonLabel,
2020-01-22 12:13:37 -05:00
'isDownload' => true,
'downloadUrl' => $assetUrl,
2018-09-17 14:28:25 -04:00
'os' => $os,
2020-01-22 12:13:37 -05:00
'skipRender' => isset($vars['preferredExt']) && $vars['preferredExt'] &&
(!$assetUrl || !str_ends_with($assetUrl, $vars['preferredExt'])),
2018-09-17 14:28:25 -04:00
'isAuto' => Request::getParam('auto'),
];
2020-01-22 12:13:37 -05:00
if ($os === OS::OS_LINUX && !isset($vars['preferredExt'])) {
2021-05-14 20:10:54 +08:00
$vars['isDownload'] = false;
$vars['downloadUrl'] = '/linux';
2020-01-22 12:13:37 -05:00
}
2018-05-21 11:49:15 -04:00
}
2018-04-09 21:46:04 -04:00
return $vars + [
'analyticsLabel' => '',
'buttonLabel' => __('Download'),
'isDownload' => false,
'downloadUrl' => null,
'os' => null,
'skipRender' => false,
'isAuto' => false,
];
2018-05-21 11:49:15 -04:00
}
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
public static function prepareMetaPartial(array $vars)
{
$osChoices = OS::getAll();
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
$os = static::guessOS();
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
if ($os && isset($osChoices[$os])) {
list($uri, $osTitle, $osIcon, $buttonLabel, $analyticsLabel) = $osChoices[$os];
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
if ($os === OS::OS_ANDROID) {
$asset = ['browser_download_url' => static::ANDROID_STORE_URL, 'size' => 0];
$release = [];
} elseif ($os === OS::OS_IOS) {
$asset = ['browser_download_url' => static::IOS_STORE_URL, 'size' => 0];
$release = [];
} else {
$release = Github::getRepoRelease(Github::REPO_LBRY_DESKTOP, false);
$asset = Github::getRepoAsset(Github::REPO_LBRY_DESKTOP, $os);
2021-05-14 20:10:54 +08:00
}
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
$vars += [
2018-09-17 14:28:25 -04:00
'os' => $os,
'osTitle' => $osTitle,
'osIcon' => $osIcon,
'releaseTimestamp' => $release ? strtotime($release['created_at']) : null,
'size' => $asset ? $asset['size'] / (1024 * 1024) : 0, //bytes -> MB
'sourceLink' => false,
'version' => $release ? $release['name'] : null,
'isAuto' => Request::getParam('auto'),
];
2021-05-14 20:10:54 +08:00
}
2018-09-17 14:28:25 -04:00
2021-05-14 20:10:54 +08:00
return $vars;
}
2016-04-25 07:54:42 -04:00
}