Files
FolderOpdsServer/src/get_handler.c
2026-06-17 17:34:07 +02:00

32 lines
1.1 KiB
C

static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url, Settings settings, SV username) {
noh_arena_save(&temp);
Noh_String fs_path = build_path(false, false, &settings.base_path, &username, &url);
url_decode(&fs_path);
noh_log(NOH_INFO, "Physical path: "Nsv_Fmt, Nsv_Arg(fs_path));
if (!noh_sv_ends_with(noh_sv_from_string(fs_path), sv(".epub"))) {
return send_response(connection, 400, sv("File is not an epub file."), false);
}
noh_string_append_null(&fs_path);
// Try to open the file.
FILE *file = fopen(fs_path.elems, "r");
if (file == NULL) {
if (errno == ENOENT) {
noh_arena_reset(&temp);
return send_response(connection, 404, sv("File does not exist."), false);
} else {
noh_log(NOH_ERROR, "Opening file failed.");
noh_arena_reset(&temp);
return send_response(connection, 500, sv(""), false);
}
}
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
return send_epub_file(connection, fileno(file), file_size);
}