arrel/main.php

81 lines
1.8 KiB
PHP
Raw Normal View History

2023-07-10 02:41:00 +00:00
<?php
$_dr = $_SERVER['DOCUMENT_ROOT'];
// TODO: Make configurable in .ini
2023-07-10 04:06:59 +00:00
// 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'] =
2023-07-10 02:41:00 +00:00
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
2023-07-10 04:06:59 +00:00
$GLOBALS['base_media'] = 'media';
2023-07-10 02:41:00 +00:00
header('Content-Type: application/json');
include_once($_dr . '/item.php');
include_once($_dr . '/mediadb.php');
include_once($_dr . '/json.php');
2023-07-10 04:06:59 +00:00
// ----- ROUTER -----
(match ([$_SERVER['DOCUMENT_URI'], $_SERVER['REQUEST_METHOD']]) {
2023-07-10 02:41:00 +00:00
['/item', 'POST'] => post_item(),
2023-07-10 04:06:59 +00:00
['/item', 'GET'] => get_item(),
['/search', 'GET'] => get_search(),
2023-07-10 02:41:00 +00:00
default => Json::error('Not implemented')
})->die();
2023-07-10 04:06:59 +00:00
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();
}
2023-07-10 04:14:29 +00:00
return $item->getJson();
2023-07-10 04:06:59 +00:00
}
2023-07-10 05:48:40 +00:00
2023-07-10 02:41:00 +00:00
function post_item()
{
// The checks are performed internally
2023-07-10 12:16:50 +00:00
$item = Item::upload($_FILES['file'], $_POST['tags']);
2023-07-10 02:41:00 +00:00
2023-07-10 04:14:29 +00:00
return $item->getJson();
2023-07-10 02:41:00 +00:00
};
2023-07-10 05:48:40 +00:00
function get_search()
2023-07-10 02:41:00 +00:00
{
$query = $_GET['q'] ?? '';
$limit = intval($_GET['limit'] ?? '20');
// TODO: Implement page
2023-07-10 02:41:00 +00:00
// 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');
2023-07-10 02:41:00 +00:00
$list = [];
2023-07-10 02:41:00 +00:00
$db = new MediaDB();
$db->map(function ($line) use (&$list, $limit) {
if (count($list) >= $limit) return true;
2023-07-10 02:41:00 +00:00
$list[] = Item::fromLine($line);
2023-07-10 02:41:00 +00:00
return false;
});
2023-07-10 02:41:00 +00:00
return Json::new($list);
2023-07-10 02:41:00 +00:00
}