From: Christian Weiske Date: Wed, 11 Sep 2013 18:18:48 +0000 (+0200) Subject: api/playlists and image resizing + delivery X-Git-Url: https://git.cweiske.de/ouya-imagestore.git/commitdiff_plain/331d593564ee818a3cc9e4cb6969b8d97fe62d1f api/playlists and image resizing + delivery --- 331d593564ee818a3cc9e4cb6969b8d97fe62d1f diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d041e45 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/data/config.php diff --git a/src/imagestore/Autoloader.php b/src/imagestore/Autoloader.php new file mode 100644 index 0000000..cde5c89 --- /dev/null +++ b/src/imagestore/Autoloader.php @@ -0,0 +1,22 @@ + diff --git a/src/imagestore/Controller/Api.php b/src/imagestore/Controller/Api.php new file mode 100644 index 0000000..42f045a --- /dev/null +++ b/src/imagestore/Controller/Api.php @@ -0,0 +1,19 @@ +error(404, 'Only API v1 supported'); + } + + $actionName = substr($uri, 3); + $class = 'imagestore\Controller_Api_' . ucfirst($actionName); + + $action = new $class(); + $action->handle(); + } +} +?> diff --git a/src/imagestore/Controller/Api/Playlists.php b/src/imagestore/Controller/Api/Playlists.php new file mode 100644 index 0000000..98d4974 --- /dev/null +++ b/src/imagestore/Controller/Api/Playlists.php @@ -0,0 +1,119 @@ + array(), + 'playlists' => array() + ); + //-1 - link to playlists + //-2 - ???? + //1 - featured + //2 - trending now + $id = 0; + foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) { + $plnum = count($playlists->playlists); + $playlists->playlists[$plnum] = (object) array( + 'id' => ++$id, + 'image' => '', + 'name' => $dir, + 'tiles' => array() + ); + foreach ($arDirs as $dirInfo) { + //FIXME: uuid dots? + $uuid = $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' + ); + $playlists->playlists[$plnum]->tiles[] = (object) array( + 'game' => $uuid + ); + } + } + + header('Content-Type: application/json'); + echo json_encode($playlists, JSON_PRETTY_PRINT); + } + + protected function getDirs() + { + $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*'); + $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*'); + + $all = new \AppendIterator(); + $all->append($dirs); + $all->append($dirs2); + + $allDirs = new \CallbackFilterIterator( + $all, + function ($fileInfo) { + return $fileInfo->isDir() && $this->hasImages($fileInfo); + } + ); + return new \CachingIterator($allDirs); + } + + protected function hasImages(\SplFileInfo $dirInfo) + { + $it = $this->getImageIterator($dirInfo); + $it->rewind(); + 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(); + foreach ($dirs as $dirInfo) { + $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo; + } + 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) + { + $it = $this->getImageIterator($dirInfo); + $it->rewind(); + return $it->current(); + } +} +?> diff --git a/src/imagestore/Controller/Image.php b/src/imagestore/Controller/Image.php new file mode 100644 index 0000000..31831b8 --- /dev/null +++ b/src/imagestore/Controller/Image.php @@ -0,0 +1,53 @@ +error('400 Bad request', 'Path missing'); + } + + $path = $GLOBALS['imagestore']['basedir'] . $_GET['path']; + if (!file_exists($path)) { + return $this->error('404 Not Found', 'File not found'); + } + + if (!isset($_GET['w'])) { + header('Content-type: image/jpeg'); + header('Content-length: ' . filesize($path)); + return readfile($path); + } + + //resize + $img = imagecreatefromjpeg($path); + + $newWidth = (int) $_GET['w']; + //if (isset($_GET['h'])) { + // $newHeight = (int) $_GET['h']; + //} else { + $newHeight = intval($newWidth / imagesx($img) * imagesy($img)); + //} + + $thumb = imagecreatetruecolor($newWidth, $newHeight); + // Resize + imagecopyresampled( + $thumb, $img, 0, 0, 0, 0, + $newWidth, $newHeight, imagesx($img), imagesy($img) + ); + imagedestroy($img); + + header('Content-type: image/jpeg'); + imagejpeg($thumb); + imagedestroy($thumb); + } + + protected function error($error, $message) + { + header('HTTP/1.0 ' . $error); + header('Content-type: text/plain; charset=utf-8'); + echo $message; + } +} +?> diff --git a/www/.htaccess b/www/.htaccess new file mode 100644 index 0000000..dc57549 --- /dev/null +++ b/www/.htaccess @@ -0,0 +1,11 @@ + + RewriteEngine On + + RewriteCond %{REQUEST_FILENAME} -f [OR] + RewriteCond %{REQUEST_FILENAME} -d [OR] + RewriteCond %{REQUEST_FILENAME} -l + RewriteRule .* - [L] + + # Rewrite the rest to index.php + RewriteRule .* /index.php [L] + diff --git a/www/index.php b/www/index.php new file mode 100644 index 0000000..8417d26 --- /dev/null +++ b/www/index.php @@ -0,0 +1,20 @@ +handle(substr($url, 5)); +} else if ($url == '/image') { + $api = new Controller_Image(); + $api->handle($url); +} else { + header('HTTP/1.0 404 Not Found'); + echo "I can't handle this request\n"; +} +?> \ No newline at end of file