From 12c0b21e8e14aa4925f7a2bc2ecd30bb3e412c67 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 15 Jan 2024 12:16:04 +0800 Subject: [PATCH] remove safety check and better md support --- constant.php | 2 +- src/Resource/Data.php | 10 +--------- src/Utils/FileMime.php | 28 ++++++++++++++++++++++++++++ src/Utils/PageParse.php | 2 +- 4 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/Utils/FileMime.php diff --git a/constant.php b/constant.php index 9aeae13..3523452 100644 --- a/constant.php +++ b/constant.php @@ -2,6 +2,6 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.5.0-alpha10"; +const VERSION = "0.5.0-alpha11"; const CHANNEL = "suk-ws"; const BRANCH = "config-v2.0"; diff --git a/src/Resource/Data.php b/src/Resource/Data.php index 3c660fe..cb80a64 100644 --- a/src/Resource/Data.php +++ b/src/Resource/Data.php @@ -18,18 +18,10 @@ class Data { public static function get(string $id): Data|false { $path = realpath(self::root.$id); - if ($path !== false && self::checkSafety($path)) { + if ($path !== false) { return new Data($path); } return false; } - private static function getRealRootPath(): string { - return realpath(self::root); - } - - private static function checkSafety (string $checked): bool { - return str_starts_with(realpath($checked), self::getRealRootPath()); - } - } \ No newline at end of file diff --git a/src/Utils/FileMime.php b/src/Utils/FileMime.php new file mode 100644 index 0000000..bae919d --- /dev/null +++ b/src/Utils/FileMime.php @@ -0,0 +1,28 @@ +add('text/markdown', 'md'); + $builder->add('text/markdown', 'mkd'); + $builder->add('text/markdown', 'mdwn'); + $builder->add('text/markdown', 'mdown'); + $builder->add('text/markdown', 'mdtxt'); + $builder->add('text/markdown', 'mdtext'); + $builder->add('text/markdown', 'markdown'); + self::$mimes = new MimeTypes($builder->getMapping()); + } + + public static function fromExtension($ext): string { + return self::$mimes->getMimeType($ext) ?: "application/octet-stream"; + } + +} FileMime::init(); diff --git a/src/Utils/PageParse.php b/src/Utils/PageParse.php index 1b1cb7d..825ce82 100644 --- a/src/Utils/PageParse.php +++ b/src/Utils/PageParse.php @@ -27,7 +27,7 @@ class PageParse { exit("File Can't Read!"); } // 判定文件类型 - $fileMime = MimeType::fromExtension($fileExtension)->getValue(); + $fileMime = FileMime::fromExtension($fileExtension); // 文件类型是二进制流。设置为utf8编码(支持中文文件名称) header('Content-type:'.$fileMime.'; charset=utf-8'); header("Access-Control-Allow-Origin: * ");