From 0329392dd6a519302b57b4e95cd5638e85039d2c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 19 May 2023 23:08:46 +0800 Subject: [PATCH] fix resource in "/" and mime-type problem --- composer.json | 3 +- composer.lock | 113 ++++++++++++++++++++++++++++++++++++---- constant.php | 2 +- src/Data/PageMeta.php | 2 +- src/Utils/PageParse.php | 49 ++++++----------- 5 files changed, 123 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index 12141fe..c4786ca 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "ext-mbstring": "*", "ext-fileinfo": "*", "league/commonmark": ">=2.4", - "symfony/yaml": ">=4.0" + "symfony/yaml": ">=4.0", + "elephox/mimey": ">=4.0" } } diff --git a/composer.lock b/composer.lock index d692d8f..f5e634a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fc6b152675eaee41381b156736040f42", + "content-hash": "5b03081f00ca7fdde9829be69348733a", "packages": [ { "name": "dflydev/dot-access-data", @@ -81,6 +81,102 @@ }, "time": "2022-10-27T11:44:00+00:00" }, + { + "name": "elephox/mimey", + "version": "4.0.5", + "source": { + "type": "git", + "url": "https://github.com/elephox-dev/mimey.git", + "reference": "2b5910b3b35613feb47436d392f857e87183fcc9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/elephox-dev/mimey/zipball/2b5910b3b35613feb47436d392f857e87183fcc9", + "reference": "2b5910b3b35613feb47436d392f857e87183fcc9", + "shasum": "" + }, + "require": { + "jetbrains/phpstorm-attributes": "^1.0", + "php": "^8.1 <8.3" + }, + "require-dev": { + "phpunit/phpunit": "^9.6" + }, + "suggest": { + "ext-mbstring": "For non-English (user) input parsing" + }, + "type": "library", + "autoload": { + "psr-4": { + "Elephox\\Mimey\\": [ + "src/", + "dist/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + }, + { + "name": "Ricardo Boss", + "email": "contact@ricardoboss.de" + } + ], + "description": "PHP package for converting file extensions to MIME types and vice versa.", + "support": { + "issues": "https://github.com/elephox-dev/mimey/issues", + "source": "https://github.com/elephox-dev/mimey/tree/4.0.5" + }, + "time": "2023-02-21T14:45:43+00:00" + }, + { + "name": "jetbrains/phpstorm-attributes", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/JetBrains/phpstorm-attributes.git", + "reference": "a7a83ae5df4dd3c0875484483de19de8edf60a9f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/JetBrains/phpstorm-attributes/zipball/a7a83ae5df4dd3c0875484483de19de8edf60a9f", + "reference": "a7a83ae5df4dd3c0875484483de19de8edf60a9f", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "JetBrains\\PhpStorm\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "JetBrains", + "homepage": "https://www.jetbrains.com" + } + ], + "description": "PhpStorm specific attributes", + "keywords": [ + "attributes", + "jetbrains", + "phpstorm" + ], + "support": { + "issues": "https://youtrack.jetbrains.com/newIssue?project=WI", + "source": "https://github.com/JetBrains/phpstorm-attributes/tree/1.0" + }, + "time": "2020-11-17T11:09:47+00:00" + }, { "name": "league/commonmark", "version": "2.4.0", @@ -702,16 +798,16 @@ }, { "name": "symfony/yaml", - "version": "v6.2.7", + "version": "v6.3.0-BETA1", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "e8e6a1d59e050525f27a1f530aa9703423cb7f57" + "reference": "a9a8337aa641ef2aa39c3e028f9107ec391e5927" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/e8e6a1d59e050525f27a1f530aa9703423cb7f57", - "reference": "e8e6a1d59e050525f27a1f530aa9703423cb7f57", + "url": "https://api.github.com/repos/symfony/yaml/zipball/a9a8337aa641ef2aa39c3e028f9107ec391e5927", + "reference": "a9a8337aa641ef2aa39c3e028f9107ec391e5927", "shasum": "" }, "require": { @@ -724,9 +820,6 @@ "require-dev": { "symfony/console": "^5.4|^6.0" }, - "suggest": { - "symfony/console": "For validating YAML files using the lint command" - }, "bin": [ "Resources/bin/yaml-lint" ], @@ -756,7 +849,7 @@ "description": "Loads and dumps YAML files", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/yaml/tree/v6.2.7" + "source": "https://github.com/symfony/yaml/tree/v6.3.0-BETA1" }, "funding": [ { @@ -772,7 +865,7 @@ "type": "tidelift" } ], - "time": "2023-02-16T09:57:23+00:00" + "time": "2023-04-28T13:28:14+00:00" } ], "packages-dev": [], diff --git a/constant.php b/constant.php index 1f961fa..970ad35 100644 --- a/constant.php +++ b/constant.php @@ -2,6 +2,6 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.5.0-alpha7"; +const VERSION = "0.5.0-alpha8"; const CHANNEL = "suk-ws"; const BRANCH = "config-v2.0"; diff --git a/src/Data/PageMeta.php b/src/Data/PageMeta.php index 1ea0ea3..f1dbe24 100644 --- a/src/Data/PageMeta.php +++ b/src/Data/PageMeta.php @@ -31,7 +31,7 @@ class PageMeta { } else { $tmp = SiteMeta::getBookshelf()->getBook($uri[0]); if ($tmp == null) - throw new RequestNotExistException("Book required \"$uri[0]\" not found!"); + return false; self::$bookId = $uri[0]; self::$book = $tmp->getContentedNode(); } diff --git a/src/Utils/PageParse.php b/src/Utils/PageParse.php index 5649c4b..1b1cb7d 100644 --- a/src/Utils/PageParse.php +++ b/src/Utils/PageParse.php @@ -2,7 +2,8 @@ namespace SukWs\Bookshelf\Utils; -use SukWs\Bookshelf\Resource\Resource; +use Elephox\Mimey\MimeType; +use JetBrains\PhpStorm\NoReturn; class PageParse { @@ -26,20 +27,7 @@ class PageParse { exit("File Can't Read!"); } // 判定文件类型 - $fileMime = mime_content_type($filePath); - $isText = false; - if ($fileMime == "text/plain") { - $isText = true; - switch ($fileExtension) { - case "css": - $fileMime = "text/css"; - break; - case "js": - $fileMime = "application/javascript"; - break; - default: - } - } + $fileMime = MimeType::fromExtension($fileExtension)->getValue(); // 文件类型是二进制流。设置为utf8编码(支持中文文件名称) header('Content-type:'.$fileMime.'; charset=utf-8'); header("Access-Control-Allow-Origin: * "); @@ -47,28 +35,23 @@ class PageParse { // 触发浏览器文件下载功能 header('Content-Disposition:attachment;filename="'.urlencode($filename).'"'); } - if ($isText) { - echo file_get_contents($filePath); - } else { - // 二进制文件数据头 - header("Accept-Ranges: bytes"); - header("Content-Length: ".filesize($filePath)); - // 以只读方式打开文件,并强制使用二进制模式 - $fileHandle = fopen($filePath, "rb"); - if ($fileHandle === false) { - exit("read false"); - } - // 循环读取文件内容,并输出 - while (!feof($fileHandle)) { - // 从文件指针 handle 读取最多 length 个字节(每次输出10k) - echo fread($fileHandle, 10240); - } - // 关闭文件流 - fclose($fileHandle); + // 二进制文件数据头 + // 以只读方式打开文件,并强制使用二进制模式 + $fileHandle = fopen($filePath, "rb"); + if ($fileHandle === false) { + exit("read false"); } + // 循环读取文件内容,并输出 + while (!feof($fileHandle)) { + // 从文件指针 handle 读取最多 length 个字节(每次输出10k) + echo fread($fileHandle, 10240); + } + // 关闭文件流 + fclose($fileHandle); return true; } + #[NoReturn] public static function output302 (string $url): void { header("Location: $url", true, 302); exit;