1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AniDbFillerBundle\Controller; |
10
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
12
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Guzzle\Http\Client; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
class MediaController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
const CACHE_LIFETIME = 15552000; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get cover from anidb.net item id. |
26
|
|
|
* |
27
|
|
|
* @param string $id |
28
|
|
|
* @param Request $request |
29
|
|
|
* |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
|
|
public function coverAction($id, Request $request) |
33
|
|
|
{ |
34
|
|
|
$body = $this->getAnime($id); |
35
|
|
|
/* @var $response Response */ |
36
|
|
|
$response = $this->get('cache_time_keeper')->getResponse([], self::CACHE_LIFETIME) |
37
|
|
|
->setEtag(sha1($body->html())); |
38
|
|
|
$response->headers->set('Content-Type', 'image/jpeg'); |
39
|
|
|
|
40
|
|
|
// response was not modified for this request |
41
|
|
|
if ($response->isNotModified($request)) { |
42
|
|
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($image = $body->filter('picture')->text()) { |
46
|
|
|
$image = $this->get('anime_db.ani_db.browser')->getImageUrl($image); |
47
|
|
|
$image_response = (new Client())->get($image)->send(); |
48
|
|
|
if (!$image_response->isSuccessful()) { |
49
|
|
|
throw new \RuntimeException('Failed download image from anidb.net'); |
50
|
|
|
} |
51
|
|
|
$response->setContent($image_response->getBody(true)); |
52
|
|
|
} else { |
53
|
|
|
throw $this->createNotFoundException('Cover not found'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $response; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $aid |
61
|
|
|
* |
62
|
|
|
* @return Crawler |
63
|
|
|
*/ |
64
|
|
|
private function getAnime($aid) |
65
|
|
|
{ |
66
|
|
|
return $this->get('anime_db.ani_db.browser')->get('anime', ['aid' => $aid]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|