docs: def detect_encoding(fp, default=None):
      """Detect the cahracter encoding of a file.
  
      :param fp: Open Python file pointer.
      :param default: Fallback encoding to use.
      :returns: The detected encoding.
  
      .. note:: The file pointer is returned at its original read position.
      """
      init_pos = fp.tell()
      try:
          sample = fp.read(
              current_app.config.get('PREVIEWER_CHARDET_BYTES', 1024))
          # Result contains 'confidence' and 'encoding'
          result = cchardet.detect(sample)
          threshold = current_app.config.get('PREVIEWER_CHARDET_CONFIDENCE', 0.9)
          if result.get('confidence', 0) > threshold:
              return result.get('encoding', default)
          else:
              return default
      except Exception:
          current_app.logger.warning('Encoding detection failed.', exc_info=True)
          return default
      finally:
          fp.seek(init_pos)