docs: def get_export_files(cursor, id, version, types, exports_dirs, read_file=True):
      """Retrieve files associated with document."""
      request = get_current_request()
      type_info = dict(request.registry.settings['_type_info'])
      metadata = get_content_metadata(id, version, cursor)
      legacy_id = metadata['legacy_id']
      legacy_version = metadata['legacy_version']
  
      reachable_dirs = [dir for dir in exports_dirs if safe_stat(dir)]
  
      # 1 result per type, in the same order as the given types
      results = []
  
      for type in list(types):
          if type not in type_info:
              raise ExportError("invalid type '{}' requested.".format(type))
          file_extension = type_info[type]['file_extension']
  
          # skip module PDFs
          if metadata['mediaType'] == MODULE_MIMETYPE and \
                  file_extension == 'pdf':
              continue
  
          mimetype = type_info[type]['mimetype']
          filename = '{}@{}.{}'.format(id, version, file_extension)
          legacy_filenames = [
              '{}-{}.{}'.format(legacy_id, legacy_version, ext)
              for ext in LEGACY_EXTENSION_MAP[file_extension]
          ]
          slugify_title_filename = u'{}-{}.{}'.format(slugify(metadata['title']),
                                                      version, file_extension)
  
          for dir in reachable_dirs:
              filepath = os.path.join(dir, filename)
              try:
                  if read_file:
                      with open(filepath, 'r') as file:
                          stats = os.fstat(file.fileno())
                          contents = file.read()
                  else:
                      stats = os.stat(filepath)
                      contents = None
                  modtime = fromtimestamp(int(stats.st_mtime))
                  results.append((slugify_title_filename, mimetype,
                                  stats.st_size, modtime, 'good', contents))
                  break
              except EnvironmentError:
                  pass
          else:
              # Let's see if the legacy file's there and make the new link
              legacy_file_found = False
              for dir in reachable_dirs:
                  filepath = os.path.join(dir, filename)
                  legacy_filepaths = [os.path.join(dir, fn)
                                      for fn in legacy_filenames]
                  for legacy_filepath in legacy_filepaths:
                      try:
                          if read_file:
                              with open(legacy_filepath, 'r') as file:
                                  stats = os.fstat(file.fileno())
                                  contents = file.read()
                          else:
                              stats = os.stat(legacy_filepath)
                              contents = None
                          modtime = fromtimestamp(stats.st_mtime)
                          os.link(legacy_filepath, filepath)
                          results.append((slugify_title_filename, mimetype,
                                          stats.st_size, modtime, 'good',
                                          contents))
                          legacy_file_found = True
                          break
                      except EnvironmentError:
                          pass
                  if legacy_file_found:
                      break
              else:
                  filenames = [filename] + legacy_filenames
                  log_formatted_filenames = '\n'.join([' - {}'.format(x)
                                                       for x in filenames])
                  logger.error("Could not find a file for '{}' at version '{}' "
                               "with any of the following file names:\n{}"
                               .format(id, version, log_formatted_filenames))
                  # No file, return "missing" state
                  results.append((slugify_title_filename, mimetype,
                                  0, None, 'missing', None))
  
      return results