def secure_filename(filename):
  """
  Similar to #werkzeug.secure_filename(), but preserves leading dots in
  the filename.
  """

  while True:
    filename = filename.lstrip('/').lstrip('\\')
    if filename.startswith('..') and filename[2:3] in '/\\':
      filename = filename[3:]
    elif filename.startswith('.') and filename[1:2] in '/\\':
      filename = filename[2:]
    else:
      break

  has_dot = filename.startswith('.')
  filename = werkzeug.secure_filename(filename)
  if has_dot:
    filename = '.' + filename
  return filename