django-imgproxy 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 anime.com.br
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,34 @@
1
+ Metadata-Version: 2.4
2
+ Name: django-imgproxy
3
+ Version: 0.1.0
4
+ Summary: Build imgproxy URLs for your Django templates
5
+ Keywords: django,imgproxy
6
+ Author: anime.com.br
7
+ Author-email: anime.com.br <opensource@anime.com.br>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Environment :: Web Environment
11
+ Classifier: Framework :: Django
12
+ Classifier: Framework :: Django :: 6
13
+ Classifier: Framework :: Django :: 6.0
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Internet
24
+ Classifier: Topic :: Internet :: WWW/HTTP
25
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
26
+ Classifier: Topic :: Software Development :: Libraries
27
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
+ Classifier: Typing :: Typed
29
+ Requires-Dist: django>=6.0
30
+ Requires-Python: >=3.12
31
+ Project-URL: Source, https://github.com/animebr/django-imgproxy
32
+ Project-URL: Discord, https://anime.com.br/discord
33
+ Description-Content-Type: text/markdown
34
+
File without changes
@@ -0,0 +1,44 @@
1
+ [project]
2
+ name = "django-imgproxy"
3
+ version = "0.1.0"
4
+ authors = [
5
+ {name = "anime.com.br", email = "opensource@anime.com.br"}
6
+ ]
7
+ description = "Build imgproxy URLs for your Django templates"
8
+ readme = "README.md"
9
+ license = "MIT"
10
+ license-files = ["LICENSE"]
11
+ keywords = ["django", "imgproxy"]
12
+ classifiers = [
13
+ "Environment :: Web Environment",
14
+ "Framework :: Django",
15
+ "Framework :: Django :: 6",
16
+ "Framework :: Django :: 6.0",
17
+ "Intended Audience :: Developers",
18
+ "License :: OSI Approved",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Programming Language :: Python :: 3.13",
25
+ "Programming Language :: Python :: 3.14",
26
+ "Topic :: Internet",
27
+ "Topic :: Internet :: WWW/HTTP",
28
+ "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
29
+ "Topic :: Software Development :: Libraries",
30
+ "Topic :: Software Development :: Libraries :: Python Modules",
31
+ "Typing :: Typed",
32
+ ]
33
+ requires-python = ">=3.12"
34
+ dependencies = [
35
+ "django>=6.0",
36
+ ]
37
+
38
+ [project.urls]
39
+ Source = "https://github.com/animebr/django-imgproxy"
40
+ Discord = "https://anime.com.br/discord"
41
+
42
+ [build-system]
43
+ requires = ["uv_build>=0.11.21,<0.12.0"]
44
+ build-backend = "uv_build"
@@ -0,0 +1,4 @@
1
+ __all__ = ["imgproxy", "ImgProxyImageField"]
2
+
3
+ from .builder import imgproxy
4
+ from .fields import ImgProxyImageField
@@ -0,0 +1,60 @@
1
+ __all__ = ["imgproxy"]
2
+
3
+ import base64
4
+ import hashlib
5
+ import hmac
6
+ import urllib.parse
7
+
8
+ from django_imgproxy import settings
9
+
10
+
11
+ def imgproxy(
12
+ source_url: str,
13
+ extension: str | None = None,
14
+ b64_filename: str | None = None,
15
+ **kwargs,
16
+ ) -> str:
17
+ source_url = settings.IMGPROXY_SOURCE_URL_PREFIX + source_url
18
+ if settings.IMGPROXY_BASE64_ENCODE_SOURCE_URL:
19
+ source_url = base64.urlsafe_b64encode(source_url.encode()).rstrip(b"=").decode()
20
+
21
+ if b64_filename:
22
+ source_url += "/" + urllib.parse.quote(b64_filename)
23
+ else:
24
+ source_url = urllib.parse.quote(
25
+ source_url,
26
+ safe=settings.IMGPROXY_PERCENT_ENCODE_SOURCE_URL_SAFE_CHARACTERS,
27
+ )
28
+ if extension:
29
+ source_url += "@" + extension
30
+ source_url = "plain/" + source_url
31
+
32
+ processing_options = []
33
+ if settings.IMGPROXY_ONLY_PRESETS:
34
+ if presets := kwargs.get("preset", kwargs.get("pr")):
35
+ processing_options.append(
36
+ presets if isinstance(presets, str) else str(presets)
37
+ )
38
+ else:
39
+ for option, arguments in kwargs.items():
40
+ if option in settings.IMGPROXY_PERCENT_ENCODED_PROCESSING_OPTIONS:
41
+ arguments = urllib.parse.quote(arguments, safe="")
42
+
43
+ processing_options.append(
44
+ "%s%s%s" % (option, settings.IMGPROXY_ARGUMENTS_SEPARATOR, arguments)
45
+ )
46
+
47
+ path = "/%s/%s" % ("/".join(processing_options), source_url)
48
+
49
+ if settings.IMGPROXY_SIGN_URL:
50
+ digest = hmac.digest(
51
+ key=settings.IMGPROXY_KEY,
52
+ msg=settings.IMGPROXY_SALT + path.encode(),
53
+ digest=hashlib.sha256,
54
+ )
55
+ signature = base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
56
+ else:
57
+ signature = settings.IMGPROXY_NO_SIGNATURE_SEGMENT
58
+
59
+ image_url = "%s/%s%s" % (settings.IMGPROXY_BASE_URL, signature, path)
60
+ return image_url
@@ -0,0 +1,26 @@
1
+ __all__ = ["ImgProxyImageFieldFile", "ImgProxyImageField"]
2
+
3
+ from datetime import timedelta
4
+
5
+ from django.db.models.fields.files import ImageField, ImageFieldFile
6
+ from django.utils import timezone
7
+
8
+ from django_imgproxy import imgproxy, settings
9
+
10
+
11
+ class ImgProxyImageFieldFile(ImageFieldFile):
12
+ @property
13
+ def url(self) -> str:
14
+ self._require_file()
15
+
16
+ processing_options = {"raw": "true"}
17
+ if ttl := settings.IMGPROXY_IMAGEFIELD_URL_TTL:
18
+ processing_options["expires"] = int(
19
+ (timezone.now() + timedelta(seconds=ttl)).timestamp()
20
+ )
21
+
22
+ return imgproxy(self.name, **processing_options)
23
+
24
+
25
+ class ImgProxyImageField(ImageField):
26
+ attr_class = ImgProxyImageFieldFile
@@ -0,0 +1,25 @@
1
+ from django.conf import settings
2
+
3
+ IMGPROXY_BASE_URL = getattr(
4
+ settings, "IMGPROXY_BASE_URL", "http://localhost:8080"
5
+ ).rstrip("/")
6
+ IMGPROXY_ONLY_PRESETS = getattr(settings, "IMGPROXY_ONLY_PRESETS", False)
7
+ IMGPROXY_ARGUMENTS_SEPARATOR = getattr(settings, "IMGPROXY_ARGUMENTS_SEPARATOR", ":")
8
+ IMGPROXY_KEY = bytes.fromhex(getattr(settings, "IMGPROXY_KEY", ""))
9
+ IMGPROXY_SALT = bytes.fromhex(getattr(settings, "IMGPROXY_SALT", ""))
10
+ IMGPROXY_SIGN_URL = bool(IMGPROXY_KEY) and bool(IMGPROXY_SALT)
11
+ IMGPROXY_NO_SIGNATURE_SEGMENT = getattr(
12
+ settings, "IMGPROXY_NO_SIGNATURE_SEGMENT", "unsafe"
13
+ )
14
+ IMGPROXY_PERCENT_ENCODED_PROCESSING_OPTIONS = getattr(
15
+ settings, "IMGPROXY_PERCENT_ENCODED_PROCESSING_OPTIONS", {"filename", "fn"}
16
+ )
17
+ IMGPROXY_SOURCE_URL_PREFIX = getattr(settings, "IMGPROXY_SOURCE_URL_PREFIX", "")
18
+ IMGPROXY_PERCENT_ENCODE_SOURCE_URL = getattr(
19
+ settings, "IMGPROXY_PERCENT_ENCODE_SOURCE_URL", True
20
+ )
21
+ IMGPROXY_PERCENT_ENCODE_SOURCE_URL_SAFE_CHARACTERS = "/"
22
+ IMGPROXY_BASE64_ENCODE_SOURCE_URL = getattr(
23
+ settings, "IMGPROXY_BASE64_ENCODE_SOURCE_URL", False
24
+ )
25
+ IMGPROXY_IMAGEFIELD_URL_TTL = getattr(settings, "IMGPROXY_IMAGEFIELD_URL_TTL", 0)
@@ -0,0 +1,6 @@
1
+ from django import template
2
+
3
+ from django_imgproxy import imgproxy
4
+
5
+ register = template.Library()
6
+ register.simple_tag(imgproxy)