Cache poster images
This commit is contained in:
parent
eff673a7bb
commit
86663d2179
@ -35,5 +35,52 @@ class BaseModel {
|
|||||||
{
|
{
|
||||||
return "{$this->base_url}{$path}";
|
return "{$this->base_url}{$path}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path of the cached version of the image. Create the cached image
|
||||||
|
* if the file does not already exist
|
||||||
|
*
|
||||||
|
* @param string $api_path - The original image url
|
||||||
|
* @param string $series_slug - The part of the url with the series name, becomes the image name
|
||||||
|
* @param string $type - Anime or Manga, controls cache path
|
||||||
|
* @return string - the frontend path for the cached image
|
||||||
|
*/
|
||||||
|
public function get_cached_image($api_path, $series_slug, $type="anime")
|
||||||
|
{
|
||||||
|
$path_parts = explode('?', basename($api_path));
|
||||||
|
$path = current($path_parts);
|
||||||
|
$ext_parts = explode('.', $path);
|
||||||
|
$ext = end($ext_parts);
|
||||||
|
|
||||||
|
$cached_image = "{$series_slug}.{$ext}";
|
||||||
|
$cached_path = __DIR__ . "/../../public/cache/{$type}/{$cached_image}";
|
||||||
|
|
||||||
|
// Cache the file if it doesn't already exist
|
||||||
|
if ( ! file_exists($cached_path))
|
||||||
|
{
|
||||||
|
if (ini_get('allow_url_fopen'))
|
||||||
|
{
|
||||||
|
copy($api_path, $cached_path);
|
||||||
|
}
|
||||||
|
elseif (function_exists('curl_init'))
|
||||||
|
{
|
||||||
|
$ch = curl_init($api_path);
|
||||||
|
$fp = fopen($cached_path, 'wb');
|
||||||
|
curl_setopt_array($ch, [
|
||||||
|
CURLOPT_FILE => $fp,
|
||||||
|
CURLOPT_HEADER => 0
|
||||||
|
]);
|
||||||
|
curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
fclose($ch);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Couldn't cache images because they couldn't be downloaded.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/public/cache/{$type}/{$cached_image}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// End of BaseModel.php
|
// End of BaseModel.php
|
@ -116,7 +116,14 @@ class AnimeModel extends BaseModel {
|
|||||||
throw new Exception($response->getEffectiveUrl());
|
throw new Exception($response->getEffectiveUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response->json();
|
$output = $response->json();
|
||||||
|
|
||||||
|
foreach($output as &$row)
|
||||||
|
{
|
||||||
|
$row['anime']['cover_image'] = $this->get_cached_image($row['anime']['cover_image'], $row['anime']['slug'], 'anime');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sort_by_name(&$array)
|
private function sort_by_name(&$array)
|
||||||
|
@ -83,10 +83,13 @@ class MangaModel extends BaseModel {
|
|||||||
$manga_data[$manga['id']] = $manga;
|
$manga_data[$manga['id']] = $manga;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($raw_data['manga_library_entries'] as $entry)
|
foreach($raw_data['manga_library_entries'] as &$entry)
|
||||||
{
|
{
|
||||||
$entry['manga'] = $manga_data[$entry['manga_id']];
|
$entry['manga'] = $manga_data[$entry['manga_id']];
|
||||||
|
|
||||||
|
// Cache poster images
|
||||||
|
$entry['manga']['poster_image'] = $this->get_cached_image($entry['manga']['poster_image'], $entry['manga_id'], 'manga');
|
||||||
|
|
||||||
switch($entry['status'])
|
switch($entry['status'])
|
||||||
{
|
{
|
||||||
case "Plan to Read":
|
case "Plan to Read":
|
||||||
|
Loading…
Reference in New Issue
Block a user