Add text file preview window
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/common.php';
|
||||
|
||||
const UNAV_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');
|
||||
}
|
||||
|
||||
$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)) {
|
||||
unav_error(415, 'Preview is not available for this file type');
|
||||
}
|
||||
|
||||
$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');
|
||||
}
|
||||
|
||||
$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([
|
||||
'path' => $resolved['path'],
|
||||
'name' => basename($resolved['path']),
|
||||
'size' => $size,
|
||||
'extension' => $extension,
|
||||
'content' => $content,
|
||||
]);
|
||||
Reference in New Issue
Block a user