1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2025-01-31 21:13:00 +08:00

remove safety check and better md support

This commit is contained in:
A.C.Sukazyo Eyre 2024-01-15 12:16:04 +08:00
parent d984e11424
commit 12c0b21e8e
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
4 changed files with 31 additions and 11 deletions

View File

@ -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";

View File

@ -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());
}
}

28
src/Utils/FileMime.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace SukWs\Bookshelf\Utils;
use Elephox\Mimey\MimeMappingBuilder;
use Elephox\Mimey\MimeTypes;
class FileMime {
private static MimeTypes $mimes;
static function init (): void {
$builder = MimeMappingBuilder::create();
$builder->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();

View File

@ -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: * ");