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; return NULL;
} }
static int compare_content_types(const void* lhs, const void* rhs) {
return g_strcmp0(lhs, rhs);
}
char* char*
zathura_content_type_guess(zathura_content_type_context_t* context, 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 */ /* try libmagic first */
char* content_type = guess_type_magic(context, path); char *content_type = guess_type_magic(context, path);
if (content_type != NULL) { 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 */ /* else fallback to g_content_type_guess method */
content_type = guess_type_glib(path); content_type = guess_type_glib(path);
if (content_type != NULL) { 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 */ /* and if libmagic is not available, try file as last resort */
return guess_type_file(path); 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. * @return content type of path, needs to freeed with g_free.
*/ */
char* zathura_content_type_guess(zathura_content_type_context_t* context, 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 #endif

View file

@ -90,7 +90,7 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri,
goto error_free; 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) { if (content_type == NULL) {
girara_error("Could not determine file type."); girara_error("Could not determine file type.");
check_set_error(error, ZATHURA_ERROR_UNKNOWN); 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); plugin = zathura_plugin_manager_get_plugin(zathura->plugins.manager, content_type);
if (plugin == NULL) { if (plugin == NULL) {
girara_error("Unknown file type: '%s'", content_type); girara_error("Unknown file type: '%s'", content_type);
check_set_error(error, ZATHURA_ERROR_UNKNOWN); check_set_error(error, ZATHURA_ERROR_UNKNOWN);

View file

@ -52,7 +52,7 @@ file_valid_extension(zathura_t* zathura, const char* path)
return false; 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) { if (content_type == NULL) {
return false; return false;
} }