Skip to content

Commit b0b4dfd

Browse files
author
Bradie Tilley
committed
Add new dashboard widget and fix '' context error
1 parent 266e411 commit b0b4dfd

File tree

6 files changed

+169
-4
lines changed

6 files changed

+169
-4
lines changed

Plugin.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ABWebDevelopers\ImageResize\Classes\Resizer;
77
use ABWebDevelopers\ImageResize\Commands\ImageResizeClear;
88
use ABWebDevelopers\ImageResize\Commands\ImageResizeGc;
9+
use ABWebDevelopers\ImageResize\ReportWidgets\ImageResizeClearWidget;
910
use Event;
1011

1112
class Plugin extends PluginBase
@@ -104,6 +105,17 @@ public function register()
104105
*/
105106
public function registerSchedule($schedule)
106107
{
107-
$schedule->command('imageresize:gc')->daily();
108+
// This is throttled by your settings, it won't necessarily clear all images every 5 minutes
109+
$schedule->command('imageresize:gc')->everyFiveMinutes();
110+
}
111+
112+
public function registerReportWidgets()
113+
{
114+
return [
115+
ImageResizeClearWidget::class => [
116+
'label' => 'Clear Image Resizer Cache',
117+
'context' => 'dashboard',
118+
],
119+
];
108120
}
109121
}

lang/en/lang.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,10 @@
143143
]
144144
],
145145
],
146+
'widgets' => [
147+
'clear' => [
148+
'cleared' => 'Successfully cleared all resized cached images (:size)',
149+
'showGcInterval' => 'Show Garbage Collection Interval',
150+
]
151+
],
146152
];

models/Settings.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ public static function getBasePath(string $subdirectoryPath = null, bool $absolu
266266
*/
267267
public static function getAgeToDelete(): string
268268
{
269-
if (!empty($this->cache_clear_interval)) {
270-
return $this->cache_clear_interval;
269+
$that = static::instance();
270+
271+
if (!empty($that->cache_clear_interval)) {
272+
return $that->cache_clear_interval;
271273
}
272274

273275
return static::DEFAULT_CACHE_CLEAR_AGE;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace ABWebDevelopers\ImageResize\ReportWidgets;
4+
5+
use ABWebDevelopers\ImageResize\Models\Settings;
6+
use Backend\Classes\ReportWidgetBase;
7+
use Illuminate\Support\Facades\Artisan;
8+
use Illuminate\Support\Facades\File;
9+
use Illuminate\Support\Facades\Lang;
10+
use October\Rain\Support\Facades\Flash;
11+
12+
class ImageResizeClearWidget extends ReportWidgetBase
13+
{
14+
public function defineProperties()
15+
{
16+
return [
17+
'showGcInterval' => [
18+
'title' => 'abwebdevelopers.imageresize::lang.widgets.clear.showGcInterval',
19+
'default' => true,
20+
'type' => 'checkbox',
21+
]
22+
];
23+
}
24+
25+
/**
26+
* Default handler: Render the widget
27+
*
28+
* @return void
29+
*/
30+
public function render()
31+
{
32+
return $this->makePartial('default');
33+
}
34+
35+
/**
36+
* AJAX callback: Clear the cache
37+
*
38+
* @return void
39+
*/
40+
public function onClear()
41+
{
42+
$from = $this->directorySize();
43+
Artisan::call('imageresize:clear');
44+
$to = $this->directorySize();
45+
46+
$size = $this->formatSize($from - $to);
47+
Flash::success(Lang::get('abwebdevelopers.imageresize::lang.widgets.clear.cleared', [
48+
'size' => $size
49+
]));
50+
51+
return [
52+
'partial' => $this->render(),
53+
];
54+
}
55+
56+
/**
57+
* Get the total size (in bytes) of all resized images
58+
*
59+
* @return int
60+
*/
61+
protected function directorySize(): int
62+
{
63+
$size = 0;
64+
$path = Settings::getBasePath();
65+
66+
foreach (File::allFiles($path) as $file) {
67+
$size += $file->getSize();
68+
}
69+
70+
return $size;
71+
}
72+
73+
/**
74+
* Format the bytes to human readable form
75+
*
76+
* @param int $size
77+
* @param int $precision
78+
* @return string
79+
*/
80+
protected function formatSize(int $size, int $precision = 2): string
81+
{
82+
if (empty($size)) {
83+
return '0B';
84+
}
85+
86+
$base = log($size, 1024);
87+
$suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];
88+
89+
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
90+
}
91+
92+
/**
93+
* Get the title of the widget
94+
*
95+
* @return string
96+
*/
97+
public function title(): string
98+
{
99+
return 'Image Resize Cache';
100+
}
101+
102+
/**
103+
* Get the size of all cached images (formatted)
104+
*
105+
* @return string
106+
*/
107+
public function size(): string
108+
{
109+
return $this->formatSize($this->directorySize());
110+
}
111+
112+
/**
113+
* Get the configured cache clear interval
114+
*
115+
* @return string|null
116+
*/
117+
public function garbageCollectionInterval(): ?string
118+
{
119+
return Settings::getAgeToDelete();
120+
}
121+
122+
/**
123+
* Should the garbage collection interval be displayed in this widget?
124+
*
125+
* @return boolean
126+
*/
127+
public function showGarbageCollectionInterval(): bool
128+
{
129+
return (bool) $this->property('showGcInterval', false);
130+
}
131+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div id="imageresizeclearwidget" class="report-widget">
2+
<h3><?= e(trans($this->title())) ?></h3>
3+
<?php if ($this->garbageCollectionInterval() && $this->showGarbageCollectionInterval()): ?>
4+
<p style="color: #999;">Garbage collection interval: <?= e($this->garbageCollectionInterval()) ?></p>
5+
<?php endif; ?>
6+
<button type="button" class="btn btn-success"
7+
title="Clearing the cache will NOT remove the original images, it will only remove the cached resized copies"
8+
data-request="<?= $this->getEventHandler('onClear') ?>"
9+
data-request-success="$('#imageresizeclearwidget').replaceWith(data.partial)">
10+
Clear all resized images (<?= $this->size() ?>) | <span class="fa icon-trash"></span>
11+
</button>
12+
</div>

updates/version.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@
3030
- Clear old images (for the v2.1.0 directory change)
3131
- clear_old_cache_dir.php
3232
2.1.3:
33-
- Fix - Generate absolute URLs instead of domain-relative URLs
33+
- Fix - Generate absolute URLs instead of domain-relative URLs
34+
2.1.4:
35+
- Add new Dashboard Widget as an alternative method of clearing the image resize cache

0 commit comments

Comments
 (0)