Completely hardcode mimetypes, improve mimetype search query.

master
voussoir 2022-10-27 20:53:20 -07:00
parent dcaff7fb11
commit 35c29e6778
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
4 changed files with 77 additions and 33 deletions

View File

@ -215,24 +215,76 @@ FILENAME_BADCHARS = '\\/:*?<>|"'
USER_ID_CHARACTERS = string.digits + string.ascii_uppercase USER_ID_CHARACTERS = string.digits + string.ascii_uppercase
ADDITIONAL_MIMETYPES = { MIMETYPES = {
'7z': 'archive', '7z': ('archive', '7z'),
'gz': 'archive', 'gz': ('archive', 'gz'),
'rar': 'archive', 'rar': ('archive', 'rar'),
'tar': ('archive', 'tar'),
'zip': ('archive', 'zip'),
'aac': 'audio/aac', 'aac': ('audio', 'aac'),
'ac3': 'audio/ac3', 'ac3': ('audio', 'ac3'),
'dts': 'audio/dts', 'aif': ('audio', 'x-aiff'),
'm4a': 'audio/mp4', 'aifc': ('audio', 'x-aiff'),
'opus': 'audio/ogg', 'aiff': ('audio', 'x-aiff'),
'au': ('audio', 'basic'),
'dts': ('audio', 'dts'),
'm4a': ('audio', 'mp4'),
'mp2': ('audio', 'mpeg'),
'mp3': ('audio', 'mpeg'),
'opus': ('audio', 'ogg'),
'snd': ('audio', 'basic'),
'wav': ('audio', 'x-wav'),
'mkv': 'video/x-matroska', 'bmp': ('image', 'x-ms-bmp'),
'gif': ('image', 'gif'),
'ico': ('image', 'vnd.microsoft.icon'),
'ief': ('image', 'ief'),
'jpe': ('image', 'jpeg'),
'jpeg': ('image', 'jpeg'),
'jpg': ('image', 'jpeg'),
'png': ('image', 'png'),
'svg': ('image', 'svg+xml'),
'tif': ('image', 'tiff'),
'tiff': ('image', 'tiff'),
'ass': 'text/plain', 'ass': ('text', 'plain'),
'md': 'text/plain', 'bat': ('text', 'plain'),
'nfo': 'text/plain', 'c': ('text', 'plain'),
'rst': 'text/plain', 'css': ('text', 'css'),
'srt': 'text/plain', 'csv': ('text', 'csv'),
'etx': ('text', 'x-setext'),
'h': ('text', 'plain'),
'htm': ('text', 'html'),
'html': ('text', 'html'),
'js': ('text', 'javascript'),
'json': ('text', 'json'),
'ksh': ('text', 'plain'),
'md': ('text', 'plain'),
'nfo': ('text', 'plain'),
'pl': ('text', 'plain'),
'py': ('text', 'x-python'),
'rst': ('text', 'plain'),
'rtx': ('text', 'richtext'),
'sgm': ('text', 'x-sgml'),
'sgml': ('text', 'x-sgml'),
'srt': ('text', 'plain'),
'tsv': ('text', 'tab-separated-values'),
'txt': ('text', 'plain'),
'vcf': ('text', 'x-vcard'),
'xml': ('text', 'xml'),
'avi': ('video', 'x-msvideo'),
'm1v': ('video', 'mpeg'),
'mkv': ('video', 'x-matroska'),
'mov': ('video', 'quicktime'),
'mp4': ('video', 'mp4'),
'mpa': ('video', 'mpeg'),
'mpe': ('video', 'mpeg'),
'mpeg': ('video', 'mpeg'),
'mpg': ('video', 'mpeg'),
'qt': ('video', 'quicktime'),
'webm': ('video', 'webm'),
} }
# Photodb ########################################################################################## # Photodb ##########################################################################################

View File

@ -5,7 +5,6 @@ codebase but don't deserve to be methods of any class.
import bs4 import bs4
import datetime import datetime
import hashlib import hashlib
import mimetypes
import os import os
import re import re
import PIL.Image import PIL.Image
@ -286,16 +285,9 @@ def generate_video_thumbnail(filepath, outfile, width, height, **special) -> PIL
) )
return True return True
def get_mimetype(filepath) -> typing.Optional[str]: def get_mimetype(extension) -> typing.Optional[str]:
''' extension = extension.strip('.')
Extension to mimetypes.guess_type which uses my return constants.MIMETYPES.get(extension, None)
constants.ADDITIONAL_MIMETYPES.
'''
extension = os.path.splitext(filepath)[1].replace('.', '')
mimetype = constants.ADDITIONAL_MIMETYPES.get(extension, None)
if mimetype is None:
mimetype = mimetypes.guess_type(filepath)[0]
return mimetype
def hash_photoset(photos) -> str: def hash_photoset(photos) -> str:
''' '''

View File

@ -926,12 +926,14 @@ class Photo(ObjectBase):
# self._mimetype vars to help memoize, which needs to be None-capable. # self._mimetype vars to help memoize, which needs to be None-capable.
# So although I normally like using @property, this is less lines of # So although I normally like using @property, this is less lines of
# code and less indirection really. # code and less indirection really.
self.mimetype = helpers.get_mimetype(self.real_path.basename) mime = helpers.get_mimetype(self.real_path.extension.no_dot)
if self.mimetype is None: if mime is None:
self.simple_mimetype = None self.simple_mimetype = None
self.mimetype = None
else: else:
self.simple_mimetype = self.mimetype.split('/')[0] self.simple_mimetype = mime[0]
self.mimetype = '/'.join(mime)
def _uncache(self): def _uncache(self):
self.photodb.caches[Photo].remove(self.id) self.photodb.caches[Photo].remove(self.id)

View File

@ -725,7 +725,8 @@ class PDBPhotoMixin:
bindings.extend(extension_not) bindings.extend(extension_not)
if mimetype: if mimetype:
notnulls.add('extension') extensions = {extension for (extension, (typ, subtyp)) in constants.MIMETYPES.items() if typ in mimetype}
wheres.append(f'extension IN {sqlhelpers.listify(extensions)} COLLATE NOCASE')
if within_directory: if within_directory:
patterns = {d.absolute_path.rstrip(os.sep) for d in within_directory} patterns = {d.absolute_path.rstrip(os.sep) for d in within_directory}
@ -808,9 +809,6 @@ class PDBPhotoMixin:
for row in generator: for row in generator:
photo = self.get_cached_instance(objects.Photo, row) photo = self.get_cached_instance(objects.Photo, row)
if mimetype and photo.simple_mimetype not in mimetype:
continue
if filename_tree and not filename_tree.evaluate(photo.basename.lower()): if filename_tree and not filename_tree.evaluate(photo.basename.lower()):
continue continue