Re-try with different methods if content type is not supported

This commit is contained in:
Sebastian Ramacher 2019-11-30 11:36:11 +01:00
parent c998ad32b1
commit 43144b23e8
4 changed files with 25 additions and 8 deletions

View File

@ -213,19 +213,36 @@ guess_type_glib(const char* path)
return NULL;
}
static int compare_content_types(const void* lhs, const void* rhs) {
return g_strcmp0(lhs, rhs);
}
char*
zathura_content_type_guess(zathura_content_type_context_t* context,
const char* path)
const char* path,
const girara_list_t* supported_content_types)
{
/* try libmagic first */
char* content_type = guess_type_magic(context, path);
char *content_type = guess_type_magic(context, path);
if (content_type != NULL) {
return content_type;
if (supported_content_types == NULL ||
girara_list_find(supported_content_types, compare_content_types,
content_type) != NULL) {
return content_type;
}
girara_debug("content type '%s' not supported, trying again", content_type);
g_free(content_type);
}
/* else fallback to g_content_type_guess method */
content_type = guess_type_glib(path);
if (content_type != NULL) {
return content_type;
if (supported_content_types == NULL ||
girara_list_find(supported_content_types, compare_content_types,
content_type) != NULL) {
return content_type;
}
girara_debug("content type '%s' not supported, trying again", content_type);
g_free(content_type);
}
/* and if libmagic is not available, try file as last resort */
return guess_type_file(path);

View File

@ -27,6 +27,7 @@ void zathura_content_type_free(zathura_content_type_context_t* context);
* @return content type of path, needs to freeed with g_free.
*/
char* zathura_content_type_guess(zathura_content_type_context_t* context,
const char* path);
const char* path,
const girara_list_t* supported_content_types);
#endif

View File

@ -90,7 +90,7 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri,
goto error_free;
}
content_type = zathura_content_type_guess(zathura->content_type_context, real_path);
content_type = zathura_content_type_guess(zathura->content_type_context, real_path, zathura_plugin_manager_get_content_types(zathura->plugins.manager));
if (content_type == NULL) {
girara_error("Could not determine file type.");
check_set_error(error, ZATHURA_ERROR_UNKNOWN);
@ -98,7 +98,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri,
}
plugin = zathura_plugin_manager_get_plugin(zathura->plugins.manager, content_type);
if (plugin == NULL) {
girara_error("Unknown file type: '%s'", content_type);
check_set_error(error, ZATHURA_ERROR_UNKNOWN);

View File

@ -52,7 +52,7 @@ file_valid_extension(zathura_t* zathura, const char* path)
return false;
}
char* content_type = zathura_content_type_guess(zathura->content_type_context, path);
char* content_type = zathura_content_type_guess(zathura->content_type_context, path, NULL);
if (content_type == NULL) {
return false;
}