Expand preview media support

This commit is contained in:
Mikei386
2026-06-23 13:51:35 +02:00
parent c7b9bf0a41
commit 9f1119a72f
7 changed files with 292 additions and 23 deletions
@@ -1,16 +1,17 @@
<?php
require_once __DIR__ . '/common.php';
const UNAV_PREVIEW_MAX_BYTES = 1048576;
const UNAV_TEXT_PREVIEW_MAX_BYTES = 1048576;
$resolved = unav_existing_path($_GET['path'] ?? null);
if (!is_file($resolved['path'])) {
unav_error(400, 'Preview target must be a file');
}
$name = basename($resolved['path']);
$extension = strtolower(pathinfo($resolved['path'], PATHINFO_EXTENSION));
$allowed = ['txt', 'log', 'md', 'rd', 'rst', 'yaml', 'yml', 'json', 'xml', 'csv', 'ini', 'cfg', 'conf', 'sh', 'php', 'js', 'css', 'html'];
if (!in_array($extension, $allowed, true)) {
$type = unav_preview_type($extension, $name);
if ($type === null) {
unav_error(415, 'Preview is not available for this file type');
}
@@ -18,22 +19,93 @@ $size = filesize($resolved['path']);
if ($size === false) {
unav_error(500, 'Could not read file size');
}
if ($size > UNAV_PREVIEW_MAX_BYTES) {
unav_error(413, 'Preview file is larger than 1 MB');
$mime = unav_preview_mime($extension, $type);
if (($_GET['raw'] ?? '') === '1') {
header('Content-Type: ' . $mime);
header('Content-Length: ' . $size);
header('X-Content-Type-Options: nosniff');
readfile($resolved['path']);
exit;
}
$content = file_get_contents($resolved['path']);
if ($content === false) {
unav_error(500, 'Could not read file');
}
if (strpos($content, "\0") !== false) {
unav_error(415, 'Preview is not available for binary files');
}
unav_json([
$payload = [
'path' => $resolved['path'],
'name' => basename($resolved['path']),
'name' => $name,
'size' => $size,
'extension' => $extension,
'content' => $content,
]);
'type' => $type,
'mime' => $mime,
'rawUrl' => 'preview.php?raw=1&path=' . rawurlencode($resolved['path']),
];
if (in_array($type, ['text', 'markdown', 'csv'], true)) {
if ($size > UNAV_TEXT_PREVIEW_MAX_BYTES) {
unav_error(413, 'Preview file is larger than 1 MB');
}
$content = file_get_contents($resolved['path']);
if ($content === false) {
unav_error(500, 'Could not read file');
}
if (strpos($content, "\0") !== false) {
unav_error(415, 'Preview is not available for binary files');
}
$payload['content'] = $content;
}
unav_json($payload);
function unav_preview_type(string $extension, string $name): ?string {
$base = strtolower($name);
if (in_array($base, ['dockerfile', 'makefile'], true)) {
return 'text';
}
$text = ['txt', 'log', 'rd', 'rst', 'yaml', 'yml', 'json', 'xml', 'ini', 'cfg', 'conf', 'env', 'toml', 'sql', 'nfo', 'srt', 'sub', 'vtt', 'm3u', 'm3u8', 'pls', 'sh', 'bash', 'zsh', 'php', 'js', 'ts', 'tsx', 'jsx', 'css', 'html', 'py', 'go', 'rs', 'java', 'c', 'cpp', 'h', 'hpp', 'bat', 'ps1', 'properties', 'service', 'timer', 'mount', 'rules', 'cron', 'plg', 'page'];
if (in_array($extension, $text, true)) {
return 'text';
}
if ($extension === 'md') {
return 'markdown';
}
if (in_array($extension, ['csv', 'tsv'], true)) {
return 'csv';
}
if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'], true)) {
return 'image';
}
if ($extension === 'pdf') {
return 'pdf';
}
if (in_array($extension, ['mp3', 'flac', 'm4a', 'ogg', 'wav'], true)) {
return 'audio';
}
if (in_array($extension, ['mp4', 'mkv', 'webm', 'mov'], true)) {
return 'video';
}
return null;
}
function unav_preview_mime(string $extension, string $type): string {
$map = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
'pdf' => 'application/pdf',
'mp3' => 'audio/mpeg',
'flac' => 'audio/flac',
'm4a' => 'audio/mp4',
'ogg' => 'audio/ogg',
'wav' => 'audio/wav',
'mp4' => 'video/mp4',
'mkv' => 'video/x-matroska',
'webm' => 'video/webm',
'mov' => 'video/quicktime',
'csv' => 'text/csv; charset=utf-8',
'tsv' => 'text/tab-separated-values; charset=utf-8',
'md' => 'text/markdown; charset=utf-8',
];
return $map[$extension] ?? ($type === 'text' ? 'text/plain; charset=utf-8' : 'application/octet-stream');
}