Add global defaults to sites.json (icon and nofollow)

This commit is contained in:
Dale Davies
2022-02-21 10:58:20 +00:00
parent c81ce98a28
commit 6528cd3ae2
8 changed files with 70 additions and 37 deletions

View File

@@ -17,19 +17,34 @@ class Site {
public string $icon;
public string $url;
public function __construct(Config $config, array $sitearray) {
public function __construct(Config $config, array $sitearray, array $default) {
$this->config = $config;
$this->defaults = $default;
if (!isset($sitearray['name'], $sitearray['url'])) {
throw new \Exception('The array passed to Site() must contain the keys "name" and "url"!');
}
$this->name = $sitearray['name'];
$this->url = $sitearray['url'];
$this->nofollow = isset($sitearray['nofollow']) ? $sitearray['nofollow'] : false;
$this->nofollow = isset($sitearray['nofollow']) ? $sitearray['nofollow'] : (isset($this->defaults['nofollow']) ? $this->defaults['nofollow'] : false);
$this->icon = isset($sitearray['icon']) ? $this->get_favicon_datauri($sitearray['icon']) : $this->get_favicon_datauri();
}
/**
* Return a data uri for a given icon, or a site's favicon if an icon
* is not provided.
*
* @param string|null $icon File name of a given icon to retrieve.
* @return string Base 64 encoded datauri for the icon image.
*/
public function get_favicon_datauri(?string $icon = null): string {
// Use the applications own default icon unless one is supplied via the sites.json file.
$defaulticon = $this->config->get('defaulticonpath');
if (isset($this->defaults['icon'])) {
$defaulticon = $this->config->get('sitesdir').'/icons/'.$this->defaults['icon'];
}
// Did we have a supplied icon or are we going to try retrieving the favicon?
if ($icon === null) {
// Go get the favicon, if there isnt one then use the default icon.
$favicon = new \Favicon\Favicon();
$favicon->cache([
'dir' => $this->config->get('cachedir').'/icons/',
@@ -37,7 +52,7 @@ class Site {
]);
$rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE);
if (!$rawimage) {
$rawimage = file_get_contents($this->config->get('defaulticonpath'));
$rawimage = file_get_contents($defaulticon);
}
} else {
$rawimage = file_get_contents($this->config->get('sitesdir').'/icons/'.$icon);
@@ -46,4 +61,4 @@ class Site {
return 'data:'.$mimetype.';base64,'.base64_encode($rawimage);
}
}
}

View File

@@ -15,6 +15,7 @@ class Sites {
private Cache $cache;
private Config $config;
private array $default;
private string $sitesfilelocation;
private array $loadedsites;
@@ -26,6 +27,10 @@ class Sites {
$this->loadedsites = [];
$this->sitesfilelocation = $this->config->get('sitesfile');
$this->cache = $cache;
$this->default = [
'icon' => null,
'nofollow' => false
];
$this->load_sites_from_json();
}
@@ -48,13 +53,23 @@ class Sites {
if ($rawjson === '') {
throw new Exception('The sites.json file is empty');
}
$decodedjson = json_decode($rawjson, true);
if (!is_array($decodedjson)) {
throw new Exception('Provided sites json does not contain a top-level array');
// Do some checks to see if the JSON decodes into something
// like what we expect to see...
$decodedjson = json_decode($rawjson);
if (is_array($decodedjson)) {
$sites = $decodedjson;
}
foreach ($decodedjson as $array) {
$sites[] = new Site($this->config, $array);
if (isset($decodedjson->sites) && is_array($decodedjson->sites)) {
$sites = $decodedjson->sites;
$this->default = (array) $decodedjson->default;
}
// Walk over the sites array and instantiate an actual Site() object
// for each element.
array_walk($sites, function(&$item, $key, $default) {
$item = new Site($this->config, (array) $item, $default);
}, $this->default);
// Return the array of Site() objects, note we are in a callback
// so the return is not from the outer function.
return $sites;
});
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 418 B

After

Width:  |  Height:  |  Size: 389 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 706 B

After

Width:  |  Height:  |  Size: 686 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@@ -1,30 +1,33 @@
[
{
"name": "Bitwarden",
"url" : "https://bitwarden.example.com",
{
"default": {
"nofollow": true,
"icon": "bitwarden.png"
"icon": "my-default-icon.png"
},
{
"name": "Gitea",
"url" : "https://git.example.com",
"nofollow": true,
"icon": "gitea.png"
},
{
"name": "Nextcloud",
"url" : "https://cloud.example.com",
"nofollow": true,
"icon": "nextcloud.png"
},
{
"name": "Paperless",
"url" : "https://paperless.example.com",
"nofollow": true,
"icon": "paperless.jpg"
},
{
"name": "Google",
"url" : "https://www.google.com"
}
]
"sites": [
{
"name": "Bitwarden",
"url" : "https://bitwarden.example.com",
"icon": "bitwarden.png"
},
{
"name": "Gitea",
"url" : "https://git.example.com",
"icon": "gitea.png"
},
{
"name": "Nextcloud",
"url" : "https://cloud.example.com",
"icon": "nextcloud.png"
},
{
"name": "Paperless",
"url" : "https://paperless.example.com",
"icon": "paperless.jpg"
},
{
"name": "Google",
"url" : "https://www.google.com",
"nofollow": false
}
]
}