Prepare LaySite 1.4.5 site icons, Head settings and release images
This commit is contained in:
+24
-2
@@ -15,6 +15,8 @@ use Webman\Http\UploadFile;
|
||||
final class Media
|
||||
{
|
||||
public const MAX_BYTES = 64 * 1024 * 1024;
|
||||
private const MAX_CODE_BYTES = 2 * 1024 * 1024;
|
||||
private const CODE_TYPES = ['css' => 'text/css', 'js' => 'text/javascript'];
|
||||
|
||||
private const EXTENSIONS = [
|
||||
'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/webp' => 'webp',
|
||||
@@ -22,6 +24,7 @@ final class Media
|
||||
'video/mp4' => 'mp4', 'video/webm' => 'webm', 'video/quicktime' => 'mov',
|
||||
'audio/mpeg' => 'mp3', 'audio/ogg' => 'ogg', 'audio/wav' => 'wav',
|
||||
'application/pdf' => 'pdf', 'font/woff' => 'woff', 'font/woff2' => 'woff2',
|
||||
'text/css' => 'css', 'text/javascript' => 'js',
|
||||
];
|
||||
|
||||
public static function all(bool $includeArchived = false): array
|
||||
@@ -42,7 +45,7 @@ final class Media
|
||||
throw new RuntimeException('请选择大小不超过 64 MB 的有效文件。');
|
||||
}
|
||||
$source = $file->getPathname();
|
||||
$mime = (new \finfo(FILEINFO_MIME_TYPE))->file($source);
|
||||
$mime = self::mime($source, (string) $file->getUploadName(), $size);
|
||||
$extension = self::EXTENSIONS[$mime] ?? null;
|
||||
if ($extension === null) {
|
||||
throw new RuntimeException('文件类型不受支持。');
|
||||
@@ -105,7 +108,7 @@ final class Media
|
||||
|
||||
public static function validateBackup(string $path, string $name, string $mime, int $size): void
|
||||
{
|
||||
if ((new \finfo(FILEINFO_MIME_TYPE))->file($path) !== $mime
|
||||
if (self::mime($path, $name, $size) !== $mime
|
||||
|| !isset(self::EXTENSIONS[$mime]) || self::EXTENSIONS[$mime] !== pathinfo($name, PATHINFO_EXTENSION)) {
|
||||
throw new RuntimeException('备份资源类型与清单不一致。');
|
||||
}
|
||||
@@ -114,6 +117,25 @@ final class Media
|
||||
}
|
||||
}
|
||||
|
||||
private static function mime(string $path, string $name, int $size): string|false
|
||||
{
|
||||
$mime = (new \finfo(FILEINFO_MIME_TYPE))->file($path);
|
||||
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||
if (!isset(self::CODE_TYPES[$extension])) {
|
||||
return in_array($mime, self::CODE_TYPES, true) ? false : $mime;
|
||||
}
|
||||
if ($size > self::MAX_CODE_BYTES || !is_string($mime)
|
||||
|| (!str_starts_with($mime, 'text/') && !in_array($mime, ['application/javascript', 'application/json'], true))) {
|
||||
throw new RuntimeException('CSS 或 JavaScript 文件须为有效的 UTF-8 文本,且不超过 2 MB。');
|
||||
}
|
||||
$source = file_get_contents($path, length: self::MAX_CODE_BYTES + 1);
|
||||
if (!is_string($source) || strlen($source) !== $size || !mb_check_encoding($source, 'UTF-8') || str_contains($source, "\0")) {
|
||||
throw new RuntimeException('CSS 或 JavaScript 文件须为有效的 UTF-8 文本,且不超过 2 MB。');
|
||||
}
|
||||
|
||||
return self::CODE_TYPES[$extension];
|
||||
}
|
||||
|
||||
private static function safeSvg(string $source, int $size): string
|
||||
{
|
||||
if ($size > 512 * 1024) {
|
||||
|
||||
Reference in New Issue
Block a user