mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-29 06:25:18 -04:00
Refactor API.
This commit is contained in:
parent
5c72378b39
commit
d5a1a829e3
4 changed files with 36 additions and 16 deletions
|
@ -59,24 +59,24 @@ class Api {
|
|||
if (Util::query_request_param("items", false)) {
|
||||
|
||||
$href = Util::query_request_param("items.href");
|
||||
$what = Util::query_request_param("items.what");
|
||||
$what = is_numeric($what) ? intval($what, 10) : 1;
|
||||
$what = Util::query_numeric_request_param("items.what");
|
||||
|
||||
$response["items"] = $this->app->get_items($href, $what);
|
||||
}
|
||||
|
||||
if (Util::query_request_param("custom", false)) {
|
||||
|
||||
Util::json_fail(Util::ERR_DISABLED, "custom disabled", !$this->app->get_option("custom.enabled", false));
|
||||
|
||||
$href = Util::query_request_param("custom");
|
||||
|
||||
$response["custom"] = $this->app->get_customizations($href);
|
||||
}
|
||||
|
||||
if (Util::query_request_param("l10n", false)) {
|
||||
|
||||
Util::json_fail(Util::ERR_DISABLED, "l10n disabled", !$this->app->get_option("l10n.enabled", false));
|
||||
$iso_codes = Util::query_array_request_param("l10n");
|
||||
|
||||
$iso_codes = Util::query_request_param("l10n");
|
||||
$iso_codes = array_filter($iso_codes);
|
||||
$response["l10n"] = $this->app->get_l10n($iso_codes);
|
||||
}
|
||||
|
@ -84,9 +84,9 @@ class Api {
|
|||
if (Util::query_request_param("search", false)) {
|
||||
|
||||
Util::json_fail(Util::ERR_DISABLED, "search disabled", !$this->app->get_option("search.enabled", false));
|
||||
|
||||
$href = Util::query_request_param("search.href");
|
||||
$pattern = Util::query_request_param("search.pattern");
|
||||
|
||||
$search = new Search($this->app);
|
||||
$response["search"] = $search->get_items($href, $pattern);
|
||||
}
|
||||
|
@ -95,8 +95,8 @@ class Api {
|
|||
|
||||
Util::json_fail(Util::ERR_DISABLED, "thumbnails disabled", !$this->app->get_option("thumbnails.enabled", false));
|
||||
Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG);
|
||||
$thumbs = Util::query_array_request_param("thumbs");
|
||||
|
||||
$thumbs = Util::query_request_param("thumbs");
|
||||
$response["thumbs"] = $this->app->get_thumbs($thumbs);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
class App {
|
||||
|
||||
private static $RE_DELIMITER = "|";
|
||||
private static $ICON_EXTS = ["svg", "png", "jpg"];
|
||||
private static $CUSTOM_EXTS = ["html", "md"];
|
||||
|
||||
|
@ -131,7 +130,7 @@ class App {
|
|||
}
|
||||
|
||||
foreach ($this->get_option("view.hidden", []) as $re) {
|
||||
$re = App::$RE_DELIMITER . str_replace(App::$RE_DELIMITER, '\\' . App::$RE_DELIMITER, $re) . App::$RE_DELIMITER;
|
||||
$re = Util::wrap_pattern($re);
|
||||
if (preg_match($re, $name)) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -10,11 +10,12 @@ class Search {
|
|||
function get_paths($root, $pattern = null) {
|
||||
|
||||
$paths = [];
|
||||
if ($this->app->is_managed_path($root)) {
|
||||
if ($pattern && $this->app->is_managed_path($root)) {
|
||||
$re = Util::wrap_pattern($pattern);
|
||||
$names = $this->app->read_dir($root);
|
||||
foreach ($names as $name) {
|
||||
$path = $root . "/" . $name;
|
||||
if ($pattern && $this->matches($path, $pattern)) {
|
||||
if (preg_match($re, @basename($path))) {
|
||||
$paths[] = $path;
|
||||
}
|
||||
if (@is_dir($path)) {
|
||||
|
@ -36,9 +37,4 @@ class Search {
|
|||
}, $paths);
|
||||
return $items;
|
||||
}
|
||||
|
||||
function matches($path, $pattern) {
|
||||
|
||||
return preg_match($pattern, basename($path)) === 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,12 @@ class Util {
|
|||
|
||||
|
||||
const ERR_MISSING_PARAM = "ERR_MISSING_PARAM";
|
||||
const ERR_ILLIGAL_PARAM = "ERR_ILLIGAL_PARAM";
|
||||
const ERR_FAILED = "ERR_FAILED";
|
||||
const ERR_DISABLED = "ERR_DISABLED";
|
||||
const ERR_UNSUPPORTED = "ERR_UNSUPPORTED";
|
||||
const NO_DEFAULT = "NO_*@+#?!_DEFAULT";
|
||||
const RE_DELIMITER = "|";
|
||||
|
||||
|
||||
public static function normalize_path($path, $trailing_slash = false) {
|
||||
|
@ -70,7 +72,24 @@ class Util {
|
|||
|
||||
public static function query_boolean_request_param($keypath = "", $default = Util::NO_DEFAULT) {
|
||||
|
||||
return filter_var(Util::query_request_param($keypath, $default), FILTER_VALIDATE_BOOLEAN);
|
||||
$value = Util::query_request_param($keypath, $default);
|
||||
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
|
||||
public static function query_numeric_request_param($keypath = "", $default = Util::NO_DEFAULT) {
|
||||
|
||||
$value = Util::query_request_param($keypath, $default);
|
||||
Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is not numeric", !is_numeric($value));
|
||||
return intval($value, 10);
|
||||
}
|
||||
|
||||
|
||||
public static function query_array_request_param($keypath = "", $default = Util::NO_DEFAULT) {
|
||||
|
||||
$value = Util::query_request_param($keypath, $default);
|
||||
Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is no array", !is_array($value));
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,6 +105,12 @@ class Util {
|
|||
}
|
||||
|
||||
|
||||
public static function wrap_pattern($pattern) {
|
||||
|
||||
return Util::RE_DELIMITER . str_replace(Util::RE_DELIMITER, '\\' . Util::RE_DELIMITER, $pattern) . Util::RE_DELIMITER;
|
||||
}
|
||||
|
||||
|
||||
public static function load_commented_json($path) {
|
||||
|
||||
if (!file_exists($path)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue