Various unsplash tweaks/fixes

This commit is contained in:
Dale Davies
2022-06-05 21:57:51 +01:00
parent 19e98c0789
commit 68f179488f
13 changed files with 33 additions and 13 deletions

View File

@@ -46,9 +46,10 @@ if (!$token || !hash_equals($csrfsection->get('token'), $token)) {
}
$unsplashdata = load_cache_unsplash_data();
shell_exec('/usr/bin/nohup /usr/bin/php -f unsplashdata.php >/dev/null 2>&1 &');
echo json_encode($unsplashdata);
shell_exec('/usr/bin/nohup /usr/bin/php -f unsplashdata.php >/dev/null 2>&1 &');
function load_cache_unsplash_data() {
global $cache, $config;
return $cache->load(cachename: 'unsplash', callback: function() use ($config) {
@@ -79,6 +80,7 @@ function load_cache_unsplash_data() {
$description = $photo->description;
}
$unsplashdata = new stdClass();
$unsplashdata->color = $photo->color;
$unsplashdata->attribution = '<a target="_blank" rel="noopener" href="'.$photo->links['html'].'">'.$description.' by '.$photo->user['name'].'</a>';
$unsplashdata->imagedatauri = 'data: '.(new finfo(FILEINFO_MIME_TYPE))->buffer($response).';base64,'.base64_encode($response);
return $unsplashdata;

View File

@@ -3,7 +3,7 @@
position: absolute;
right: 15px;
bottom: 10px;
z-index: 100;
z-index: 1000;
font-family: 'Quicksand', sans-serif;
font-weight: 400;
text-shadow: 1px 1px 2px #000000a0;

View File

@@ -78,6 +78,10 @@ body {
a {
color: inherit;
text-decoration: none;
&:hover {
font-weight: bold;
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -51,6 +51,10 @@ export default class Main {
init() {
// Let's display some images from unsplash then shall we...
if (JUMP.unsplash) {
const backgroundelm = document.querySelector('.background');
if (JUMP.unsplashcolor) {
backgroundelm.style.backgroundColor = JUMP.unsplashcolor;
}
fetch('/api/unsplashdata.php?token=' + JUMP.token)
.then(response => response.json())
.then(data => {
@@ -58,7 +62,7 @@ export default class Main {
console.error('JUMP ERROR: There was an issue with the Unsplash API... ' + data.error);
return;
}
document.querySelector('.background').style.backgroundImage = 'url("' + data.imagedatauri + '")';
backgroundelm.style.backgroundImage = 'url("' + data.imagedatauri + '")';
document.querySelector('.unsplash').innerHTML = data.attribution;
});
}

View File

@@ -102,14 +102,18 @@ class Cache {
* @param callable $callback The code from which the result should be stored in cache.
* @return mixed The result of callback function retreieved from cache.
*/
public function load(string $cachename, ?string $key = 'default', callable $callback): mixed {
public function load(string $cachename, ?string $key = 'default', callable $callback = null): mixed {
// If cachebypass has been set in config.php then just execute the callback.
if ($this->config->parse_bool($this->config->get('cachebypass'))) {
if ($this->config->parse_bool($this->config->get('cachebypass')) && $callback !== null) {
return $callback();
}
$this->init_cache($cachename, $key);
// Retrieve the initialised cache object from $caches, defines the caches expiry
// and executes the callback.
// Retrieve the initialised cache object from $caches.
if ($callback === null) {
return $this->caches[$cachename]['cache'][$key]->load($cachename.'/'.$key);
}
// If we have a callback supplied then get the cached object and also define the
// cache's expiry and execute the callback.
return $this->caches[$cachename]['cache'][$key]->load($cachename.'/'.$key,
function (&$dependencies) use ($callback, $cachename) {
$dependencies[$this->caches[$cachename]['expirationtype']] = $this->caches[$cachename]['expirationparams'];

View File

@@ -11,6 +11,7 @@ class HomePage extends AbstractPage {
$greeting = 'home';
}
$csrfsection = $this->session->getSection('csrf');
$unsplashdata = $this->cache->load('unsplash');
$templatecontext = [
'csrftoken' => $csrfsection->get('token'),
'greeting' => $greeting,
@@ -20,6 +21,7 @@ class HomePage extends AbstractPage {
'metrictemp' => $this->config->parse_bool($this->config->get('metrictemp')),
'ampmclock' => $this->config->parse_bool($this->config->get('ampmclock', false)),
'unsplash' => !!$this->config->get('unsplashapikey', false),
'unsplashcolor' => $unsplashdata?->color,
];
if ($this->config->parse_bool($this->config->get('showsearch', false))) {
$templatecontext = array_merge($templatecontext, [

View File

@@ -11,6 +11,7 @@ class TagPage extends AbstractPage {
$greeting = $this->param;
$title = 'Tag: '.$this->param;
$csrfsection = $this->session->getSection('csrf');
$unsplashdata = $this->cache->load('unsplash');
$templatecontext = [
'csrftoken' => $csrfsection->get('token'),
'greeting' => $greeting,
@@ -20,6 +21,7 @@ class TagPage extends AbstractPage {
'metrictemp' => $this->config->parse_bool($this->config->get('metrictemp')),
'ampmclock' => $this->config->parse_bool($this->config->get('ampmclock', false)),
'unsplash' => !!$this->config->get('unsplashapikey', false),
'unsplashcolor' => $unsplashdata?->color,
];
if ($this->config->parse_bool($this->config->get('showsearch', false))) {
$templatecontext = array_merge($templatecontext, [

View File

@@ -33,6 +33,6 @@
{{/ hastags}}
<span class="unsplash"></span>
<div class="background fixed"></div>
<script defer src="/assets/js/index.dc0f2861d62f085d50c9.min.js"></script>
<script defer src="/assets/js/index.30b1fd8630047c05ccca.min.js"></script>
</body>
</html>

View File

@@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{# noindex}}<meta name="robots" content="noindex">{{/ noindex}}
<link rel="stylesheet" href="/assets/css/styles.0c1a47459cd078d3c71d.min.css">
<link rel="stylesheet" href="/assets/css/styles.016ca924b60cb567810a.min.css">
<link rel="stylesheet" href="/background-css.php">
<link rel="stylesheet" href="/assets/css/weather-icons.min.css">
<link rel="icon" type="image/png" href="/favicon.png">
@@ -17,7 +17,8 @@
token: '{{csrftoken}}',
search: '{{{searchjson}}}',
searchengines: '{{{searchengines}}}',
unsplash: '{{{unsplash}}}'
unsplash: '{{{unsplash}}}',
unsplashcolor: '{{unsplashcolor}}',
};
</script>
</head>

View File

@@ -17,7 +17,8 @@
token: '{{csrftoken}}',
search: '{{{searchjson}}}',
searchengines: '{{{searchengines}}}',
unsplash: '{{{unsplash}}}'
unsplash: '{{{unsplash}}}',
unsplashcolor: '{{unsplashcolor}}',
};
</script>
</head>