escape spaces in folders
[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                     array('/', ' '), array('-.-', '+'),
43                     $this->getRelPath($dirInfo->getPathname())
44                 );
45                 $playlists->games[] = (object) array(
46                     'content_rating' => 'Everyone', 
47                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo))
48                         . '&w=400',
49                     'title'   => basename($dirInfo->getPathname()), 
50                     'uuid'    =>  $uuid,
51                     'version' => '11111111-0000-1111-0000-111111111111',
52                     '__details' => $this->getDetailsUrl($uuid),
53                 );
54                 $playlists->playlists[$plnum]->tiles[] = (object) array(
55                     'game' => $uuid
56                 );
57
58                 if (++$mainpage < 10) {
59                     $playlists->playlists[1]->tiles[] = (object) array(
60                         'game' => $uuid
61                     );
62                 }
63             }
64         }
65
66         header('Content-Type: application/json');
67         echo json_encode($playlists, JSON_PRETTY_PRINT);
68     }
69
70     protected function getDirs()
71     {
72         $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
73         $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
74
75         $all = new \AppendIterator();
76         $all->append($dirs);
77         $all->append($dirs2);
78
79         $allDirs = new \CallbackFilterIterator(
80             $all,
81             function ($fileInfo) {
82                 return $fileInfo->isDir() && $this->hasImages($fileInfo);
83             }
84         );
85         return new \CachingIterator($allDirs);
86     }
87
88     protected function hasImages(\SplFileInfo $dirInfo)
89     {
90         $it = $this->getImageIterator($dirInfo);
91         $it->rewind();
92         return $it->valid();
93     }
94
95     protected function groupByParent($dirs)
96     {
97         $arGroups = array();
98         foreach ($dirs as $dirInfo) {
99             $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
100         }
101         return $arGroups;
102     }
103
104     protected function getDetailsUrl($uuid)
105     {
106         return $this->getBaseUrl()
107             . 'api/v1/apps/' . rawurlencode($uuid);
108     }
109 }
110 ?>