mirror of
https://github.com/koel/koel
synced 2024-11-10 14:44:13 +00:00
Refactor album cover cache function
This commit is contained in:
parent
d40a5ff751
commit
4e273639bd
1 changed files with 19 additions and 23 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Cache;
|
||||
use Exception;
|
||||
use getID3;
|
||||
use getid3_lib;
|
||||
|
@ -363,31 +364,26 @@ class File
|
|||
private function getCoverFileUnderSameDirectory()
|
||||
{
|
||||
// As directory scanning can be expensive, we cache and reuse the result.
|
||||
$cacheKey = md5($this->path.'_cover');
|
||||
return Cache::remember(md5($this->path.'_cover'), 24 * 60, function () {
|
||||
$matches = array_keys(iterator_to_array(
|
||||
Finder::create()
|
||||
->depth(0)
|
||||
->ignoreUnreadableDirs()
|
||||
->files()
|
||||
->followLinks()
|
||||
->name('/(cov|fold)er\.(jpe?g|png)$/i')
|
||||
->in(dirname($this->path))
|
||||
)
|
||||
);
|
||||
|
||||
$cover = $matches ? $matches[0] : false;
|
||||
// Even if a file is found, make sure it's a real image.
|
||||
if ($cover && exif_imagetype($cover) === false) {
|
||||
$cover = false;
|
||||
}
|
||||
|
||||
if (!is_null($cover = cache($cacheKey))) {
|
||||
return $cover;
|
||||
}
|
||||
|
||||
$matches = array_keys(iterator_to_array(
|
||||
Finder::create()
|
||||
->depth(0)
|
||||
->ignoreUnreadableDirs()
|
||||
->files()
|
||||
->followLinks()
|
||||
->name('/(cov|fold)er\.(jpe?g|png)$/i')
|
||||
->in(dirname($this->path))
|
||||
));
|
||||
|
||||
$cover = $matches ? $matches[0] : false;
|
||||
// Even if a file is found, make sure it's a real image.
|
||||
if ($cover && exif_imagetype($cover) === false) {
|
||||
$cover = false;
|
||||
}
|
||||
|
||||
cache([$cacheKey => $cover], 24 * 60);
|
||||
|
||||
return $cover;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue