api/playlists and image resizing + delivery
[ouya-imagestore.git] / src / imagestore / Controller / Api / Playlists.php
1 <?php
2 namespace imagestore;
3
4 class Controller_Api_Playlists
5 {
6     public function handle()
7     {
8         $playlists = (object) array(
9             'games' => array(),
10             'playlists' => array()
11         );
12         //-1 - link to playlists
13         //-2 - ????
14         //1 - featured
15         //2 - trending now
16         $id = 0;
17         foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
18             $plnum = count($playlists->playlists);
19             $playlists->playlists[$plnum] = (object) array(
20                 'id'    => ++$id,
21                 'image' => '',
22                 'name'  => $dir,
23                 'tiles' => array()
24             );
25             foreach ($arDirs as $dirInfo) {
26                 //FIXME: uuid dots?
27                 $uuid = $this->getRelPath($dirInfo->getPathname());
28                 $playlists->games[] = (object) array(
29                     'content_rating' => 'Everyone', 
30                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo)),
31                     'title'   => basename($dirInfo->getPathname()), 
32                     'uuid'    =>  $uuid,
33                     'version' => '11111111-0000-1111-0000-111111111111'
34                 );
35                 $playlists->playlists[$plnum]->tiles[] = (object) array(
36                     'game' => $uuid
37                 );
38             }
39         }
40
41         header('Content-Type: application/json');
42         echo json_encode($playlists, JSON_PRETTY_PRINT);
43     }
44
45     protected function getDirs()
46     {
47         $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
48         $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
49
50         $all = new \AppendIterator();
51         $all->append($dirs);
52         $all->append($dirs2);
53
54         $allDirs = new \CallbackFilterIterator(
55             $all,
56             function ($fileInfo) {
57                 return $fileInfo->isDir() && $this->hasImages($fileInfo);
58             }
59         );
60         return new \CachingIterator($allDirs);
61     }
62
63     protected function hasImages(\SplFileInfo $dirInfo)
64     {
65         $it = $this->getImageIterator($dirInfo);
66         $it->rewind();
67         return $it->valid();
68     }
69
70     protected function getImageIterator(\SplFileInfo $dirInfo)
71     {
72         $it = new \AppendIterator();
73         $it->append(
74             new \LimitIterator(
75                 new \GlobIterator($dirInfo->getPathName() . '/*.jpg'),
76                 0, 1
77             )
78         );
79         $it->append(
80             new \LimitIterator(
81                 new \GlobIterator($dirInfo->getPathName() . '/*.JPG'),
82                 0, 1
83             )
84         );
85         return $it;
86     }
87
88     protected function groupByParent($dirs)
89     {
90         $arGroups = array();
91         foreach ($dirs as $dirInfo) {
92             $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
93         }
94         return $arGroups;
95     }
96
97     protected function getRelPath($path)
98     {
99         return substr($path, strlen($GLOBALS['imagestore']['basedir']));
100     }
101
102     protected function getImageUrl($path)
103     {
104         if (isset($_SERVER['HTTPS'])) {
105         } else {
106             $protocol = 'http';
107         }
108         return $protocol . '://' . $_SERVER['HTTP_HOST']
109             . '/image?path=' . urlencode($this->getRelPath($path));
110     }
111
112     protected function getFirstImage(\SplFileInfo $dirInfo)
113     {
114         $it = $this->getImageIterator($dirInfo);
115         $it->rewind();
116         return $it->current();
117     }
118 }
119 ?>