Django Library PyGal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from django.conf import settings
  2. import fstools
  3. import logging
  4. import mimetypes
  5. import media
  6. from ..models import get_item_type, TYPE_IMAGE, TYPE_VIDEO
  7. import os
  8. import pygal
  9. # Get a logger instance
  10. logger = logging.getLogger("CACHING")
  11. def get_image_class(full_path):
  12. return {
  13. TYPE_IMAGE: image,
  14. TYPE_VIDEO: video,
  15. }.get(get_item_type(full_path), other)
  16. def get_image_instance(full_path, request):
  17. return get_image_class(full_path)(full_path, request)
  18. class base_item(object):
  19. MIME_TYPES = {
  20. '.ada': 'text/x/adasrc',
  21. '.hex': 'text/x/hex',
  22. '.jpg': 'image/x/generic',
  23. '.jpeg': 'image/x/generic',
  24. '.jpe': 'image/x/generic',
  25. '.png': 'image/x/generic',
  26. '.tif': 'image/x/generic',
  27. '.tiff': 'image/x/generic',
  28. '.gif': 'image/x/generic',
  29. '.avi': 'video/x/generic',
  30. '.mpg': 'video/x/generic',
  31. '.mpeg': 'video/x/generic',
  32. '.mpe': 'video/x/generic',
  33. '.mov': 'video/x/generic',
  34. '.qt': 'video/x/generic',
  35. '.mp4': 'video/x/generic',
  36. '.webm': 'video/x/generic',
  37. '.ogv': 'video/x/generic',
  38. '.flv': 'video/x/generic',
  39. '.3gp': 'video/x/generic',
  40. }
  41. def __init__(self, full_path, request):
  42. self.full_path = full_path
  43. self.request = request
  44. self.rel_path = pygal.get_rel_path(full_path)
  45. #
  46. ext = os.path.splitext(self.full_path)[1].lower()
  47. self.mime_type = self.MIME_TYPES.get(ext, mimetypes.types_map.get(ext, 'unknown'))
  48. def __cache_image_folder__(self, rel_path):
  49. return os.path.join(settings.XNAIL_ROOT, rel_path.replace('_', '__').replace('/', '_'))
  50. def __cache_image_name__(self, max_size):
  51. filename = '%04d_%02x_%s.jpg' % (max_size, self.XNAIL_VERSION_NUMBER, fstools.uid(self.full_path, None))
  52. return os.path.join(self.__cache_image_folder__(self.rel_path), filename)
  53. def __delete_cache_image__(self, max_size):
  54. folder = self.__cache_image_folder__(self.rel_path)
  55. if os.path.isdir(folder):
  56. for fn in fstools.filelist(folder, '%04d*' % max_size):
  57. try:
  58. os.remove(fn)
  59. except OSError:
  60. pass # possibly file is already removed by another process
  61. class image(base_item):
  62. XNAIL_VERSION_NUMBER = 1
  63. def __init__(self, *args, **kwargs):
  64. base_item.__init__(self, *args, **kwargs)
  65. self.mime_type_xnails = mimetypes.types_map.get('.jpg', 'unknown')
  66. def get_resized_image_data(self, max_size):
  67. cache_filename = self.__cache_image_name__(max_size)
  68. if not os.path.exists(cache_filename):
  69. logger.info('Creating xnail-%d for %s', max_size, self.rel_path)
  70. self.__delete_cache_image__(max_size)
  71. im = media.image(self.full_path)
  72. im.resize(max_size)
  73. im.rotate_by_orientation()
  74. #
  75. # create cache file
  76. #
  77. fstools.mkdir(os.path.dirname(cache_filename))
  78. im.save(cache_filename)
  79. return im.image_data()
  80. return open(cache_filename, 'rb').read()
  81. def thumbnail_picture(self):
  82. return self.get_resized_image_data(pygal.get_thumbnail_max_size(self.request))
  83. def webnail_picture(self):
  84. return self.get_resized_image_data(pygal.get_webnail_size(self.request))
  85. class video(image):
  86. def get_resized_image_data(self, max_size):
  87. cache_filename = self.__cache_image_name__(max_size)
  88. if not os.path.exists(cache_filename):
  89. logger.info('Creating xnail-%d for %s', max_size, self.rel_path)
  90. self.__delete_cache_image__(max_size)
  91. im = media.image(self.full_path)
  92. im.resize(max_size)
  93. im.join(os.path.join(os.path.dirname(__file__), 'video.png'))
  94. #
  95. # create cache file
  96. #
  97. fstools.mkdir(os.path.dirname(cache_filename))
  98. im.save(cache_filename)
  99. return im.image_data()
  100. return open(cache_filename, 'rb').read()
  101. class other(base_item):
  102. def __init__(self, *args, **kwargs):
  103. base_item.__init__(self, *args, **kwargs)
  104. self.mime_type_xnails = mimetypes.types_map.get('.png', 'unknown')
  105. def thumbnail_picture(self):
  106. fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'mimetype_icons', '%s.png' % (self.mime_type).replace('/', '-'))
  107. if not os.path.exists(fn):
  108. fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'mimetype_icons', 'unknown.png')
  109. return open(fn, 'rb').read()
  110. def webnail_picture(self):
  111. return self.thumbnail_picture()