From c8524b95bcc6a478ccf4d6d44a9bd88d1152489a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 12 Sep 2024 17:39:49 +0400 Subject: [PATCH] Protection against adding resources of new types Now if a static resource of a new type is added the build will fail unless the list of known file type extensions is updated. --- scripts/kiwix-compile-resources | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/scripts/kiwix-compile-resources b/scripts/kiwix-compile-resources index 7cf90733..998a4abe 100755 --- a/scripts/kiwix-compile-resources +++ b/scripts/kiwix-compile-resources @@ -61,11 +61,31 @@ resource_decl_template = """{namespaces_open} extern const std::string {identifier}; {namespaces_close}""" -BINARY_RESOURCE_EXTENSIONS = ('.png', '.ttf', '.ico') +BINARY_RESOURCE_EXTENSIONS = {'.ico', '.png', '.ttf'} + +TEXT_RESOURCE_EXTENSIONS = { + '.css', + '.html', + '.js', + '.json', + '.svg', + '.tmpl', + '.webmanifest', + '.xml', +} + +if not BINARY_RESOURCE_EXTENSIONS.isdisjoint(TEXT_RESOURCE_EXTENSIONS): + raise RuntimeError(f"The following file type extensions are declared to be both binary and text: {BINARY_RESOURCE_EXTENSIONS.intersection(TEXT_RESOURCE_EXTENSIONS)}") def is_binary_resource(filename): _, extension = os.path.splitext(filename) - return extension in BINARY_RESOURCE_EXTENSIONS + is_binary = extension in BINARY_RESOURCE_EXTENSIONS + is_text = extension in TEXT_RESOURCE_EXTENSIONS + if not is_binary and not is_text: + # all file type extensions of static resources must be listed + # in either BINARY_RESOURCE_EXTENSIONS or TEXT_RESOURCE_EXTENSIONS + raise RuntimeError(f"Unknown file type extension: {extension}") + return is_binary class Resource: def __init__(self, base_dirs, filename, cacheid=None):