models.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. import concurrent.futures
  2. import hashlib
  3. import itertools
  4. import logging
  5. import os.path
  6. import re
  7. import time
  8. from collections import OrderedDict, defaultdict
  9. from contextlib import contextmanager
  10. from io import BytesIO
  11. from tempfile import SpooledTemporaryFile
  12. from typing import Any, Dict, Iterable, List, Optional, Union
  13. import willow
  14. from django.apps import apps
  15. from django.conf import settings
  16. from django.core import checks
  17. from django.core.cache import DEFAULT_CACHE_ALIAS, InvalidCacheBackendError, caches
  18. from django.core.cache.backends.base import BaseCache
  19. from django.core.exceptions import ImproperlyConfigured
  20. from django.core.files import File
  21. from django.core.files.storage import InvalidStorageError, default_storage, storages
  22. from django.db import models
  23. from django.db.models import Q
  24. from django.forms.utils import flatatt
  25. from django.urls import reverse
  26. from django.utils.functional import cached_property, classproperty
  27. from django.utils.module_loading import import_string
  28. from django.utils.safestring import mark_safe
  29. from django.utils.translation import gettext_lazy as _
  30. from taggit.managers import TaggableManager
  31. from wagtail import hooks
  32. from wagtail.coreutils import string_to_ascii
  33. from wagtail.images.exceptions import (
  34. InvalidFilterSpecError,
  35. UnknownOutputImageFormatError,
  36. )
  37. from wagtail.images.fields import image_format_name_to_content_type
  38. from wagtail.images.image_operations import (
  39. FilterOperation,
  40. FormatOperation,
  41. ImageTransform,
  42. TransformOperation,
  43. )
  44. from wagtail.images.rect import Rect
  45. from wagtail.models import CollectionMember, ReferenceIndex
  46. from wagtail.search import index
  47. from wagtail.search.queryset import SearchableQuerySetMixin
  48. from wagtail.utils.file import hash_filelike
  49. logger = logging.getLogger("wagtail.images")
  50. IMAGE_FORMAT_EXTENSIONS = {
  51. "avif": ".avif",
  52. "jpeg": ".jpg",
  53. "png": ".png",
  54. "gif": ".gif",
  55. "webp": ".webp",
  56. "svg": ".svg",
  57. "ico": ".ico",
  58. }
  59. class SourceImageIOError(IOError):
  60. """
  61. Custom exception to distinguish IOErrors that were thrown while opening the source image
  62. """
  63. pass
  64. class ImageQuerySet(SearchableQuerySetMixin, models.QuerySet):
  65. def prefetch_renditions(self, *filters):
  66. """
  67. Prefetches generated renditions for the given filters.
  68. Returns all renditions when no filters are provided.
  69. """
  70. rendition_model = self.model.get_rendition_model()
  71. queryset = rendition_model.objects.all()
  72. if filters:
  73. # Get a list of filter spec strings. The given value could contain Filter objects
  74. filter_specs = [
  75. filter.spec if isinstance(filter, Filter) else filter
  76. for filter in filters
  77. ]
  78. queryset = queryset.filter(filter_spec__in=filter_specs)
  79. return self.prefetch_related(
  80. models.Prefetch(
  81. "renditions",
  82. queryset=queryset,
  83. to_attr="prefetched_renditions",
  84. )
  85. )
  86. def get_upload_to(instance, filename):
  87. """
  88. Obtain a valid upload path for an image file.
  89. This needs to be a module-level function so that it can be referenced within migrations,
  90. but simply delegates to the `get_upload_to` method of the instance, so that AbstractImage
  91. subclasses can override it.
  92. """
  93. return instance.get_upload_to(filename)
  94. def get_rendition_upload_to(instance, filename):
  95. """
  96. Obtain a valid upload path for an image rendition file.
  97. This needs to be a module-level function so that it can be referenced within migrations,
  98. but simply delegates to the `get_upload_to` method of the instance, so that AbstractRendition
  99. subclasses can override it.
  100. """
  101. return instance.get_upload_to(filename)
  102. def get_rendition_storage():
  103. """
  104. Obtain the storage object for an image rendition file.
  105. Returns custom storage (if defined), or the default storage.
  106. This needs to be a module-level function, because we do not yet
  107. have an instance when Django loads the models.
  108. """
  109. storage = getattr(settings, "WAGTAILIMAGES_RENDITION_STORAGE", default_storage)
  110. if isinstance(storage, str):
  111. try:
  112. # First see if the string is a storage alias
  113. storage = storages[storage]
  114. except InvalidStorageError:
  115. # Otherwise treat the string as a dotted path
  116. try:
  117. module = import_string(storage)
  118. storage = module()
  119. except ImportError:
  120. raise ImproperlyConfigured(
  121. "WAGTAILIMAGES_RENDITION_STORAGE must be either a valid storage alias or dotted module path."
  122. )
  123. return storage
  124. class ImageFileMixin:
  125. def is_stored_locally(self):
  126. """
  127. Returns True if the image is hosted on the local filesystem
  128. """
  129. try:
  130. self.file.path
  131. return True
  132. except NotImplementedError:
  133. return False
  134. def get_file_size(self):
  135. if self.file_size is None:
  136. try:
  137. self.file_size = self.file.size
  138. except Exception as e: # noqa: BLE001
  139. # File not found
  140. #
  141. # Have to catch everything, because the exception
  142. # depends on the file subclass, and therefore the
  143. # storage being used.
  144. raise SourceImageIOError(str(e))
  145. self.save(update_fields=["file_size"])
  146. return self.file_size
  147. @contextmanager
  148. def open_file(self):
  149. # Open file if it is closed
  150. close_file = False
  151. try:
  152. image_file = self.file
  153. if self.file.closed:
  154. # Reopen the file
  155. if self.is_stored_locally():
  156. self.file.open("rb")
  157. else:
  158. # Some external storage backends don't allow reopening
  159. # the file. Get a fresh file instance. #1397
  160. storage = self._meta.get_field("file").storage
  161. image_file = storage.open(self.file.name, "rb")
  162. close_file = True
  163. except OSError as e:
  164. # re-throw this as a SourceImageIOError so that calling code can distinguish
  165. # these from IOErrors elsewhere in the process
  166. raise SourceImageIOError(str(e))
  167. # Seek to beginning
  168. image_file.seek(0)
  169. try:
  170. yield image_file
  171. finally:
  172. if close_file:
  173. image_file.close()
  174. @contextmanager
  175. def get_willow_image(self):
  176. with self.open_file() as image_file:
  177. yield willow.Image.open(image_file)
  178. class WagtailImageFieldFile(models.fields.files.ImageFieldFile):
  179. """
  180. Override the ImageFieldFile in order to use Willow instead
  181. of Pillow.
  182. """
  183. def _get_image_dimensions(self):
  184. """
  185. override _get_image_dimensions to call our own get_image_dimensions.
  186. """
  187. if not hasattr(self, "_dimensions_cache"):
  188. self._dimensions_cache = self.get_image_dimensions()
  189. return self._dimensions_cache
  190. def get_image_dimensions(self):
  191. """
  192. The upstream ImageFieldFile calls a local function get_image_dimensions. In this implementation we've made get_image_dimensions
  193. a method to make it easier to override for Wagtail developers in the future.
  194. """
  195. close = self.closed
  196. try:
  197. self.open()
  198. image = willow.Image.open(self)
  199. return image.get_size()
  200. finally:
  201. if close:
  202. self.close()
  203. else:
  204. self.seek(0)
  205. class WagtailImageField(models.ImageField):
  206. """
  207. Override the attr_class on the Django ImageField Model to inject our ImageFieldFile
  208. with Willow support.
  209. """
  210. attr_class = WagtailImageFieldFile
  211. class AbstractImage(ImageFileMixin, CollectionMember, index.Indexed, models.Model):
  212. title = models.CharField(max_length=255, verbose_name=_("title"))
  213. """ Use local ImageField with Willow support. """
  214. file = WagtailImageField(
  215. verbose_name=_("file"),
  216. upload_to=get_upload_to,
  217. width_field="width",
  218. height_field="height",
  219. )
  220. width = models.IntegerField(verbose_name=_("width"), editable=False)
  221. height = models.IntegerField(verbose_name=_("height"), editable=False)
  222. created_at = models.DateTimeField(
  223. verbose_name=_("created at"), auto_now_add=True, db_index=True
  224. )
  225. uploaded_by_user = models.ForeignKey(
  226. settings.AUTH_USER_MODEL,
  227. verbose_name=_("uploaded by user"),
  228. null=True,
  229. blank=True,
  230. editable=False,
  231. on_delete=models.SET_NULL,
  232. )
  233. uploaded_by_user.wagtail_reference_index_ignore = True
  234. tags = TaggableManager(help_text=None, blank=True, verbose_name=_("tags"))
  235. focal_point_x = models.PositiveIntegerField(null=True, blank=True)
  236. focal_point_y = models.PositiveIntegerField(null=True, blank=True)
  237. focal_point_width = models.PositiveIntegerField(null=True, blank=True)
  238. focal_point_height = models.PositiveIntegerField(null=True, blank=True)
  239. file_size = models.PositiveIntegerField(null=True, editable=False)
  240. # A SHA-1 hash of the file contents
  241. file_hash = models.CharField(
  242. max_length=40, blank=True, editable=False, db_index=True
  243. )
  244. objects = ImageQuerySet.as_manager()
  245. def _set_file_hash(self):
  246. with self.open_file() as f:
  247. self.file_hash = hash_filelike(f)
  248. def get_file_hash(self):
  249. if self.file_hash == "":
  250. self._set_file_hash()
  251. self.save(update_fields=["file_hash"])
  252. return self.file_hash
  253. def _set_image_file_metadata(self):
  254. self.file.open()
  255. # Set new image file size
  256. self.file_size = self.file.size
  257. # Set new image file hash
  258. self._set_file_hash()
  259. self.file.seek(0)
  260. def get_upload_to(self, filename):
  261. """
  262. Generates a file path in the "original_images" folder.
  263. Ensuring ASCII characters and limiting length to prevent filesystem issues during uploads.
  264. """
  265. folder_name = "original_images"
  266. filename = self.file.field.storage.get_valid_name(filename)
  267. # convert the filename to simple ascii characters and then
  268. # replace non-ascii characters in filename with _ , to sidestep issues with filesystem encoding
  269. filename = "".join(
  270. (i if ord(i) < 128 else "_") for i in string_to_ascii(filename)
  271. )
  272. # Truncate filename so it fits in the 100 character limit
  273. # https://code.djangoproject.com/ticket/9893
  274. full_path = os.path.join(folder_name, filename)
  275. if len(full_path) >= 95:
  276. chars_to_trim = len(full_path) - 94
  277. prefix, extension = os.path.splitext(filename)
  278. filename = prefix[:-chars_to_trim] + extension
  279. full_path = os.path.join(folder_name, filename)
  280. return full_path
  281. def get_usage(self):
  282. return ReferenceIndex.get_grouped_references_to(self)
  283. @property
  284. def usage_url(self):
  285. return reverse("wagtailimages:image_usage", args=(self.id,))
  286. search_fields = CollectionMember.search_fields + [
  287. index.SearchField("title", boost=10),
  288. index.AutocompleteField("title"),
  289. index.FilterField("title"),
  290. index.RelatedFields(
  291. "tags",
  292. [
  293. index.SearchField("name", boost=10),
  294. index.AutocompleteField("name"),
  295. ],
  296. ),
  297. index.FilterField("uploaded_by_user"),
  298. index.FilterField("created_at"),
  299. index.FilterField("id"),
  300. ]
  301. def __str__(self):
  302. return self.title
  303. def get_rect(self):
  304. return Rect(0, 0, self.width, self.height)
  305. def get_focal_point(self):
  306. if (
  307. self.focal_point_x is not None
  308. and self.focal_point_y is not None
  309. and self.focal_point_width is not None
  310. and self.focal_point_height is not None
  311. ):
  312. return Rect.from_point(
  313. self.focal_point_x,
  314. self.focal_point_y,
  315. self.focal_point_width,
  316. self.focal_point_height,
  317. )
  318. def has_focal_point(self):
  319. return self.get_focal_point() is not None
  320. def set_focal_point(self, rect):
  321. if rect is not None:
  322. self.focal_point_x = rect.centroid_x
  323. self.focal_point_y = rect.centroid_y
  324. self.focal_point_width = rect.width
  325. self.focal_point_height = rect.height
  326. else:
  327. self.focal_point_x = None
  328. self.focal_point_y = None
  329. self.focal_point_width = None
  330. self.focal_point_height = None
  331. def get_suggested_focal_point(self):
  332. if self.is_svg():
  333. # We can't run feature detection on SVGs, and don't provide a
  334. # pathway from SVG -> raster formats, so don't try it.
  335. return None
  336. with self.get_willow_image() as willow:
  337. faces = willow.detect_faces()
  338. if faces:
  339. # Create a bounding box around all faces
  340. left = min(face[0] for face in faces)
  341. top = min(face[1] for face in faces)
  342. right = max(face[2] for face in faces)
  343. bottom = max(face[3] for face in faces)
  344. focal_point = Rect(left, top, right, bottom)
  345. else:
  346. features = willow.detect_features()
  347. if features:
  348. # Create a bounding box around all features
  349. left = min(feature[0] for feature in features)
  350. top = min(feature[1] for feature in features)
  351. right = max(feature[0] for feature in features)
  352. bottom = max(feature[1] for feature in features)
  353. focal_point = Rect(left, top, right, bottom)
  354. else:
  355. return None
  356. # Add 20% to width and height and give it a minimum size
  357. x, y = focal_point.centroid
  358. width, height = focal_point.size
  359. width *= 1.20
  360. height *= 1.20
  361. width = max(width, 100)
  362. height = max(height, 100)
  363. return Rect.from_point(x, y, width, height)
  364. @classmethod
  365. def get_rendition_model(cls):
  366. """Get the Rendition model for this Image model"""
  367. return cls.renditions.rel.related_model
  368. def _get_prefetched_renditions(self) -> Union[Iterable["AbstractRendition"], None]:
  369. if "renditions" in getattr(self, "_prefetched_objects_cache", {}):
  370. return self.renditions.all()
  371. return getattr(self, "prefetched_renditions", None)
  372. def _add_to_prefetched_renditions(self, rendition: "AbstractRendition") -> None:
  373. # Reuse this rendition if requested again from this object
  374. try:
  375. self._prefetched_objects_cache["renditions"]._result_cache.append(rendition)
  376. except (AttributeError, KeyError):
  377. pass
  378. try:
  379. self.prefetched_renditions.append(rendition)
  380. except AttributeError:
  381. pass
  382. def get_rendition(self, filter: Union["Filter", str]) -> "AbstractRendition":
  383. """
  384. Returns a ``Rendition`` instance with a ``file`` field value (an
  385. image) reflecting the supplied ``filter`` value and focal point values
  386. from this object.
  387. Note: If using custom image models, an instance of the custom rendition
  388. model will be returned.
  389. """
  390. Rendition = self.get_rendition_model()
  391. if isinstance(filter, str):
  392. filter = Filter(spec=filter)
  393. try:
  394. rendition = self.find_existing_rendition(filter)
  395. except Rendition.DoesNotExist:
  396. rendition = self.create_rendition(filter)
  397. # Reuse this rendition if requested again from this object
  398. self._add_to_prefetched_renditions(rendition)
  399. cache_key = Rendition.construct_cache_key(
  400. self, filter.get_cache_key(self), filter.spec
  401. )
  402. Rendition.cache_backend.set(cache_key, rendition)
  403. return rendition
  404. def find_existing_rendition(self, filter: "Filter") -> "AbstractRendition":
  405. """
  406. Returns an existing ``Rendition`` instance with a ``file`` field value
  407. (an image) reflecting the supplied ``filter`` value and focal point
  408. values from this object.
  409. If no such rendition exists, a ``DoesNotExist`` error is raised for the
  410. relevant model.
  411. Note: If using custom image models, an instance of the custom rendition
  412. model will be returned.
  413. """
  414. Rendition = self.get_rendition_model()
  415. try:
  416. return self.find_existing_renditions(filter)[filter]
  417. except KeyError:
  418. raise Rendition.DoesNotExist
  419. def create_rendition(self, filter: "Filter") -> "AbstractRendition":
  420. """
  421. Creates and returns a ``Rendition`` instance with a ``file`` field
  422. value (an image) reflecting the supplied ``filter`` value and focal
  423. point values from this object.
  424. This method is usually called by ``Image.get_rendition()``, after first
  425. checking that a suitable rendition does not already exist.
  426. Note: If using custom image models, an instance of the custom rendition
  427. model will be returned.
  428. """
  429. # Because of unique constraints applied to the model, we use
  430. # get_or_create() to guard against race conditions
  431. rendition, created = self.renditions.get_or_create(
  432. filter_spec=filter.spec,
  433. focal_point_key=filter.get_cache_key(self),
  434. defaults={"file": self.generate_rendition_file(filter)},
  435. )
  436. return rendition
  437. def get_renditions(
  438. self, *filters: Union["Filter", str]
  439. ) -> Dict[str, "AbstractRendition"]:
  440. """
  441. Returns a ``dict`` of ``Rendition`` instances with image files reflecting
  442. the supplied ``filters``, keyed by filter spec patterns.
  443. Note: If using custom image models, instances of the custom rendition
  444. model will be returned.
  445. """
  446. Rendition = self.get_rendition_model()
  447. # We don’t support providing mixed Filter and string arguments in the same call.
  448. if isinstance(filters[0], str):
  449. filters = [Filter(spec) for spec in dict.fromkeys(filters).keys()]
  450. # Find existing renditions where possible
  451. renditions = self.find_existing_renditions(*filters)
  452. # Create any renditions not found in prefetched values, cache or database
  453. not_found = [f for f in filters if f not in renditions]
  454. for filter, rendition in self.create_renditions(*not_found).items():
  455. self._add_to_prefetched_renditions(rendition)
  456. renditions[filter] = rendition
  457. # Update the cache
  458. cache_additions = {
  459. Rendition.construct_cache_key(
  460. self, filter.get_cache_key(self), filter.spec
  461. ): rendition
  462. for filter, rendition in renditions.items()
  463. # prevent writing of cached data back to the cache
  464. if not getattr(rendition, "_from_cache", False)
  465. }
  466. if cache_additions:
  467. Rendition.cache_backend.set_many(cache_additions)
  468. # Make sure key insertion order matches the input order.
  469. return {filter.spec: renditions[filter] for filter in filters}
  470. def find_existing_renditions(
  471. self, *filters: "Filter"
  472. ) -> Dict["Filter", "AbstractRendition"]:
  473. """
  474. Returns a dictionary of existing ``Rendition`` instances with ``file``
  475. values (images) reflecting the supplied ``filters`` and the focal point
  476. values from this object.
  477. Filters for which an existing rendition cannot be found are omitted
  478. from the return value. If none of the requested renditions have been
  479. created before, the return value will be an empty dict.
  480. """
  481. Rendition = self.get_rendition_model()
  482. filters_by_spec: Dict[str, Filter] = {f.spec: f for f in filters}
  483. found: Dict[Filter, AbstractRendition] = {}
  484. # Interrogate prefetched values first (where available)
  485. prefetched_renditions = self._get_prefetched_renditions()
  486. if prefetched_renditions is not None:
  487. # NOTE: When renditions are prefetched, it's assumed that if the
  488. # requested renditions exist, they will be present in the
  489. # prefetched value, and further cache/database lookups are avoided.
  490. # group renditions by the filters of interest
  491. potential_matches: Dict[Filter, List[AbstractRendition]] = defaultdict(list)
  492. for rendition in prefetched_renditions:
  493. try:
  494. filter = filters_by_spec[rendition.filter_spec]
  495. except KeyError:
  496. continue # this rendition can be ignored
  497. else:
  498. potential_matches[filter].append(rendition)
  499. # For each filter we have renditions for, look for one with a
  500. # 'focal_point_key' value matching filter.get_cache_key()
  501. for filter, renditions in potential_matches.items():
  502. focal_point_key = filter.get_cache_key(self)
  503. for rendition in renditions:
  504. if rendition.focal_point_key == focal_point_key:
  505. # to prevent writing of cached data back to the cache
  506. rendition._from_cache = True
  507. # use this rendition
  508. found[filter] = rendition
  509. # skip to the next filter
  510. break
  511. else:
  512. # Renditions are not prefetched, so attempt to find suitable
  513. # items in the cache or database
  514. # Query the cache first
  515. cache_keys = [
  516. Rendition.construct_cache_key(self, filter.get_cache_key(self), spec)
  517. for spec, filter in filters_by_spec.items()
  518. ]
  519. for rendition in Rendition.cache_backend.get_many(cache_keys).values():
  520. filter = filters_by_spec[rendition.filter_spec]
  521. found[filter] = rendition
  522. # For items not found in the cache, look in the database
  523. not_found = [f for f in filters if f not in found]
  524. if not_found:
  525. lookup_q = Q()
  526. for filter in not_found:
  527. lookup_q |= Q(
  528. filter_spec=filter.spec,
  529. focal_point_key=filter.get_cache_key(self),
  530. )
  531. for rendition in self.renditions.filter(lookup_q):
  532. filter = filters_by_spec[rendition.filter_spec]
  533. found[filter] = rendition
  534. return found
  535. def create_renditions(
  536. self, *filters: "Filter"
  537. ) -> Dict["Filter", "AbstractRendition"]:
  538. """
  539. Creates multiple ``Rendition`` instances with image files reflecting the supplied
  540. ``filters``, and returns them as a ``dict`` keyed by the relevant ``Filter`` instance.
  541. Where suitable renditions already exist in the database, they will be returned instead,
  542. so as not to create duplicates.
  543. This method is usually called by ``Image.get_renditions()``, after first
  544. checking that a suitable rendition does not already exist.
  545. Note: If using custom image models, an instance of the custom rendition
  546. model will be returned.
  547. """
  548. Rendition = self.get_rendition_model()
  549. if not filters:
  550. return {}
  551. if len(filters) == 1:
  552. # create_rendition() is better for single renditions, as it can
  553. # utilize QuerySet.get_or_create(), which has better handling of
  554. # race conditions
  555. filter = filters[0]
  556. return {filter: self.create_rendition(filter)}
  557. return_value: Dict[Filter, AbstractRendition] = {}
  558. filter_map: Dict[str, Filter] = {f.spec: f for f in filters}
  559. # Read file contents into memory
  560. with self.open_file() as file:
  561. original_image_bytes = file.read()
  562. to_create = []
  563. with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
  564. for future in concurrent.futures.as_completed(
  565. executor.submit(
  566. self.generate_rendition_instance,
  567. filter,
  568. BytesIO(original_image_bytes),
  569. )
  570. for filter in filters
  571. ):
  572. to_create.append(future.result())
  573. # Rendition generation can take a while. So, if other processes have created
  574. # identical renditions in the meantime, we should find them to avoid clashes.
  575. # NB: Clashes can still occur, because there is no get_or_create() equivalent
  576. # for multiple objects. However, this will reduce that risk considerably.
  577. files_for_deletion: List[File] = []
  578. # Assemble Q() to identify potential clashes
  579. lookup_q = Q()
  580. for rendition in to_create:
  581. lookup_q |= Q(
  582. filter_spec=rendition.filter_spec,
  583. focal_point_key=rendition.focal_point_key,
  584. )
  585. for existing in self.renditions.filter(lookup_q):
  586. # Include the existing rendition in the return value
  587. filter = filter_map[existing.filter_spec]
  588. return_value[filter] = existing
  589. for new in list(to_create):
  590. if (
  591. new.filter_spec == existing.filter_spec
  592. and new.focal_point_key == existing.focal_point_key
  593. ):
  594. # Avoid creating the new version
  595. to_create.remove(new)
  596. # Mark for deletion later, so as not to hold up creation
  597. files_for_deletion.append(new.file)
  598. for new in Rendition.objects.bulk_create(to_create, ignore_conflicts=True):
  599. filter = filter_map[new.filter_spec]
  600. return_value[filter] = new
  601. # Delete redundant rendition image files
  602. for file in files_for_deletion:
  603. file.delete(save=False)
  604. return return_value
  605. def generate_rendition_instance(
  606. self, filter: "Filter", source: BytesIO
  607. ) -> "AbstractRendition":
  608. """
  609. Use the supplied ``source`` image to create and return an
  610. **unsaved** ``Rendition`` instance, with a ``file`` value reflecting
  611. the supplied ``filter`` value and focal point values from this object.
  612. """
  613. return self.get_rendition_model()(
  614. image=self,
  615. filter_spec=filter.spec,
  616. focal_point_key=filter.get_cache_key(self),
  617. file=self.generate_rendition_file(
  618. filter, source=File(source, name=self.file.name)
  619. ),
  620. )
  621. def generate_rendition_file(self, filter: "Filter", *, source: File = None) -> File:
  622. """
  623. Generates an in-memory image matching the supplied ``filter`` value
  624. and focal point value from this object, wraps it in a ``File`` object
  625. with a suitable filename, and returns it. The return value is used
  626. as the ``file`` field value for rendition objects saved by
  627. ``AbstractImage.create_rendition()``.
  628. If the contents of ``self.file`` has already been read into memory, the
  629. ``source`` keyword can be used to provide a reference to the in-memory
  630. ``File``, bypassing the need to reload the image contents from storage.
  631. NOTE: The responsibility of generating the new image from the original
  632. falls to the supplied ``filter`` object. If you want to do anything
  633. custom with rendition images (for example, to preserve metadata from
  634. the original image), you might want to consider swapping out ``filter``
  635. for an instance of a custom ``Filter`` subclass of your design.
  636. """
  637. cache_key = filter.get_cache_key(self)
  638. logger.debug(
  639. "Generating '%s' rendition for image %d",
  640. filter.spec,
  641. self.pk,
  642. )
  643. start_time = time.time()
  644. try:
  645. generated_image = filter.run(
  646. self,
  647. SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE),
  648. source=source,
  649. )
  650. logger.debug(
  651. "Generated '%s' rendition for image %d in %.1fms",
  652. filter.spec,
  653. self.pk,
  654. (time.time() - start_time) * 1000,
  655. )
  656. except: # noqa:B901,E722
  657. logger.debug(
  658. "Failed to generate '%s' rendition for image %d",
  659. filter.spec,
  660. self.pk,
  661. )
  662. raise
  663. # Generate filename
  664. input_filename = os.path.basename(self.file.name)
  665. input_filename_without_extension, input_extension = os.path.splitext(
  666. input_filename
  667. )
  668. output_extension = (
  669. filter.spec.replace("|", ".")
  670. + IMAGE_FORMAT_EXTENSIONS[generated_image.format_name]
  671. )
  672. if cache_key:
  673. output_extension = cache_key + "." + output_extension
  674. # Truncate filename to prevent it going over 60 chars
  675. output_filename_without_extension = input_filename_without_extension[
  676. : (59 - len(output_extension))
  677. ]
  678. output_filename = output_filename_without_extension + "." + output_extension
  679. return File(generated_image.f, name=output_filename)
  680. def is_portrait(self):
  681. return self.width < self.height
  682. def is_landscape(self):
  683. return self.height < self.width
  684. def is_svg(self):
  685. _, ext = os.path.splitext(self.file.name)
  686. return ext.lower() == ".svg"
  687. @property
  688. def filename(self):
  689. return os.path.basename(self.file.name)
  690. @property
  691. def default_alt_text(self):
  692. # by default the alt text field (used in rich text insertion) is populated
  693. # from the title. Subclasses might provide a separate alt field, and
  694. # override this
  695. return self.title
  696. def is_editable_by_user(self, user):
  697. from wagtail.images.permissions import permission_policy
  698. return permission_policy.user_has_permission_for_instance(user, "change", self)
  699. class Meta:
  700. abstract = True
  701. class Image(AbstractImage):
  702. admin_form_fields = (
  703. "title",
  704. "file",
  705. "collection",
  706. "tags",
  707. "focal_point_x",
  708. "focal_point_y",
  709. "focal_point_width",
  710. "focal_point_height",
  711. )
  712. class Meta(AbstractImage.Meta):
  713. verbose_name = _("image")
  714. verbose_name_plural = _("images")
  715. permissions = [
  716. ("choose_image", "Can choose image"),
  717. ]
  718. class Filter:
  719. """
  720. Represents one or more operations that can be applied to an Image to produce a rendition
  721. appropriate for final display on the website. Usually this would be a resize operation,
  722. but could potentially involve colour processing, etc.
  723. """
  724. spec_pattern = re.compile(r"^[A-Za-z0-9_\-\.]+$")
  725. pipe_spec_pattern = re.compile(r"^[A-Za-z0-9_\-\.\|]+$")
  726. expanding_spec_pattern = re.compile(r"^[A-Za-z0-9_\-\.{},]+$")
  727. pipe_expanding_spec_pattern = re.compile(r"^[A-Za-z0-9_\-\.{},\|]+$")
  728. def __init__(self, spec=None):
  729. # The spec pattern is operation1-var1-var2|operation2-var1
  730. self.spec = spec
  731. @classmethod
  732. def expand_spec(self, spec: Union["str", Iterable["str"]]) -> List["str"]:
  733. """
  734. Converts a spec pattern with brace-expansions, into a list of spec patterns.
  735. For example, "width-{100,200}" becomes ["width-100", "width-200"].
  736. Supports providing filter specs already split, or pipe or space-separated.
  737. """
  738. if isinstance(spec, str):
  739. separator = "|" if "|" in spec else " "
  740. spec = spec.split(separator)
  741. expanded_segments = []
  742. for segment in spec:
  743. # Check if segment has braces to expand
  744. if "{" in segment and "}" in segment:
  745. prefix, options_suffixed = segment.split("{")
  746. options_pattern, suffix = options_suffixed.split("}")
  747. options = options_pattern.split(",")
  748. expanded_segments.append(
  749. [prefix + option + suffix for option in options]
  750. )
  751. else:
  752. expanded_segments.append([segment])
  753. # Cartesian product of all expanded segments (equivalent to nested for loops).
  754. combinations = itertools.product(*expanded_segments)
  755. return ["|".join(combination) for combination in combinations]
  756. @cached_property
  757. def operations(self):
  758. # Search for operations
  759. registered_operations = {}
  760. for fn in hooks.get_hooks("register_image_operations"):
  761. registered_operations.update(dict(fn()))
  762. # Build list of operation objects
  763. operations = []
  764. for op_spec in self.spec.split("|"):
  765. op_spec_parts = op_spec.split("-")
  766. if op_spec_parts[0] not in registered_operations:
  767. raise InvalidFilterSpecError(
  768. "Unrecognised operation: %s" % op_spec_parts[0]
  769. )
  770. op_class = registered_operations[op_spec_parts[0]]
  771. operations.append(op_class(*op_spec_parts))
  772. return operations
  773. @property
  774. def transform_operations(self):
  775. return [
  776. operation
  777. for operation in self.operations
  778. if isinstance(operation, TransformOperation)
  779. ]
  780. @property
  781. def filter_operations(self):
  782. return [
  783. operation
  784. for operation in self.operations
  785. if isinstance(operation, FilterOperation)
  786. ]
  787. def get_transform(self, image, size=None):
  788. """
  789. Returns an ImageTransform with all the transforms in this filter applied.
  790. The ImageTransform is an object with two attributes:
  791. - .size - The size of the final image
  792. - .matrix - An affine transformation matrix that combines any
  793. transform/scale/rotation operations that need to be applied to the image
  794. """
  795. if not size:
  796. size = (image.width, image.height)
  797. transform = ImageTransform(size, image_is_svg=image.is_svg())
  798. for operation in self.transform_operations:
  799. transform = operation.run(transform, image)
  800. return transform
  801. @contextmanager
  802. def get_willow_image(self, image: AbstractImage, source: File = None):
  803. if source is not None:
  804. yield willow.Image.open(source)
  805. else:
  806. with image.get_willow_image() as willow_image:
  807. yield willow_image
  808. def run(self, image: AbstractImage, output: BytesIO, source: File = None):
  809. with self.get_willow_image(image, source) as willow:
  810. original_format = willow.format_name
  811. # Fix orientation of image
  812. willow = willow.auto_orient()
  813. # Transform the image
  814. transform = self.get_transform(
  815. image, (willow.image.width, willow.image.height)
  816. )
  817. willow = willow.crop(transform.get_rect().round())
  818. willow = willow.resize(transform.size)
  819. # Apply filters
  820. env = {
  821. "original-format": original_format,
  822. }
  823. for operation in self.filter_operations:
  824. willow = operation.run(willow, image, env) or willow
  825. # Find the output format to use
  826. if "output-format" in env:
  827. # Developer specified an output format
  828. output_format = env["output-format"]
  829. else:
  830. # Convert bmp and webp to png by default
  831. default_conversions = {
  832. "avif": "png",
  833. "bmp": "png",
  834. "webp": "png",
  835. }
  836. # Convert unanimated GIFs to PNG as well
  837. if not willow.has_animation():
  838. default_conversions["gif"] = "png"
  839. # Allow the user to override the conversions
  840. conversion = getattr(settings, "WAGTAILIMAGES_FORMAT_CONVERSIONS", {})
  841. default_conversions.update(conversion)
  842. # Get the converted output format falling back to the original
  843. output_format = default_conversions.get(
  844. original_format, original_format
  845. )
  846. if output_format == "jpeg":
  847. # Allow changing of JPEG compression quality
  848. if "jpeg-quality" in env:
  849. quality = env["jpeg-quality"]
  850. else:
  851. quality = getattr(settings, "WAGTAILIMAGES_JPEG_QUALITY", 85)
  852. # If the image has an alpha channel, give it a white background
  853. if willow.has_alpha():
  854. willow = willow.set_background_color_rgb((255, 255, 255))
  855. return willow.save_as_jpeg(
  856. output, quality=quality, progressive=True, optimize=True
  857. )
  858. elif output_format == "png":
  859. return willow.save_as_png(output, optimize=True)
  860. elif output_format == "gif":
  861. return willow.save_as_gif(output)
  862. elif output_format == "webp":
  863. # Allow changing of WebP compression quality
  864. if (
  865. "output-format-options" in env
  866. and "lossless" in env["output-format-options"]
  867. ):
  868. return willow.save_as_webp(output, lossless=True)
  869. elif "webp-quality" in env:
  870. quality = env["webp-quality"]
  871. else:
  872. quality = getattr(settings, "WAGTAILIMAGES_WEBP_QUALITY", 80)
  873. return willow.save_as_webp(output, quality=quality)
  874. elif output_format == "avif":
  875. # Allow changing of AVIF compression quality
  876. if (
  877. "output-format-options" in env
  878. and "lossless" in env["output-format-options"]
  879. ):
  880. return willow.save_as_avif(output, lossless=True)
  881. elif "avif-quality" in env:
  882. quality = env["avif-quality"]
  883. else:
  884. quality = getattr(settings, "WAGTAILIMAGES_AVIF_QUALITY", 80)
  885. return willow.save_as_avif(output, quality=quality)
  886. elif output_format == "svg":
  887. return willow.save_as_svg(output)
  888. elif output_format == "ico":
  889. return willow.save_as_ico(output)
  890. raise UnknownOutputImageFormatError(
  891. f"Unknown output image format '{output_format}'"
  892. )
  893. def get_cache_key(self, image):
  894. vary_parts = []
  895. for operation in self.operations:
  896. for field in getattr(operation, "vary_fields", []):
  897. value = getattr(image, field, "")
  898. vary_parts.append(str(value))
  899. vary_string = "-".join(vary_parts)
  900. # Return blank string if there are no vary fields
  901. if not vary_string:
  902. return ""
  903. return hashlib.sha1(vary_string.encode("utf-8")).hexdigest()[:8]
  904. class ResponsiveImage:
  905. """
  906. A custom object used to represent a collection of renditions.
  907. Provides a 'renditions' property to access the renditions,
  908. and renders to the front-end HTML.
  909. """
  910. def __init__(
  911. self,
  912. renditions: Dict[str, "AbstractRendition"],
  913. attrs: Optional[Dict[str, Any]] = None,
  914. ):
  915. self.renditions = list(renditions.values())
  916. self.attrs = attrs
  917. @classmethod
  918. def get_width_srcset(cls, renditions_list: List["AbstractRendition"]):
  919. if len(renditions_list) == 1:
  920. # No point in using width descriptors if there is a single image.
  921. return renditions_list[0].url
  922. return ", ".join([f"{r.url} {r.width}w" for r in renditions_list])
  923. def __html__(self):
  924. attrs = self.attrs or {}
  925. # No point in adding a srcset if there is a single image.
  926. if len(self.renditions) > 1:
  927. attrs["srcset"] = self.get_width_srcset(self.renditions)
  928. # The first rendition is the "base" / "fallback" image.
  929. return self.renditions[0].img_tag(attrs)
  930. def __str__(self):
  931. return mark_safe(self.__html__())
  932. def __bool__(self):
  933. return bool(self.renditions)
  934. def __eq__(self, other: "ResponsiveImage"):
  935. if isinstance(other, ResponsiveImage):
  936. return self.renditions == other.renditions and self.attrs == other.attrs
  937. return False
  938. class Picture(ResponsiveImage):
  939. # Keep this separate from FormatOperation.supported_formats,
  940. # as the order our formats are defined in is essential for the picture tag.
  941. # Defines the order of <source> elements in the tag when format operations
  942. # are in use, and the priority order to identify the "fallback" format.
  943. # The browser will pick the first supported format in this list.
  944. source_format_order = ["avif", "webp", "jpeg", "png", "gif"]
  945. def __init__(
  946. self,
  947. renditions: Dict[str, "AbstractRendition"],
  948. attrs: Optional[Dict[str, Any]] = None,
  949. ):
  950. super().__init__(renditions, attrs)
  951. # Store renditions grouped by format separately for access from templates.
  952. self.formats = self.get_formats(renditions)
  953. def get_formats(
  954. self, renditions: Dict[str, "AbstractRendition"]
  955. ) -> Dict[str, List["AbstractRendition"]]:
  956. """
  957. Group renditions by the format they are for, if any.
  958. If there is only one format, no grouping is required.
  959. """
  960. formats = defaultdict(list)
  961. for spec, rendition in renditions.items():
  962. for fmt in FormatOperation.supported_formats:
  963. # Identify the spec’s format (if any).
  964. if f"format-{fmt}" in spec:
  965. formats[fmt].append(rendition)
  966. break
  967. # Avoid the split by format if there is only one.
  968. if len(formats.keys()) < 2:
  969. return {}
  970. return formats
  971. def get_fallback_format(self):
  972. for fmt in reversed(self.source_format_order):
  973. if fmt in self.formats:
  974. return fmt
  975. def __html__(self):
  976. # If there aren’t multiple formats, render a vanilla img tag with srcset.
  977. if not self.formats:
  978. return mark_safe(f"<picture>{super().__html__()}</picture>")
  979. attrs = self.attrs or {}
  980. sizes = f'sizes="{attrs["sizes"]}" ' if "sizes" in attrs else ""
  981. fallback_format = self.get_fallback_format()
  982. fallback_renditions = self.formats[fallback_format]
  983. sources = []
  984. for fmt in self.source_format_order:
  985. if fmt != fallback_format and fmt in self.formats:
  986. srcset = self.get_width_srcset(self.formats[fmt])
  987. mime = image_format_name_to_content_type(fmt)
  988. sources.append(f'<source srcset="{srcset}" {sizes}type="{mime}">')
  989. if len(fallback_renditions) > 1:
  990. attrs["srcset"] = self.get_width_srcset(fallback_renditions)
  991. # The first rendition is the "base" / "fallback" image.
  992. fallback = fallback_renditions[0].img_tag(attrs)
  993. return mark_safe(f"<picture>{''.join(sources)}{fallback}</picture>")
  994. class AbstractRendition(ImageFileMixin, models.Model):
  995. filter_spec = models.CharField(max_length=255, db_index=True)
  996. """ Use local ImageField with Willow support. """
  997. file = WagtailImageField(
  998. upload_to=get_rendition_upload_to,
  999. storage=get_rendition_storage,
  1000. width_field="width",
  1001. height_field="height",
  1002. )
  1003. width = models.IntegerField(editable=False)
  1004. height = models.IntegerField(editable=False)
  1005. focal_point_key = models.CharField(
  1006. max_length=16, blank=True, default="", editable=False
  1007. )
  1008. wagtail_reference_index_ignore = True
  1009. @property
  1010. def url(self):
  1011. return self.file.url
  1012. @property
  1013. def alt(self):
  1014. return self.image.default_alt_text
  1015. @property
  1016. def attrs(self):
  1017. """
  1018. The src, width, height, and alt attributes for an <img> tag, as a HTML
  1019. string
  1020. """
  1021. return flatatt(self.attrs_dict)
  1022. @property
  1023. def attrs_dict(self):
  1024. """
  1025. A dict of the src, width, height, and alt attributes for an <img> tag.
  1026. """
  1027. return OrderedDict(
  1028. [
  1029. ("src", self.url),
  1030. ("width", self.width),
  1031. ("height", self.height),
  1032. ("alt", self.alt),
  1033. ]
  1034. )
  1035. @property
  1036. def full_url(self):
  1037. url = self.url
  1038. if hasattr(settings, "WAGTAILADMIN_BASE_URL") and url.startswith("/"):
  1039. url = settings.WAGTAILADMIN_BASE_URL + url
  1040. return url
  1041. @property
  1042. def filter(self):
  1043. return Filter(self.filter_spec)
  1044. @cached_property
  1045. def focal_point(self):
  1046. image_focal_point = self.image.get_focal_point()
  1047. if image_focal_point:
  1048. transform = self.filter.get_transform(self.image)
  1049. return image_focal_point.transform(transform)
  1050. @property
  1051. def background_position_style(self):
  1052. """
  1053. Returns a `background-position` rule to be put in the inline style of an element which uses the rendition for its background.
  1054. This positions the rendition according to the value of the focal point. This is helpful for when the element does not have
  1055. the same aspect ratio as the rendition.
  1056. For example:
  1057. {% image page.image fill-1920x600 as image %}
  1058. <div style="background-image: url('{{ image.url }}'); {{ image.background_position_style }}">
  1059. </div>
  1060. """
  1061. focal_point = self.focal_point
  1062. if focal_point:
  1063. horz = int((focal_point.x * 100) // self.width)
  1064. vert = int((focal_point.y * 100) // self.height)
  1065. return f"background-position: {horz}% {vert}%;"
  1066. else:
  1067. return "background-position: 50% 50%;"
  1068. def img_tag(self, extra_attributes={}):
  1069. attrs = self.attrs_dict.copy()
  1070. attrs.update(apps.get_app_config("wagtailimages").default_attrs)
  1071. attrs.update(extra_attributes)
  1072. return mark_safe(f"<img{flatatt(attrs)}>")
  1073. def __html__(self):
  1074. return self.img_tag()
  1075. def get_upload_to(self, filename):
  1076. """
  1077. Generates a file path within the "images" folder by combining the folder name and the validated filename.
  1078. """
  1079. folder_name = "images"
  1080. filename = self.file.field.storage.get_valid_name(filename)
  1081. return os.path.join(folder_name, filename)
  1082. @classmethod
  1083. def check(cls, **kwargs):
  1084. errors = super().check(**kwargs)
  1085. if not cls._meta.abstract:
  1086. if not any(
  1087. set(constraint) == {"image", "filter_spec", "focal_point_key"}
  1088. for constraint in cls._meta.unique_together
  1089. ):
  1090. errors.append(
  1091. checks.Error(
  1092. "Custom rendition model %r has an invalid unique_together setting"
  1093. % cls,
  1094. hint="Custom rendition models must include the constraint "
  1095. "('image', 'filter_spec', 'focal_point_key') in their unique_together definition.",
  1096. obj=cls,
  1097. id="wagtailimages.E001",
  1098. )
  1099. )
  1100. return errors
  1101. @staticmethod
  1102. def construct_cache_key(image, filter_cache_key, filter_spec):
  1103. return "wagtail-rendition-" + "-".join(
  1104. [str(image.id), image.file_hash, filter_cache_key, filter_spec]
  1105. )
  1106. @classproperty
  1107. def cache_backend(cls) -> BaseCache:
  1108. try:
  1109. return caches["renditions"]
  1110. except InvalidCacheBackendError:
  1111. return caches[DEFAULT_CACHE_ALIAS]
  1112. def get_cache_key(self):
  1113. return self.construct_cache_key(
  1114. self.image, self.focal_point_key, self.filter_spec
  1115. )
  1116. def purge_from_cache(self):
  1117. self.cache_backend.delete(self.get_cache_key())
  1118. class Meta:
  1119. abstract = True
  1120. class Rendition(AbstractRendition):
  1121. image = models.ForeignKey(
  1122. Image, related_name="renditions", on_delete=models.CASCADE
  1123. )
  1124. class Meta:
  1125. unique_together = (("image", "filter_spec", "focal_point_key"),)