Uploads
File and image fields store their files through a fastapi-storages backend passed as
Admin(storage=...); see File Uploads for the column types and the
per-record download route. S3StorageBackend needs the s3 extra
(pip install 'hyper-admin[s3]'); the image helpers need Pillow.
| Name | What it does |
|---|---|
S3StorageBackend |
An S3-compatible storage backend configured per instance or from HYPERADMIN_AWS_* settings |
validate_upload |
Checks an UploadFile against allowed MIME types and a size limit; raises FileValidationError |
prepare_image_upload |
Rewrites an uploaded image upright when its EXIF orientation asks for it and returns its metadata; raises ImageTooLargeError above max_pixels |
extract_metadata |
Width, height, format and EXIF orientation of an image without decoding its pixels, or None for a non-image |
auto_rotate |
Applies the EXIF orientation to a Pillow image |
generate_thumbnail |
Makes a thumbnail of a stored image once and returns its name, or a placeholder icon when the file cannot be read |
API Reference
hyperadmin.uploads.S3StorageBackend
Bases: S3Storage
S3Storage configured per instance, for AWS S3 and S3-compatible services.
fastapi_storages.S3Storage reads its configuration from AWS_*
environment variables when the module is imported. This subclass takes the
same configuration as keyword arguments (or from settings via
:meth:from_settings), so two backends can point at different buckets and
nothing is read at import time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_name
|
str
|
The bucket objects are written to. |
required |
access_key_id
|
str | None
|
Static credentials. When unset, boto3's own credential
chain applies (profiles, instance and task roles, |
None
|
secret_access_key
|
str | None
|
See |
None
|
region
|
str | None
|
The region name ( |
None
|
endpoint_url
|
str | None
|
|
None
|
custom_domain
|
str | None
|
Serve object URLs from this host instead of the endpoint (a CDN or an R2 public bucket domain). URLs are then not signed. |
None
|
querystring_auth
|
bool
|
Sign object URLs (presigned, time-limited; default).
Set |
True
|
default_acl
|
str | None
|
ACL set on every written object ( |
None
|
client
|
Any
|
An existing boto3 S3 client to use instead of building one (tests, custom sessions). The other connection arguments are then only used to build URLs. |
None
|
Source code in src/hyperadmin/uploads/storage.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
delete(name)
Delete the object name from the bucket.
Source code in src/hyperadmin/uploads/storage.py
210 211 212 | |
from_settings(settings, *, client=None)
classmethod
Build a backend from the aws_* settings (HYPERADMIN_AWS_* / AWS_* env).
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in src/hyperadmin/uploads/storage.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
get_name(name)
Return the object key for name: the basename sanitised, directories kept.
Source code in src/hyperadmin/uploads/storage.py
169 170 171 172 173 | |
get_path(name)
Return the URL of the object name.
Served from custom_domain when set, otherwise presigned through the
client when querystring_auth is on (default), otherwise the plain
path-style URL under the endpoint.
Source code in src/hyperadmin/uploads/storage.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
get_size(name)
Return the object size in bytes (one HeadObject call).
Source code in src/hyperadmin/uploads/storage.py
190 191 192 193 | |
write(file, name)
Upload file as name and return the object key.
No ACL parameter is sent when default_acl is unset: boto3
rejects ACL=None, and services such as R2 reject any ACL at all.
Source code in src/hyperadmin/uploads/storage.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
hyperadmin.uploads.validate_upload(file, *, allowed_types=None, max_size=None)
Validate an uploaded file against size and MIME type constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
The uploaded file to validate. |
required |
allowed_types
|
list[str] | None
|
Allowed MIME types (e.g. |
None
|
max_size
|
int | None
|
Maximum file size in bytes. When |
None
|
Raises:
| Type | Description |
|---|---|
FileValidationError
|
If validation fails. |
Source code in src/hyperadmin/uploads/validation.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
hyperadmin.uploads.FileValidationError
Bases: ValueError
Raised when an uploaded file fails validation checks.
Source code in src/hyperadmin/uploads/validation.py
11 12 | |
hyperadmin.uploads.prepare_image_upload(upload, *, max_pixels=MAX_IMAGE_PIXELS)
Normalise an uploaded image in place before the storage column saves it.
This is the hook the form save path calls for ImageType fields. A
non-image upload is left untouched. An image whose EXIF orientation says
"rotate me" is re-encoded upright, in its original format and with the
rest of its EXIF block, straight back into upload.file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upload
|
UploadFile
|
The multipart upload; its |
required |
max_pixels
|
int
|
Decompression-bomb threshold (width x height). |
MAX_IMAGE_PIXELS
|
Returns:
| Type | Description |
|---|---|
ImageMetadata | None
|
The metadata of the file as it will be stored, or |
Raises:
| Type | Description |
|---|---|
ImageTooLargeError
|
The image is larger than |
Source code in src/hyperadmin/uploads/images.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
hyperadmin.uploads.ImageTooLargeError
Bases: FileValidationError
Raised when an image declares more pixels than max_pixels allows.
Source code in src/hyperadmin/uploads/images.py
56 57 | |
hyperadmin.uploads.extract_metadata(source, *, max_pixels=MAX_IMAGE_PIXELS)
Read dimensions, format and EXIF orientation without decoding pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path | IO[bytes] | UploadFile
|
A path, a binary stream or a Starlette |
required |
max_pixels
|
int
|
Decompression-bomb threshold (width x height). |
MAX_IMAGE_PIXELS
|
Returns:
| Type | Description |
|---|---|
ImageMetadata | None
|
The metadata, or |
Raises:
| Type | Description |
|---|---|
ImageTooLargeError
|
The image is larger than |
Source code in src/hyperadmin/uploads/images.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
hyperadmin.uploads.auto_rotate(image)
Return image transposed so its pixels match the EXIF orientation.
The returned image carries no orientation tag, so re-saving it will not rotate it twice. An image without an orientation tag is returned as a copy.
Source code in src/hyperadmin/uploads/images.py
154 155 156 157 158 159 160 161 | |
hyperadmin.uploads.generate_thumbnail(storage, filename, max_size=DEFAULT_THUMBNAIL_SIZE, *, max_pixels=MAX_IMAGE_PIXELS, placeholder=PLACEHOLDER_THUMBNAIL)
Return a thumbnail of filename that fits inside max_size, making it once.
The thumbnail is written under :data:THUMBNAIL_DIR next to the original,
auto-rotated, with its aspect ratio preserved and LANCZOS resampling. A
cached thumbnail is reused as long as it is not older than the original.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
Any
|
A |
required |
filename
|
str
|
The stored name of the original, as held by the file column. |
required |
max_size
|
tuple[int, int]
|
Bounding box |
DEFAULT_THUMBNAIL_SIZE
|
max_pixels
|
int
|
Decompression-bomb threshold for the original. |
MAX_IMAGE_PIXELS
|
placeholder
|
str
|
Value returned when no thumbnail can be produced. The default is a static icon path, not a storage name. |
PLACEHOLDER_THUMBNAIL
|
Returns:
| Type | Description |
|---|---|
str
|
The storage-relative thumbnail name, or |
str
|
original is missing, outside the storage root, not an image, or too large. |
Source code in src/hyperadmin/uploads/images.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |