Finalizing MVP for image cache plugin. Closes #27.

This commit is contained in:
Jason Williams 2021-01-02 18:31:29 -07:00
parent 0f302ec5ba
commit 710abf1237
4 changed files with 56 additions and 3 deletions

View File

@ -5,6 +5,7 @@
*/ */
use System\Classes\PluginBase; use System\Classes\PluginBase;
use JasonWilliams\ImgCache\Classes\Image;
class Plugin extends PluginBase class Plugin extends PluginBase
{ {
@ -24,7 +25,8 @@ class Plugin extends PluginBase
return [ return [
'filters' => [ 'filters' => [
'imgcache' => function($imgurl) { 'imgcache' => function($imgurl) {
return $imgurl; $image = new Image($imgurl);
return $image;
} }
] ]
]; ];

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -0,0 +1,52 @@
<?php namespace JasonWilliams\ImgCache\Classes;
/*
Find a faster way of returning in the event of a 404
*/
use October\Rain\Network\Http;
use October\Rain\Filesystem\Filesystem;
class Image
{
protected $file;
protected $folder;
protected $urlbase;
protected $imgurl;
protected $filename;
protected $found = false;
public function __construct($url)
{
$this->imgurl = $url;
$this->folder = base_path(substr(config('cms.storage.media.path'), 1)).'/external/';
$this->urlbase = config('cms.storage.media.path').'/external/';
$this->filename = md5($url).'.jpg';
$fs = new Filesystem;
if($fs->existsInsensitive($this->folder.$this->filename))
{
$this->found = true;
$this->imgurl = $this->urlbase.$this->filename;
return;
}
$req = new Http();
$img = $req->get($url, [
'CURLOPT_CONNECTTIMEOUT' => 1
]);
if ($img->code !== 200) $this->imgurl = '/plugins/jasonwilliams/imgcache/assets/images/notfound.png';
else {
$this->found = true;
$fs->put($this->folder.$this->filename, $img);
$this->imgurl = $this->urlbase.$this->filename;
}
}
public function __toString()
{
return $this->imgurl;
}
}

View File

@ -13,7 +13,6 @@
<div class="{{ feeditemclass }}" data-timestamp="{{ post.timestamp }}"> <div class="{{ feeditemclass }}" data-timestamp="{{ post.timestamp }}">
<div class="card"> <div class="card">
{% if post.extra.img %} {% if post.extra.img %}
<!-- <img class="card-img-top" src="{{ post.extra.img }}"> -->
<img class="card-img-top" src="{{ post.extra.img | imgcache }}"> <img class="card-img-top" src="{{ post.extra.img | imgcache }}">
{% elseif post.extra.lat %} {% elseif post.extra.lat %}
<img class="card-img-top" src="https://api.mapbox.com/styles/v1/jason32dev/ckgehopjc2o1y19o09aqs4x11/static/pin-l+B05C71({{ post.extra.lng }},{{ post.extra.lat }})/{{ post.extra.lng }},{{ post.extra.lat }},14,0/545x300@2x?access_token=pk.eyJ1IjoiamFzb24zMmRldiIsImEiOiJjazFsNHd5djcwMXptM2htbW8zM3MyZGxuIn0.-TtNNGFysQPQRfGR1P8DUA"> <img class="card-img-top" src="https://api.mapbox.com/styles/v1/jason32dev/ckgehopjc2o1y19o09aqs4x11/static/pin-l+B05C71({{ post.extra.lng }},{{ post.extra.lat }})/{{ post.extra.lng }},{{ post.extra.lat }},14,0/545x300@2x?access_token=pk.eyJ1IjoiamFzb24zMmRldiIsImEiOiJjazFsNHd5djcwMXptM2htbW8zM3MyZGxuIn0.-TtNNGFysQPQRfGR1P8DUA">
@ -35,7 +34,7 @@
<div class="feedcard-footer"> <div class="feedcard-footer">
<p class="float-right feedcard-itemtype"><span class="friendlytime">{{ post.friendlytime }}</span> <i class="{{ post.channel.icon }}" style="color: {{ post.channel.colour }};"></i></p> <p class="float-right feedcard-itemtype"><span class="friendlytime">{{ post.friendlytime }}</span> <i class="{{ post.channel.icon }}" style="color: {{ post.channel.colour }};"></i></p>
{% if post.channel.slug == 'twitter' and post.extra.opuser %} {% if post.channel.slug == 'twitter' and post.extra.opuser %}
<p class="float-left feedcard-attribution"><i class="fas fa-retweet"></i> <a href="https://twitter.com/{{ post.extra.opuser }}">{{ post.extra.opname }}</a> <img src="{{ post.extra.opimg }}"></p> <p class="float-left feedcard-attribution"><i class="fas fa-retweet"></i> <a href="https://twitter.com/{{ post.extra.opuser }}">{{ post.extra.opname }}</a> <img src="{{ post.extra.opimg | imgcache }}"></p>
{% endif %} {% endif %}
</div> </div>
</div> </div>