post_item(), ['/item', 'GET'] => get_item(), ['/search', 'GET'] => get_search(), default => Json::error('Not implemented') })->die(); function get_item() { $id = $_GET['q'] ?? false; if (!$id) Json::error('ID not specified')->die(); $media_db = new MediaDB(); $item = Item::load($media_db, $id); if ($item === null) { Json::error('No item was found with the given ID')->die(); } return $item->getJson(); } function post_item() { // The checks are performed internally $item = Item::upload($_FILES['file'], $_POST['tags']); return $item->getJson(); }; function get_search() { $query = $_GET['q'] ?? ''; $limit = intval($_GET['limit'] ?? '20'); // TODO: Implement page // If intval returns 0, it's an error if ($limit == 0) return Json::error('Invalid limit parameter'); if ($query !== '') return Json::error('Search not implemented yet'); $list = []; $db = new MediaDB(); $db->map(function ($line) use (&$list, $limit) { if (count($list) >= $limit) return true; $list[] = Item::fromLine($line); return false; }); return Json::new($list); }