Cache poster images
This commit is contained in:
parent
eff673a7bb
commit
86663d2179
@ -35,5 +35,52 @@ class BaseModel {
|
||||
{
|
||||
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
|
@ -116,7 +116,14 @@ class AnimeModel extends BaseModel {
|
||||
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)
|
||||
|
@ -22,12 +22,12 @@ class MangaModel extends BaseModel {
|
||||
public function get_all_lists()
|
||||
{
|
||||
$data = $this->_get_list();
|
||||
|
||||
|
||||
foreach ($data as $key => &$val)
|
||||
{
|
||||
$this->sort_by_name($val);
|
||||
}
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ class MangaModel extends BaseModel {
|
||||
public function get_list($type)
|
||||
{
|
||||
$data = $this->_get_list($type);
|
||||
|
||||
|
||||
$this->sort_by_name($data);
|
||||
|
||||
return $data;
|
||||
@ -68,7 +68,7 @@ class MangaModel extends BaseModel {
|
||||
|
||||
// Reorganize data to be more usable
|
||||
$raw_data = $response->json();
|
||||
|
||||
|
||||
$data = [
|
||||
'Reading' => [],
|
||||
'Plan to Read' => [],
|
||||
@ -77,16 +77,19 @@ class MangaModel extends BaseModel {
|
||||
'Completed' => [],
|
||||
];
|
||||
$manga_data = [];
|
||||
|
||||
|
||||
foreach($raw_data['manga'] as $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']];
|
||||
|
||||
|
||||
// Cache poster images
|
||||
$entry['manga']['poster_image'] = $this->get_cached_image($entry['manga']['poster_image'], $entry['manga_id'], 'manga');
|
||||
|
||||
switch($entry['status'])
|
||||
{
|
||||
case "Plan to Read":
|
||||
@ -104,14 +107,14 @@ class MangaModel extends BaseModel {
|
||||
case "Currently Reading":
|
||||
$data['Reading'][] = $entry;
|
||||
break;
|
||||
|
||||
|
||||
case "Completed":
|
||||
default:
|
||||
$data['Completed'][] = $entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (array_key_exists($type, $data)) ? $data[$type] : $data;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user