limit image size in playlist to 400px width
[ouya-imagestore.git] / src / imagestore / Controller / Api / Playlists.php
1 <?php
2 namespace imagestore;
3
4 class Controller_Api_Playlists extends Controller_Api_ImageBase
5 {
6     public function handle()
7     {
8         $playlists = (object) array(
9             'games' => array(),
10             'playlists' => array(
11                 (object) array(
12                     //main playlists in the store?
13                     'name'  => null,
14                     'id'    => -1,
15                     'image' => null,
16                     'tiles' => array()
17                 ),
18                 (object) array(
19                     //mainpage
20                     'name'  => null,
21                     'id'    => -2,
22                     'image' => null,
23                     'tiles' => array()
24                 )
25             )
26         );
27
28         $mainpage = 0;
29         $id = 0;
30         foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
31             $plnum = count($playlists->playlists);
32             $playlists->playlists[$plnum] = (object) array(
33                 'id'    => ++$id,
34                 'image' => '',
35                 'name'  => $dir,
36                 'tiles' => array()
37             );
38             $playlists->playlists[0]->tiles[] = array('playlist' => $id);
39
40             foreach ($arDirs as $dirInfo) {
41                 $uuid = str_replace(
42                     '/', '-.-', $this->getRelPath($dirInfo->getPathname())
43                 );
44                 $playlists->games[] = (object) array(
45                     'content_rating' => 'Everyone', 
46                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo))
47                         . '&w=400',
48                     'title'   => basename($dirInfo->getPathname()), 
49                     'uuid'    =>  $uuid,
50                     'version' => '11111111-0000-1111-0000-111111111111',
51                     '__details' => $this->getDetailsUrl($uuid),
52                 );
53                 $playlists->playlists[$plnum]->tiles[] = (object) array(
54                     'game' => $uuid
55                 );
56
57                 if (++$mainpage < 10) {
58                     $playlists->playlists[1]->tiles[] = (object) array(
59                         'game' => $uuid
60                     );
61                 }
62             }
63         }
64
65         header('Content-Type: application/json');
66         echo json_encode($playlists, JSON_PRETTY_PRINT);
67     }
68
69     protected function getDirs()
70     {
71         $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
72         $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
73
74         $all = new \AppendIterator();
75         $all->append($dirs);
76         $all->append($dirs2);
77
78         $allDirs = new \CallbackFilterIterator(
79             $all,
80             function ($fileInfo) {
81                 return $fileInfo->isDir() && $this->hasImages($fileInfo);
82             }
83         );
84         return new \CachingIterator($allDirs);
85     }
86
87     protected function hasImages(\SplFileInfo $dirInfo)
88     {
89         $it = $this->getImageIterator($dirInfo);
90         $it->rewind();
91         return $it->valid();
92     }
93
94     protected function groupByParent($dirs)
95     {
96         $arGroups = array();
97         foreach ($dirs as $dirInfo) {
98             $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
99         }
100         return $arGroups;
101     }
102
103     protected function getDetailsUrl($uuid)
104     {
105         return $this->getBaseUrl()
106             . 'api/v1/apps/' . rawurlencode($uuid);
107     }
108 }
109 ?>