предпросмотр и скрипт для сборки бинарей

This commit is contained in:
2025-11-21 02:27:55 +08:00
parent 4159637b83
commit 0fdfb09fa0
4 changed files with 464 additions and 31 deletions

81
index.c
View File

@@ -1,5 +1,4 @@
// /home/romkazvo/www/cgi-bin/index.c
// Основной файловый сервер
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,6 +22,7 @@ typedef struct {
char file_url[1024];
} entry_t;
// Быстрый буферизированный вывод
typedef struct {
char *data;
size_t size;
@@ -90,6 +90,47 @@ const char* get_file_icon(const char* filename) {
return "📄";
}
// Проверяет, можно ли открыть файл в браузере
int is_previewable(const char* filename) {
const char *ext = strrchr(filename, '.');
if (!ext) return 0;
ext++;
// Изображения
if (strcasecmp(ext, "jpg") == 0 || strcasecmp(ext, "jpeg") == 0 ||
strcasecmp(ext, "png") == 0 || strcasecmp(ext, "gif") == 0 ||
strcasecmp(ext, "webp") == 0 || strcasecmp(ext, "bmp") == 0 ||
strcasecmp(ext, "svg") == 0 || strcasecmp(ext, "ico") == 0)
return 1;
// Текстовые файлы
if (strcasecmp(ext, "txt") == 0 || strcasecmp(ext, "md") == 0 ||
strcasecmp(ext, "html") == 0 || strcasecmp(ext, "htm") == 0 ||
strcasecmp(ext, "css") == 0 || strcasecmp(ext, "js") == 0 ||
strcasecmp(ext, "json") == 0 || strcasecmp(ext, "xml") == 0 ||
strcasecmp(ext, "csv") == 0)
return 1;
// PDF
if (strcasecmp(ext, "pdf") == 0)
return 1;
// Аудио
if (strcasecmp(ext, "mp3") == 0 || strcasecmp(ext, "wav") == 0 ||
strcasecmp(ext, "ogg") == 0 || strcasecmp(ext, "flac") == 0 ||
strcasecmp(ext, "m4a") == 0 || strcasecmp(ext, "aac") == 0)
return 1;
// Видео
if (strcasecmp(ext, "mp4") == 0 || strcasecmp(ext, "webm") == 0 ||
strcasecmp(ext, "ogv") == 0 || strcasecmp(ext, "mov") == 0 ||
strcasecmp(ext, "avi") == 0 || strcasecmp(ext, "mkv") == 0)
return 1;
return 0;
}
int compare_entries(const void *a, const void *b) {
const entry_t *entryA = (const entry_t *)a;
const entry_t *entryB = (const entry_t *)b;
@@ -251,6 +292,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
}
}
// Предварительная обработка записей
while ((entry = readdir(dir)) != NULL && entry_count < MAX_ENTRIES) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
@@ -268,6 +310,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
entries[entry_count].size = 0;
strcpy(entries[entry_count].icon, "📁");
// Предварительно кодируем путь для папок
char new_path[2048];
snprintf(new_path, sizeof(new_path), "%s%s%s", display_path,
display_path[0] ? "/" : "", entries[entry_count].name);
@@ -279,6 +322,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
entries[entry_count].size = file_stat.st_size;
strcpy(entries[entry_count].icon, get_file_icon(entry->d_name));
// Предварительно формируем URL для файлов
snprintf(entries[entry_count].file_url, sizeof(entries[0].file_url),
"%s%s%s", display_path, display_path[0] ? "/" : "", entries[entry_count].name);
@@ -305,6 +349,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
buffer_append(buf, "<ul>\n");
// Вывод папок с предварительно закодированными путями
if (dir_count > 0) {
char section_title[128];
snprintf(section_title, sizeof(section_title), " <li class=\"section-title\">📁 Папки (%d)</li>\n", dir_count);
@@ -330,6 +375,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
}
}
// Вывод файлов с предварительно сформированными URL
if (file_count > 0) {
char section_title[128];
snprintf(section_title, sizeof(section_title), " <li class=\"section-title\">📄 Файлы (%d)</li>\n", file_count);
@@ -341,21 +387,34 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
format_size(entries[i].size, size_str);
buffer_append(buf, " <li>\n");
char file_link[512];
snprintf(file_link, sizeof(file_link),
" <a class=\"file-link\" href=\"/%s\" download>\n",
entries[i].file_url);
buffer_append(buf, file_link);
buffer_append(buf, " <span class=\"file-icon\">");
buffer_append(buf, " <div class=\"file-row\">\n");
// Основная ссылка для скачивания
buffer_append(buf, " <a class=\"file-link\" href=\"/");
buffer_append(buf, entries[i].file_url);
buffer_append(buf, "\" download>\n");
buffer_append(buf, " <span class=\"file-icon\">");
buffer_append(buf, entries[i].icon);
buffer_append(buf, "</span>\n");
buffer_append(buf, " <span class=\"file-name\">");
buffer_append(buf, " <span class=\"file-name\">");
buffer_append(buf, entries[i].name);
buffer_append(buf, "</span>\n");
buffer_append(buf, " </a>\n");
buffer_append(buf, " <span class=\"size\">");
buffer_append(buf, " </a>\n");
buffer_append(buf, " <div class=\"file-controls\">\n");
// Кнопка предпросмотра (если поддерживается)
if (is_previewable(entries[i].name)) {
buffer_append(buf, " <button class=\"preview-btn\" onclick=\"openPreview('/");
buffer_append(buf, entries[i].file_url);
buffer_append(buf, "')\" title=\"Открыть в браузере\">👁</button>\n");
}
buffer_append(buf, " <span class=\"size\">");
buffer_append(buf, size_str);
buffer_append(buf, "</span>\n");
buffer_append(buf, " </div>\n");
buffer_append(buf, " </div>\n");
buffer_append(buf, " </li>\n");
}
}
@@ -363,6 +422,7 @@ void print_content(buffer_t *buf, const char *base_path, const char *display_pat
buffer_append(buf, "</ul>\n");
// Статистика
char total_size_str[32];
format_size(total_size, total_size_str);
char stats[256];
@@ -394,6 +454,7 @@ void print_template(const char *base_path, const char *display_path) {
}
fclose(file);
// Один быстрый вывод вместо множества printf
fwrite(output.data, 1, output.size, stdout);
buffer_free(&output);
}