app details (image list)
[ouya-imagestore.git] / src / imagestore / Controller / Api / ImageBase.php
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']));
+    }
+}
+?>