Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

56 řádky
1.8KB

  1. content_types = {
  2. 'css': 'text/css',
  3. 'gif': 'image/gif',
  4. 'html': 'text/html',
  5. 'jpg': 'image/jpeg',
  6. 'js': 'application/javascript',
  7. 'json': 'application/json',
  8. 'png': 'image/png',
  9. 'txt': 'text/plain',
  10. }
  11. def get_static_file(path, static_files):
  12. """Return the local filename and content type for the requested static
  13. file URL.
  14. :param path: the path portion of the requested URL.
  15. :param static_files: a static file configuration dictionary.
  16. This function returns a dictionary with two keys, "filename" and
  17. "content_type". If the requested URL does not match any static file, the
  18. return value is None.
  19. """
  20. if path in static_files:
  21. f = static_files[path]
  22. else:
  23. f = None
  24. rest = ''
  25. while path != '':
  26. path, last = path.rsplit('/', 1)
  27. rest = '/' + last + rest
  28. if path in static_files:
  29. f = static_files[path] + rest
  30. break
  31. elif path + '/' in static_files:
  32. f = static_files[path + '/'] + rest[1:]
  33. break
  34. if f:
  35. if isinstance(f, str):
  36. f = {'filename': f}
  37. if f['filename'].endswith('/'):
  38. if '' in static_files:
  39. if isinstance(static_files[''], str):
  40. f['filename'] += static_files['']
  41. else:
  42. f['filename'] += static_files['']['filename']
  43. if 'content_type' in static_files['']:
  44. f['content_type'] = static_files['']['content_type']
  45. else:
  46. f['filename'] += 'index.html'
  47. if 'content_type' not in f:
  48. ext = f['filename'].rsplit('.')[-1]
  49. f['content_type'] = content_types.get(
  50. ext, 'application/octet-stream')
  51. return f