79 lines
1.7 KiB
PHP
79 lines
1.7 KiB
PHP
<?php
|
|
|
|
$_dr = $_SERVER['DOCUMENT_ROOT'];
|
|
// TODO: Make configurable in .ini
|
|
|
|
// Actual filesystem
|
|
$GLOBALS['path_root'] = '/srv/http/boorein';
|
|
$GLOBALS['path_media'] = $GLOBALS['path_root'] . '/media';
|
|
$GLOBALS['path_mediadb'] = $GLOBALS['path_root'] . '/media.db';
|
|
// Uri addressed
|
|
$GLOBALS['base_root'] =
|
|
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
|
|
$GLOBALS['base_media'] = 'media';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
|
include_once($_dr . '/item.php');
|
|
include_once($_dr . '/mediadb.php');
|
|
include_once($_dr . '/json.php');
|
|
|
|
|
|
// ----- ROUTER -----
|
|
(match ([$_SERVER['DOCUMENT_URI'], $_SERVER['REQUEST_METHOD']]) {
|
|
['/item', 'POST'] => 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']);
|
|
|
|
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);
|
|
}
|