escape spaces in folders
[ouya-imagestore.git] / src / imagestore / Controller / Api / Playlists.php
index 98d497418c1a3180e35a80f5d6d98545091d492d..d35d7860ab12c61fb5eebaca321f144a6b224774 100644 (file)
@@ -1,18 +1,31 @@
 <?php
 namespace imagestore;
 
-class Controller_Api_Playlists
+class Controller_Api_Playlists extends Controller_Api_ImageBase
 {
     public function handle()
     {
         $playlists = (object) array(
             'games' => array(),
-            'playlists' => array()
+            'playlists' => array(
+                (object) array(
+                    //main playlists in the store?
+                    'name'  => null,
+                    'id'    => -1,
+                    'image' => null,
+                    'tiles' => array()
+                ),
+                (object) array(
+                    //mainpage
+                    'name'  => null,
+                    'id'    => -2,
+                    'image' => null,
+                    'tiles' => array()
+                )
+            )
         );
-        //-1 - link to playlists
-        //-2 - ????
-        //1 - featured
-        //2 - trending now
+
+        $mainpage = 0;
         $id = 0;
         foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
             $plnum = count($playlists->playlists);
@@ -22,19 +35,31 @@ class Controller_Api_Playlists
                 'name'  => $dir,
                 'tiles' => array()
             );
+            $playlists->playlists[0]->tiles[] = array('playlist' => $id);
+
             foreach ($arDirs as $dirInfo) {
-                //FIXME: uuid dots?
-                $uuid = $this->getRelPath($dirInfo->getPathname());
+                $uuid = str_replace(
+                    array('/', ' '), array('-.-', '+'),
+                    $this->getRelPath($dirInfo->getPathname())
+                );
                 $playlists->games[] = (object) array(
                     'content_rating' => 'Everyone', 
-                    'image'   => $this->getImageUrl($this->getFirstImage($dirInfo)),
+                    'image'   => $this->getImageUrl($this->getFirstImage($dirInfo))
+                        . '&w=400',
                     'title'   => basename($dirInfo->getPathname()), 
                     'uuid'    =>  $uuid,
-                    'version' => '11111111-0000-1111-0000-111111111111'
+                    'version' => '11111111-0000-1111-0000-111111111111',
+                    '__details' => $this->getDetailsUrl($uuid),
                 );
                 $playlists->playlists[$plnum]->tiles[] = (object) array(
                     'game' => $uuid
                 );
+
+                if (++$mainpage < 10) {
+                    $playlists->playlists[1]->tiles[] = (object) array(
+                        'game' => $uuid
+                    );
+                }
             }
         }
 
@@ -67,24 +92,6 @@ class Controller_Api_Playlists
         return $it->valid();
     }
 
-    protected function getImageIterator(\SplFileInfo $dirInfo)
-    {
-        $it = new \AppendIterator();
-        $it->append(
-            new \LimitIterator(
-                new \GlobIterator($dirInfo->getPathName() . '/*.jpg'),
-                0, 1
-            )
-        );
-        $it->append(
-            new \LimitIterator(
-                new \GlobIterator($dirInfo->getPathName() . '/*.JPG'),
-                0, 1
-            )
-        );
-        return $it;
-    }
-
     protected function groupByParent($dirs)
     {
         $arGroups = array();
@@ -94,26 +101,10 @@ class Controller_Api_Playlists
         return $arGroups;
     }
 
-    protected function getRelPath($path)
-    {
-        return substr($path, strlen($GLOBALS['imagestore']['basedir']));
-    }
-
-    protected function getImageUrl($path)
-    {
-        if (isset($_SERVER['HTTPS'])) {
-        } else {
-            $protocol = 'http';
-        }
-        return $protocol . '://' . $_SERVER['HTTP_HOST']
-            . '/image?path=' . urlencode($this->getRelPath($path));
-    }
-
-    protected function getFirstImage(\SplFileInfo $dirInfo)
+    protected function getDetailsUrl($uuid)
     {
-        $it = $this->getImageIterator($dirInfo);
-        $it->rewind();
-        return $it->current();
+        return $this->getBaseUrl()
+            . 'api/v1/apps/' . rawurlencode($uuid);
     }
 }
 ?>