mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2024-12-29 13:56:00 +01:00
Split guess_type methods into separate functions
Also don't try to run file if we have libmagic available. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
parent
250c575f44
commit
ecf090b9b1
1 changed files with 37 additions and 12 deletions
49
document.c
49
document.c
|
@ -665,11 +665,9 @@ zathura_document_get_information(zathura_document_t* document, zathura_error_t*
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const gchar*
|
|
||||||
guess_type(const char* path)
|
|
||||||
{
|
|
||||||
const gchar* content_type = NULL;
|
|
||||||
#ifdef WITH_MAGIC
|
#ifdef WITH_MAGIC
|
||||||
|
static const char*
|
||||||
|
guess_type_magic(const char* path) {
|
||||||
const char* mime_type = NULL;
|
const char* mime_type = NULL;
|
||||||
|
|
||||||
/* creat magic cookie */
|
/* creat magic cookie */
|
||||||
|
@ -698,22 +696,30 @@ guess_type(const char* path)
|
||||||
girara_debug("failed guessing filetype: %s", magic_error(magic));
|
girara_debug("failed guessing filetype: %s", magic_error(magic));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
/* dup so we own the memory */
|
||||||
|
mime_type = g_strdup(mime_type);
|
||||||
|
|
||||||
girara_debug("magic detected filetype: %s", mime_type);
|
girara_debug("magic detected filetype: %s", mime_type);
|
||||||
content_type = g_strdup(mime_type);
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (magic != NULL) {
|
if (magic != NULL) {
|
||||||
magic_close(magic);
|
magic_close(magic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content_type != NULL) {
|
return mime_type;
|
||||||
return content_type;
|
}
|
||||||
}
|
#else
|
||||||
/* else fallback to g_content_type_guess method */
|
static const char*
|
||||||
#endif /*WITH_MAGIC*/
|
guess_type_magic(const char* UNUSED(path)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char*
|
||||||
|
guess_type_glib(const char* path)
|
||||||
|
{
|
||||||
gboolean uncertain = FALSE;
|
gboolean uncertain = FALSE;
|
||||||
content_type = g_content_type_guess(path, NULL, 0, &uncertain);
|
const char* content_type = g_content_type_guess(path, NULL, 0, &uncertain);
|
||||||
if (content_type == NULL) {
|
if (content_type == NULL) {
|
||||||
girara_debug("g_content_type failed\n");
|
girara_debug("g_content_type failed\n");
|
||||||
} else {
|
} else {
|
||||||
|
@ -755,8 +761,26 @@ cleanup:
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free((void*)content_type);
|
g_free((void*)content_type);
|
||||||
content_type = NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char*
|
||||||
|
guess_type(const char* path)
|
||||||
|
{
|
||||||
|
/* try libmagic first */
|
||||||
|
const char* content_type = guess_type_magic(path);
|
||||||
|
if (content_type != NULL) {
|
||||||
|
return content_type;
|
||||||
|
}
|
||||||
|
/* else fallback to g_content_type_guess method */
|
||||||
|
content_type = guess_type_glib(path);
|
||||||
|
if (content_type != NULL) {
|
||||||
|
return content_type;
|
||||||
|
}
|
||||||
|
#ifdef WITH_MAGIC
|
||||||
|
return NULL;
|
||||||
|
#else
|
||||||
|
/* and if libmagic is not available, try file as last resort */
|
||||||
girara_debug("falling back to file");
|
girara_debug("falling back to file");
|
||||||
|
|
||||||
GString* command = g_string_new("file -b --mime-type ");
|
GString* command = g_string_new("file -b --mime-type ");
|
||||||
|
@ -784,6 +808,7 @@ cleanup:
|
||||||
|
|
||||||
g_strdelimit(out, "\n\r", '\0');
|
g_strdelimit(out, "\n\r", '\0');
|
||||||
return out;
|
return out;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
zathura_plugin_t*
|
zathura_plugin_t*
|
||||||
|
|
Loading…
Reference in a new issue