app details (image list)
authorChristian Weiske <cweiske@cweiske.de>
Wed, 11 Sep 2013 18:54:24 +0000 (20:54 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 11 Sep 2013 18:54:24 +0000 (20:54 +0200)
src/imagestore/Controller/Api.php
src/imagestore/Controller/Api/Apps.php [new file with mode: 0644]
src/imagestore/Controller/Api/ImageBase.php [new file with mode: 0644]
src/imagestore/Controller/Api/Playlists.php
src/imagestore/Controller/Base.php [new file with mode: 0644]
src/imagestore/Controller/Image.php

index 42f045a..91e4373 100644 (file)
@@ -9,11 +9,13 @@ class Controller_Api
             $this->error(404, 'Only API v1 supported');
         }
 
-        $actionName = substr($uri, 3);
+        $rest = substr($uri, 3);
+        $parts = explode('/', $rest);
+        $actionName = $parts[0];
         $class = 'imagestore\Controller_Api_' . ucfirst($actionName);
 
         $action = new $class();
-        $action->handle();
+        $action->handle(substr($rest, strlen($parts[0]) + 1));
     }
 }
 ?>
diff --git a/src/imagestore/Controller/Api/Apps.php b/src/imagestore/Controller/Api/Apps.php
new file mode 100644 (file)
index 0000000..32b6ca6
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+namespace imagestore;
+
+class Controller_Api_Apps extends Controller_Api_ImageBase
+{
+    public function handle($data)
+    {
+        $path = str_replace('-.-', '/', $data);
+        $fullPath = $GLOBALS['imagestore']['basedir'] . $path;
+        if (!is_dir($fullPath)) {
+            return $this->error('404 Not Found', 'Path not found');
+        }
+
+        $dirInfo = new \SplFileInfo($fullPath);
+
+        $data = array(
+            'app' => array(
+                'apkFileSize'   => 123456789,
+                'contentRating' => 'Everyone',
+                'description'   => 'Images from folder ' . $fullPath,
+                'developer'     => 'Christian Weiske',
+                'filepickerScreenshots' => $this->getScreenshots($dirInfo),
+                'founder'       => false,
+                'latestVersion' => '11111111-0000-1111-0000-111111111111',
+                'likeCount'     => 1337,
+                'mainImageFullUrl' => $this->getImageUrl($this->getFirstImage($dirInfo)),
+                'overview'      => 'Images from a folder',
+                'publishedAt'   => date('c', $dirInfo->getMTime()),
+                'screenshots'   => array(),
+                'supportEmailAddress' => 'cweiske+ouya@cweiske.de',
+                'supportPhone'  => null,
+                'title'         => $dirInfo->getBasename(),
+                'uuid'          => $data,
+                'versionNumber' => '0.0.1',
+                'website'       => 'http://git.cweiske.de/ouya-imagestore.git/'
+            )
+        );
+
+        header('Content-Type: application/json');
+        echo json_encode($data, JSON_PRETTY_PRINT);
+    }
+
+    protected function getScreenshots(\SplFileInfo $dirInfo)
+    {
+        $lit = new \LimitIterator($this->getImageIterator($dirInfo), 0, 20);
+        $urls = array();
+        foreach ($lit as $file) {
+            $urls[] = $this->getImageUrl($file);
+        }
+        return $urls;
+    }
+}
+?>
diff --git a/src/imagestore/Controller/Api/ImageBase.php b/src/imagestore/Controller/Api/ImageBase.php
new file mode 100644 (file)
index 0000000..e48a52d
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+namespace imagestore;
+
+class Controller_Api_ImageBase extends Controller_Base
+{
+    protected function getBaseUrl()
+    {
+        if (isset($_SERVER['HTTPS'])) {
+        } else {
+            $protocol = 'http';
+        }
+        return $protocol . '://' . $_SERVER['HTTP_HOST'] . '/';
+    }
+
+    protected function getFirstImage(\SplFileInfo $dirInfo)
+    {
+        $it = $this->getImageIterator($dirInfo);
+        $it->rewind();
+        return $it->current();
+    }
+
+    protected function getImageIterator(\SplFileInfo $dirInfo)
+    {
+        $it = new \AppendIterator();
+        $it->append(
+            new \GlobIterator($dirInfo->getPathName() . '/*.jpg')
+        );
+        $it->append(
+            new \GlobIterator($dirInfo->getPathName() . '/*.JPG')
+        );
+        return $it;
+    }
+
+    protected function getImageUrl($path)
+    {
+        return $this->getBaseUrl()
+            . 'image?path=' . urlencode($this->getRelPath($path));
+    }
+
+    protected function getRelPath($path)
+    {
+        return substr($path, strlen($GLOBALS['imagestore']['basedir']));
+    }
+}
+?>
index 98d4974..5728efe 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace imagestore;
 
-class Controller_Api_Playlists
+class Controller_Api_Playlists extends Controller_Api_ImageBase
 {
     public function handle()
     {
@@ -23,14 +23,16 @@ class Controller_Api_Playlists
                 'tiles' => array()
             );
             foreach ($arDirs as $dirInfo) {
-                //FIXME: uuid dots?
-                $uuid = $this->getRelPath($dirInfo->getPathname());
+                $uuid = str_replace(
+                    '/', '-.-', $this->getRelPath($dirInfo->getPathname())
+                );
                 $playlists->games[] = (object) array(
                     'content_rating' => 'Everyone', 
                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo)),
                     '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
@@ -67,24 +69,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 +78,10 @@ class Controller_Api_Playlists
         return $arGroups;
     }
 
-    protected function getRelPath($path)
+    protected function getDetailsUrl($uuid)
     {
-        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)
-    {
-        $it = $this->getImageIterator($dirInfo);
-        $it->rewind();
-        return $it->current();
+        return $this->getBaseUrl()
+            . 'api/v1/apps/' . rawurlencode($uuid);
     }
 }
 ?>
diff --git a/src/imagestore/Controller/Base.php b/src/imagestore/Controller/Base.php
new file mode 100644 (file)
index 0000000..de7e03f
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+namespace imagestore;
+
+abstract class Controller_Base
+{
+    protected function error($error, $message)
+    {
+        header('HTTP/1.0 ' . $error);
+        header('Content-type: text/plain; charset=utf-8');
+        echo $message;
+    }
+}
+?>
index 31831b8..f606d04 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace imagestore;
 
-class Controller_Image
+class Controller_Image extends Controller_Base
 {
     public function handle($uri)
     {
@@ -42,12 +42,5 @@ class Controller_Image
         imagejpeg($thumb);
         imagedestroy($thumb);
     }
-
-    protected function error($error, $message)
-    {
-        header('HTTP/1.0 ' . $error);
-        header('Content-type: text/plain; charset=utf-8');
-        echo $message;
-    }
 }
 ?>