Skip to content

Commit 42a5f95

Browse files
author
Bradie Tilley
committed
Add migration to remove the old cached directory if no longer used
1 parent 0e1db2e commit 42a5f95

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

classes/Resizer.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,11 +886,14 @@ public function render(): void
886886
* Delete all cached images.
887887
*
888888
* @param Carbon|null $minAge Optional minimum age (delete before this date), `null` for all files.
889+
* @param string|null $directory Optional directory to delete from
889890
* @return int Number of files deleted
890891
*/
891-
public static function clearFiles(Carbon $minAge = null): int
892+
public static function clearFiles(Carbon $minAge = null, string $directory = null): int
892893
{
893-
$files = collect(File::allFiles(Settings::getBasePath()))
894+
$directory = $directory ?? Settings::getBasePath();
895+
896+
$files = collect(File::allFiles($directory))
894897
->transform(function ($file) use ($minAge) {
895898
$delete = true;
896899

@@ -905,6 +908,17 @@ public static function clearFiles(Carbon $minAge = null): int
905908
->filter()
906909
->toArray();
907910

911+
// Iterate each directory and delete it if it's empty
912+
collect(File::directories($directory))
913+
->each(function ($dir) {
914+
$files = File::allFiles($dir);
915+
916+
if (empty($files)) {
917+
File::deleteDirectory($dir);
918+
@unlink($dir);
919+
}
920+
});
921+
908922
// Fire event to hook into and modify $files before deleting
909923
Event::fire('abweb.imageresize.clearFiles', [&$files, $minAge]);
910924

updates/clear_old_cache_dir.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ABWebDevelopers\Portfolio\Updates;
4+
5+
use ABWebDevelopers\ImageResize\Classes\Resizer;
6+
use Schema;
7+
use October\Rain\Database\Updates\Migration;
8+
use ABWebDevelopers\ImageResize\Models\Settings;
9+
use Illuminate\Support\Facades\File;
10+
use Illuminate\Support\Facades\Storage;
11+
12+
class ClearOldCacheDir extends Migration
13+
{
14+
public function up()
15+
{
16+
$oldpath = base_path('storage/app/media/imageresizecache');
17+
18+
if (Settings::instance()->cache_directory !== $oldpath) {
19+
if (is_dir($oldpath)) {
20+
Resizer::clearFiles(null, $oldpath);
21+
22+
File::deleteDirectory($oldpath);
23+
@unlink($oldpath);
24+
}
25+
}
26+
}
27+
28+
public function down()
29+
{
30+
}
31+
}

updates/version.yaml

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
1.0.0: Initial version of Image Resize
2-
1.1.0: Add settings, filters, image not found placeholder, and various other improvements
3-
1.1.1: Enable image caching
4-
1.1.2: Minor tweaks and improvements, ship plugin with 2 filters
5-
1.1.3: Fix PHP 7.0 incompatibility issue, support all relative URL images
6-
1.1.4: Register access permissions
7-
1.1.5: Fix issue with spaces in filenames
8-
1.1.6: Fix return type errors
9-
1.1.7: Remove debug routes file
10-
2.0.0: Optimise image resizing on initial pageload by offloading to separate thread (by resizing when requesting images)
11-
2.0.1: Fix bug (since 2.0.0) where the 'default 404 image' does not utilise the configured background/mode/quality
12-
2.0.2: Implement ability to add watermarks to images
13-
2.1.0: Move default imageresizecache directory (out of app/media) and store+serve images with file extension (change default imagresize cache clear to 1 week)
14-
2.1.1: Fix double slash issue in resized image URLs
1+
1.0.0:
2+
- Initial version of Image Resize
3+
1.1.0:
4+
- Add settings, filters, image not found placeholder, and various other improvements
5+
1.1.1:
6+
- Enable image caching
7+
1.1.2:
8+
- Minor tweaks and improvements, ship plugin with 2 filters
9+
1.1.3:
10+
- Fix PHP 7.0 incompatibility issue, support all relative URL images
11+
1.1.4:
12+
- Register access permissions
13+
1.1.5:
14+
- Fix issue with spaces in filenames
15+
1.1.6:
16+
- Fix return type errors
17+
1.1.7:
18+
- Remove debug routes file
19+
2.0.0:
20+
- Optimise image resizing on initial pageload by offloading to separate thread (by resizing when requesting images)
21+
2.0.1:
22+
- Fix bug (since 2.0.0) where the 'default 404 image' does not utilise the configured background/mode/quality
23+
2.0.2:
24+
- Implement ability to add watermarks to images
25+
2.1.0:
26+
- Move default imageresizecache directory (out of app/media) and store+serve images with file extension (change default imagresize cache clear to 1 week)
27+
2.1.1:
28+
- Fix double slash issue in resized image URLs
29+
2.1.2:
30+
- Clear old images (for the v2.1.0 directory change)
31+
- clear_old_cache_dir.php

0 commit comments

Comments
 (0)