code: async def _download_document(
              self, document, file, date, thumb, progress_callback):
          
          if isinstance(document, types.MessageMediaDocument):
              document = document.document
          if not isinstance(document, types.Document):
              return
  
          kind, possible_names = self._get_kind_and_names(document.attributes)
          file = self._get_proper_filename(
              file, kind, utils.get_extension(document),
              date=date, possible_names=possible_names
          )
  
          if thumb is None:
              size = None
          else:
              size = self._get_thumb(document.thumbs, thumb)
              if isinstance(size, (types.PhotoCachedSize, types.PhotoStrippedSize)):
                  return self._download_cached_photo_size(size, file)
  
          result = await self.download_file(
              types.InputDocumentFileLocation(
                  id=document.id,
                  access_hash=document.access_hash,
                  file_reference=document.file_reference,
                  thumb_size=size.type if size else ''
              ),
              file,
              file_size=size.size if size else document.size,
              progress_callback=progress_callback
          )
  
          return result if file is bytes else file
docs: Specialized version of .download_media() for documents.