simplify static download

This commit is contained in:
2026-02-03 11:51:04 +02:00
parent 800689021c
commit 44dda3252f
3 changed files with 13 additions and 239 deletions

192
index.php
View File

@@ -7,185 +7,6 @@ require_once __DIR__ . '/functions.php';
if (!file_exists(UPLOAD_DIR)) mkdir(UPLOAD_DIR, 0755, true);
if (!file_exists(CHUNK_DIR)) mkdir(CHUNK_DIR, 0755, true);
// Обработка запросов файлов напрямую из корня
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$requestedFile = ltrim($requestUri, '/');
// Если запрашивается файл (не административный интерфейс)
if (
!empty($requestedFile) &&
$requestedFile !== 'index.php' &&
$requestedFile !== 'upload_chunk.php' &&
$requestedFile !== 'merge_chunks.php' &&
!isset($_GET['action']) &&
!isset($_GET['delete']) &&
!isset($_POST['password'])
) {
$requestedFile = rawurldecode($requestedFile);
$filename = basename($requestedFile);
$filepath = UPLOAD_DIR . $filename;
if (file_exists($filepath)) {
// Определение MIME типа
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$mimeTypes = [
// Изображения
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
'bmp' => 'image/bmp',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'ico' => 'image/x-icon',
'heic' => 'image/heic',
'heif' => 'image/heif',
'avif' => 'image/avif',
// Документы
'pdf' => 'application/pdf',
'txt' => 'text/plain',
'rtf' => 'application/rtf',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'ppt' => 'application/vnd.ms-powerpoint',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'epub' => 'application/epub+zip',
'mobi' => 'application/x-mobipocket-ebook',
// Архивы
'zip' => 'application/zip',
'rar' => 'application/vnd.rar',
'7z' => 'application/x-7z-compressed',
'tar' => 'application/x-tar',
'gz' => 'application/gzip',
'bz2' => 'application/x-bzip2',
'xz' => 'application/x-xz',
'iso' => 'application/x-iso9660-image',
// Видео
'mp4' => 'video/mp4',
'avi' => 'video/x-msvideo',
'mkv' => 'video/x-matroska',
'mov' => 'video/quicktime',
'webm' => 'video/webm',
'flv' => 'video/x-flv',
'wmv' => 'video/x-ms-wmv',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'm4v' => 'video/x-m4v',
'3gp' => 'video/3gpp',
'ogv' => 'video/ogg',
// Аудио
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'flac' => 'audio/flac',
'ogg' => 'audio/ogg',
'aac' => 'audio/aac',
'm4a' => 'audio/mp4',
'wma' => 'audio/x-ms-wma',
'opus' => 'audio/opus',
'aiff' => 'audio/aiff',
// Данные
'json' => 'application/json',
'xml' => 'application/xml',
'csv' => 'text/csv',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'yaml' => 'text/yaml',
'yml' => 'text/yaml',
'toml' => 'text/toml',
// Код
'html' => 'text/html',
'htm' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript',
'php' => 'text/x-php',
'py' => 'text/x-python',
'java' => 'text/x-java-source',
'cpp' => 'text/x-c++src',
'c' => 'text/x-csrc',
'h' => 'text/x-chdr',
'cs' => 'text/x-csharp',
'rb' => 'text/x-ruby',
'go' => 'text/x-go',
'rs' => 'text/x-rust',
'sql' => 'text/x-sql',
'md' => 'text/markdown',
'log' => 'text/plain',
'sh' => 'text/x-shellscript',
'bat' => 'text/x-msdos-batch',
// Веб и CMS
'wpress' => 'application/octet-stream',
'backup' => 'application/octet-stream',
'bak' => 'application/octet-stream',
'db' => 'application/x-sqlite3',
'sqlite' => 'application/x-sqlite3',
'sqlite3' => 'application/x-sqlite3',
// Шрифты
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'eot' => 'application/vnd.ms-fontobject',
// Дизайн
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'indd' => 'application/x-indesign',
// CAD
'dwg' => 'image/vnd.dwg',
'dxf' => 'image/vnd.dxf',
'obj' => 'model/obj',
'stl' => 'model/stl',
// Мобильные приложения и исполняемые файлы
'apk' => 'application/vnd.android.package-archive',
'ipa' => 'application/octet-stream',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msi',
'deb' => 'application/vnd.debian.binary-package',
'rpm' => 'application/x-rpm',
'dmg' => 'application/x-apple-diskimage',
'pkg' => 'application/x-newton-compatible-pkg',
// Прочие
'torrent' => 'application/x-bittorrent',
'ics' => 'text/calendar',
'vcf' => 'text/vcard',
'gpx' => 'application/gpx+xml',
'kml' => 'application/vnd.google-earth.kml+xml',
'kmz' => 'application/vnd.google-earth.kmz'
];
$mimeType = $mimeTypes[$extension] ?? 'application/octet-stream';
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($filepath));
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Cache-Control: public, max-age=31536000'); // Кеш на год
readfile($filepath);
exit;
} else {
http_response_code(404);
echo '404 - Файл не найден';
exit;
}
}
// Проверка авторизации
if (!isset($_SESSION['logged_in']) || ($_GET['action'] ?? '') === 'logout') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
@@ -314,7 +135,7 @@ $csrf_token = generateCSRFToken();
<?php foreach ($files as $file):
$filepath = UPLOAD_DIR . $file;
$filesize = file_exists($filepath) ? filesize($filepath) : 0;
$url = rawurlencode($file);
$url = 'upload/' . rawurlencode($file);
$delete_url = '?delete=' . urlencode($file) . '&csrf_token=' . urlencode($csrf_token);
?>
<li class="file-item">
@@ -338,6 +159,7 @@ $csrf_token = generateCSRFToken();
const MAX_FILE_SIZE = <?= getMaxFileSize() ?>;
const CHUNK_SIZE = <?= getChunkSize() ?>;
const ALLOWED_EXTENSIONS = <?= json_encode(ALLOWED_EXTENSIONS) ?>;
const ALLOW_ALL_EXTENSIONS = ALLOWED_EXTENSIONS.length === 0 || ALLOWED_EXTENSIONS.includes('*');
const CSRF_TOKEN = '<?= htmlspecialchars($csrf_token) ?>';
// Показать серверные сообщения при загрузке страницы
@@ -451,10 +273,12 @@ $csrf_token = generateCSRFToken();
}
// Проверка расширения
const extension = file.name.split('.').pop().toLowerCase();
if (!ALLOWED_EXTENSIONS.includes(extension)) {
showNotification(`Недопустимый тип файла. Разрешены: ${ALLOWED_EXTENSIONS.join(', ')}`, 'error');
return false;
if (!ALLOW_ALL_EXTENSIONS) {
const extension = file.name.split('.').pop().toLowerCase();
if (!ALLOWED_EXTENSIONS.includes(extension)) {
showNotification(`Недопустимый тип файла. Разрешены: ${ALLOWED_EXTENSIONS.join(', ')}`, 'error');
return false;
}
}
// Проверка имени файла