file_utilities.py 813 B

123456789101112131415161718192021222324252627282930
  1. import hashlib
  2. import mimetypes
  3. MEDIA_EXTS_CACHE = []
  4. def media_exts():
  5. """ List of media file extensions from local system mimetypes """
  6. if not MEDIA_EXTS_CACHE:
  7. mimetypes.init()
  8. for ext in mimetypes.types_map:
  9. if mimetypes.types_map[ext].split('/')[0] in ('audio', 'video'):
  10. MEDIA_EXTS_CACHE.append(ext)
  11. return MEDIA_EXTS_CACHE
  12. def is_media_file_ext(ext):
  13. """ Is the given extension a media file extension according to the local system mimetypes """
  14. return ext.lower().lstrip('.') in [e.lower().lstrip('.') for e in media_exts()]
  15. HASHER = hashlib.md5()
  16. def hash_file(filename):
  17. """ return a hash for the given file """
  18. with open(filename, 'rb') as f:
  19. buf = f.read()
  20. HASHER.update(buf)
  21. return HASHER.hexdigest()