Skip to content
Snippets Groups Projects
odt_format.py 874 B
Newer Older
  • Learn to ignore specific revisions
  • import zipfile
    
    
    def is_valid(resource, file):
        """Check if the content is a ODT file."""
    
        if not zipfile.is_zipfile(file.name):
            resource["error"] = "Not a ZIP file."
            return False
    
        with zipfile.ZipFile(file.name, "r") as zip_ref:
            zip_contents = zip_ref.namelist()
    
            required_files = ["mimetype", "content.xml", "meta.xml", "styles.xml"]
    
            if not all(file in zip_contents for file in required_files):
                resource["error"] = "That does not look like an ODT file."
                return False
    
            with zip_ref.open("mimetype") as mimetype_file:
                mimetype_content = mimetype_file.read().decode("utf-8").strip()
    
            if mimetype_content != "application/vnd.oasis.opendocument.text":
                resource["error"] = f"Incorrect MIME type: {mimetype_content}"
                return False
    
            return True