Verified Commit dd461583 authored by noplanman's avatar noplanman

Fallback to cached file if bintray API can't be accessed.

parent 195bde58
......@@ -13,18 +13,20 @@ function get_bintray_files_cache($force = false)
getenv('BINTRAY_API_KEY')
));
file_put_contents($cache_file, $files_json);
} else {
$files_json = file_get_contents($cache_file);
// Fallback to cached file if the new files couldn't be fetched.
if ($files_json !== false) {
file_put_contents($cache_file, $files_json);
return json_decode($files_json, false);
}
}
return json_decode($files_json, false);
return json_decode(file_get_contents($cache_file), false);
}
function get_binary_files()
{
$files = [];
foreach (get_bintray_files_cache() as $file) {
foreach (get_bintray_files_cache() ?? [] as $file) {
$files[$file->name] = $file;
}
krsort($files, SORT_NATURAL);
......@@ -34,6 +36,10 @@ function get_binary_files()
function save_binaries_json($files, $version)
{
if (empty($files) || empty($version)) {
return;
}
$binaries = [];
foreach (['armv5', 'armv6', 'arm64'] as $flavor) {
$binary = "borg-{$version}-{$flavor}";
......@@ -49,6 +55,10 @@ function save_binaries_json($files, $version)
function human_filesize($bytes, $decimals = 2)
{
if ($bytes === null) {
return '';
}
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
$suff = @$sz[$factor];
......@@ -70,8 +80,8 @@ function get_dl_link($flavor, $version, $file = null)
// Fetch the clean list of binary files.
$files = get_binary_files();
// Get the latest version number.
$stable = reset($files)->version;
// Get the latest version number, fallback to 1.1.10.
$stable = reset($files)->version ?? '1.1.10';
// Save binary details of latest stable version to "binaries.json".
save_binaries_json($files, $stable);
......@@ -109,7 +119,7 @@ save_binaries_json($files, $stable);
<div class="flavor">
<a class="download" href="<?= get_dl_link('armv5', $stable) ?>">
<h2>ARMv5 / ARMv4t</h2><br>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-armv5"]->size) ?></b>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-armv5"]->size ?? null) ?></b>
</a>
<span class="description">
This version is built for 32Bit-ARM CPUs without a Floating-Point-Unit (ARM v4t and v5).<br>
......@@ -125,7 +135,7 @@ save_binaries_json($files, $stable);
<div class="flavor">
<a class="download" href="<?= get_dl_link('armv6', $stable) ?>">
<h2>ARMv7 / ARMv6</h2><br>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-armv6"]->size) ?></b>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-armv6"]->size ?? null) ?></b>
</a>
<span class="description">
This version is built for 32Bit-ARM CPUs with a Floating-Point-Unit (ARM v6 and v7).<br>
......@@ -141,7 +151,7 @@ save_binaries_json($files, $stable);
<div class="flavor">
<a class="download" href="<?= get_dl_link('arm64', $stable) ?>">
<h2>ARMv8 (or ARM64)</h2><br>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-arm64"]->size) ?></b>
<b class="download-link">Borg <?= $stable ?><br><br><?= human_filesize($files["borg-{$stable}-arm64"]->size ?? null) ?></b>
</a>
<span class="description">
This version is built for modern 64Bit-ARM CPUs (ARM v8).<br>
......@@ -177,6 +187,7 @@ save_binaries_json($files, $stable);
Yes, but please use <a href="/binaries.json">this JSON representation</a> in stead of this website. Also, please note that excessive polling might get you banned.
</div>
<?php if ($files): ?>
<h2>Archive</h2>
<div id="view" class="view-details view-size-20">
<ul id="items" class="clearfix">
......@@ -196,6 +207,7 @@ save_binaries_json($files, $stable);
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment