feat: Editor mit Zeilennummern und deutlich mehr Textformaten
- Zeilennummern in Ansicht und Editor, Statuszeile mit Zeile/Spalte, Zeilenanzahl, Kodierung, Zeilenende und Änderungsmarker. - Tab/Umschalt+Tab rücken (auch mehrere Zeilen) ein/aus, Enter übernimmt die Einrückung, Strg+G springt zu einer Zeile. - Vor dem Speichern: JSON muss gültig sein, YAML darf nicht mit Tabs eingerückt sein (mit Option, trotzdem zu speichern). - Über 200 Endungen und typische Dateinamen (go, hosts, Dockerfile …); unbekannte Dateien werden am Inhalt als Text erkannt, "Als Text öffnen". - Windows-1252-Dateien werden angezeigt und in ihrer Kodierung gespeichert (vorher leere Antwort); CRLF bleibt erhalten. - Paket 2026.11.21.r006. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0592a02c8e
commit
1ca0d5aaae
9 files changed
+457
-31
No files matched your search
@@ -12,6 +12,10 @@ if (!is_file($resolved['path'])) {
|
||||
$name = basename($resolved['path']);
|
||||
$extension = strtolower(pathinfo($resolved['path'], PATHINFO_EXTENSION));
|
||||
$type = unav_preview_type($extension, $name);
|
||||
// Unknown extension (e.g. Unraid's /boot/config/go): open it as text if the content looks like text.
|
||||
if ($type === null && unav_looks_like_text($resolved['path'])) {
|
||||
$type = 'text';
|
||||
}
|
||||
if ($type === null) {
|
||||
unav_error(415, 'Preview is not available for this file type');
|
||||
}
|
||||
@@ -26,11 +30,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (!array_key_exists('content', $data)) {
|
||||
unav_error(400, 'Missing file content');
|
||||
}
|
||||
$content = (string)$data['content'];
|
||||
if (strlen($content) > UNAV_TEXT_PREVIEW_MAX_BYTES) {
|
||||
$text = (string)$data['content'];
|
||||
if (strlen($text) > UNAV_TEXT_PREVIEW_MAX_BYTES) {
|
||||
unav_error(413, 'Die bearbeitete Datei ist größer als 1 MB');
|
||||
}
|
||||
if (strpos($content, "\0") !== false) {
|
||||
if (strpos($text, "\0") !== false) {
|
||||
unav_error(415, 'Binärdaten können nicht im Texteditor gespeichert werden');
|
||||
}
|
||||
$expectedRevision = trim((string)($data['revision'] ?? ''));
|
||||
@@ -56,6 +60,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
fclose($handle);
|
||||
unav_error(409, 'Die Datei wurde außerhalb von U-Navigator geändert. Bitte vor dem Speichern neu laden.');
|
||||
}
|
||||
// Keep the file's encoding: Windows-1252 files are written back as Windows-1252.
|
||||
$encoding = unav_text_encoding($current);
|
||||
$content = $encoding === 'windows-1252' ? unav_encode_cp1252($text) : $text;
|
||||
if ($content === null) {
|
||||
flock($handle, LOCK_UN);
|
||||
fclose($handle);
|
||||
unav_error(422, 'Der Text enthält Zeichen, die in der Kodierung der Datei (Windows-1252) nicht möglich sind.');
|
||||
}
|
||||
rewind($handle);
|
||||
if (!ftruncate($handle, 0) || !unav_write_all($handle, $content) || !fflush($handle)) {
|
||||
flock($handle, LOCK_UN);
|
||||
@@ -70,7 +82,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
'size' => strlen($content),
|
||||
'modifiedAt' => gmdate('c', filemtime($resolved['path']) ?: time()),
|
||||
'revision' => hash('sha256', $content),
|
||||
'content' => $content,
|
||||
'content' => $text,
|
||||
'encoding' => $encoding ?? 'utf-8',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -118,8 +131,17 @@ if (in_array($type, ['text', 'markdown', 'csv'], true)) {
|
||||
if (strpos($content, "\0") !== false) {
|
||||
unav_error(415, 'Preview is not available for binary files');
|
||||
}
|
||||
$payload['content'] = $content;
|
||||
$encoding = unav_text_encoding($content);
|
||||
$payload['revision'] = hash('sha256', $content);
|
||||
$payload['encoding'] = $encoding ?? 'unknown';
|
||||
if ($encoding === 'windows-1252') {
|
||||
$content = unav_decode_cp1252($content);
|
||||
} elseif ($encoding === null) {
|
||||
// Not valid UTF-8 and no converter available: show it, but never write it back.
|
||||
$content = unav_lossy_utf8($content);
|
||||
$payload['editable'] = false;
|
||||
}
|
||||
$payload['content'] = $content;
|
||||
}
|
||||
|
||||
unav_json($payload);
|
||||
@@ -139,14 +161,39 @@ function unav_write_all($handle, string $content): bool {
|
||||
|
||||
function unav_preview_type(string $extension, string $name): ?string {
|
||||
$base = strtolower($name);
|
||||
if (in_array($base, ['dockerfile', 'makefile'], true)) {
|
||||
$textNames = ['dockerfile', 'containerfile', 'makefile', 'gnumakefile', 'vagrantfile', 'gemfile', 'rakefile', 'procfile',
|
||||
'jenkinsfile', 'caddyfile', 'brewfile', 'go', 'hosts', 'fstab', 'crontab', 'exports', 'passwd', 'group', 'shells',
|
||||
'authorized_keys', 'known_hosts', 'config', 'license', 'licence', 'readme', 'changelog', 'authors', 'copying',
|
||||
'notice', 'todo', 'version', 'codeowners'];
|
||||
if (in_array($base, $textNames, 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'];
|
||||
$text = [
|
||||
// plain text, logs, docs
|
||||
'txt', 'text', 'log', 'out', 'err', 'nfo', 'diz', 'rd', 'rst', 'adoc', 'asciidoc', 'org', 'tex', 'bib', 'rtf',
|
||||
// configuration
|
||||
'cfg', 'conf', 'config', 'cnf', 'cf', 'ini', 'inf', 'env', 'properties', 'prefs', 'toml', 'yaml', 'yml',
|
||||
'json', 'jsonc', 'json5', 'ndjson', 'jsonl', 'geojson', 'xml', 'xsd', 'xsl', 'xslt', 'plist', 'hcl', 'tf', 'tfvars',
|
||||
'nix', 'rc', 'list', 'lst', 'tmpl', 'tpl', 'j2', 'jinja', 'jinja2', 'twig', 'mustache', 'hbs', 'lock', 'sum', 'mod',
|
||||
'editorconfig', 'gitignore', 'gitattributes', 'gitmodules', 'dockerignore', 'npmrc', 'yarnrc', 'nvmrc', 'htaccess',
|
||||
'htpasswd', 'bashrc', 'bash_profile', 'profile', 'zshrc', 'vimrc', 'tmux', 'screenrc', 'inputrc', 'wgetrc', 'curlrc',
|
||||
// Unraid / Linux system files
|
||||
'plg', 'page', 'cron', 'crontab', 'service', 'timer', 'socket', 'target', 'mount', 'automount', 'path', 'network',
|
||||
'netdev', 'link', 'rules', 'desktop', 'repo', 'sources', 'exports', 'fstab', 'hosts', 'pub', 'pem', 'csr', 'asc',
|
||||
'md5', 'sha1', 'sha256', 'sha512', 'sfv', 'cue', 'm3u', 'm3u8', 'pls', 'xspf', 'opml',
|
||||
'ics', 'vcf', 'srt', 'sub', 'vtt', 'ass', 'ssa', 'nzb',
|
||||
// scripts and source code
|
||||
'sh', 'bash', 'zsh', 'fish', 'ksh', 'csh', 'awk', 'sed', 'php', 'phtml', 'js', 'mjs', 'cjs', 'ts', 'tsx', 'jsx',
|
||||
'vue', 'svelte', 'css', 'scss', 'sass', 'less', 'html', 'htm', 'xhtml', 'py', 'pyw', 'rb', 'pl', 'pm', 'lua', 'tcl',
|
||||
'go', 'rs', 'java', 'kt', 'kts', 'scala', 'groovy', 'gradle', 'swift', 'c', 'cc', 'cpp', 'cxx', 'h', 'hh', 'hpp',
|
||||
'cs', 'vb', 'fs', 'dart', 'r', 'jl', 'ex', 'exs', 'erl', 'hs', 'ml', 'clj', 'lisp', 'el', 'vim', 'sql', 'graphql',
|
||||
'gql', 'proto', 'bat', 'cmd', 'ps1', 'psm1', 'psd1', 'vbs', 'ahk', 'cmake', 'mk', 'mak', 'make', 'in', 'am', 'ac',
|
||||
'm4', 'spec', 'diff', 'patch', 'dockerfile', 'containerfile', 'compose',
|
||||
];
|
||||
if (in_array($extension, $text, true)) {
|
||||
return 'text';
|
||||
}
|
||||
if ($extension === 'md') {
|
||||
if (in_array($extension, ['md', 'markdown', 'mdown', 'mkd', 'mdx'], true)) {
|
||||
return 'markdown';
|
||||
}
|
||||
if (in_array($extension, ['csv', 'tsv'], true)) {
|
||||
@@ -191,3 +238,51 @@ function unav_preview_mime(string $extension, string $type): string {
|
||||
];
|
||||
return $map[$extension] ?? ($type === 'text' ? 'text/plain; charset=utf-8' : 'application/octet-stream');
|
||||
}
|
||||
|
||||
// First 8 KB without NUL bytes and mostly printable/UTF-8 → treat as text.
|
||||
function unav_looks_like_text(string $path): bool {
|
||||
$size = @filesize($path);
|
||||
if ($size === false || $size > UNAV_TEXT_PREVIEW_MAX_BYTES) {
|
||||
return false;
|
||||
}
|
||||
if ($size === 0) {
|
||||
return true;
|
||||
}
|
||||
$sample = (string)@file_get_contents($path, false, null, 0, 8192);
|
||||
if ($sample === '' || str_contains($sample, "\0")) {
|
||||
return false;
|
||||
}
|
||||
if (preg_match('//u', $sample) || preg_match('//u', substr($sample, 0, -3))) {
|
||||
return true;
|
||||
}
|
||||
$control = preg_match_all('/[\x01-\x08\x0E-\x1F\x7F]/', $sample);
|
||||
return $control / strlen($sample) < 0.02;
|
||||
}
|
||||
|
||||
function unav_text_encoding(string $bytes): ?string {
|
||||
if (preg_match('//u', $bytes)) {
|
||||
return 'utf-8';
|
||||
}
|
||||
return (function_exists('mb_convert_encoding') || function_exists('iconv')) ? 'windows-1252' : null;
|
||||
}
|
||||
|
||||
function unav_decode_cp1252(string $bytes): string {
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
return mb_convert_encoding($bytes, 'UTF-8', 'Windows-1252');
|
||||
}
|
||||
return (string)iconv('Windows-1252', 'UTF-8//IGNORE', $bytes);
|
||||
}
|
||||
|
||||
function unav_encode_cp1252(string $text): ?string {
|
||||
$bytes = function_exists('mb_convert_encoding')
|
||||
? mb_convert_encoding($text, 'Windows-1252', 'UTF-8')
|
||||
: @iconv('UTF-8', 'Windows-1252', $text);
|
||||
if (!is_string($bytes) || unav_decode_cp1252($bytes) !== $text) {
|
||||
return null;
|
||||
}
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
function unav_lossy_utf8(string $bytes): string {
|
||||
return (string)preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '?', $bytes);
|
||||
}
|
||||
Reference in new issue
Block a user