useCwaResourceUpload
useCwaResourceUpload uploads, replaces and deletes the file in one uploadable field of the selected resource. It is what <CwaUiFileUpload> uses, so most admin tabs never call it:
<template>
<CwaUiManagerTab>
<CwaUiFileUpload field="file" label="Image" accept="image/*" />
</CwaUiManagerTab>
</template>
With field, CwaUiFileUpload calls useCwaResourceUpload(iri, field, label) for the tab's resource. Call the composable yourself only to pass it options, or to build your own control, and hand the result to CwaUiFileUpload as :upload:
<template>
<CwaUiManagerTab>
<CwaUiFileUpload field="file" label="Download" :upload="upload" />
</CwaUiManagerTab>
</template>
<script setup lang="ts">
import { useCwaResourceManagerTab, useCwaResourceUpload } from '#imports'
const { exposeMeta, iri } = useCwaResourceManagerTab({ name: 'File' })
// A downloads field that must keep the original file
const upload = useCwaResourceUpload(iri, 'file', 'Download', { imageDownscale: { enabled: false } })
defineExpose(exposeMeta)
</script>
Keep field beside :upload: it tells the tab which property the control edits, so a refused save is shown on it.
Signature
useCwaResourceUpload(
iri: ComputedRef<string | undefined>,
filename: string = 'file',
fileDisplayType: string = 'File',
ops?: { imageDownscale?: Partial<ImageDownscaleOptions> }
)
Parameters
| Parameter | Type | Description |
|---|---|---|
iri | ComputedRef<string | undefined> | The reactive IRI ref from useCwaResourceManagerTab. It identifies which resource to upload to |
filename | string | The PHP entity property name, and media object key, of the file field. Default 'file'. Pass the real name if your entity uses another (e.g. 'image', 'document') |
fileDisplayType | string | The noun used in the delete confirmation, and the name shown for a stored file whose URL has none (e.g. 'Image', 'Document'). Default 'File'. CwaUiFileUpload passes its label |
ops.imageDownscale | Partial<ImageDownscaleOptions> | Overrides how large images are resized for this field only. See Large images are resized before upload |
Return Values
| Return | Type | Purpose |
|---|---|---|
file | ComputedRef<{ name, size, url?, mimeType? } | undefined> | The stored file, or the file chosen for a component not added yet. undefined when there is none |
fileExists | ComputedRef<boolean> | true when a file is stored or chosen |
state | ComputedRef<CwaFieldState> | The field's save state, as useCwaResourceModel reports it |
updating | Ref<boolean> | true while an upload or delete is in progress |
retry | () => Promise<void> | Sends the last failed upload or delete again |
reset | () => Promise<void> | Clears the field's error |
handleInputChangeFile | (file: File | undefined) => Promise<void> | Uploads a chosen file |
handleInputDeleteFile | () => Promise<void> | Deletes the file, after a confirmation |
How It Works
handleInputChangeFile — resizes the file first if it is a large image (see below), then sends it to {iri}/upload as multipart/form-data (not base64). Sets updating.value = true during the upload. On success the resource store updates automatically.
A component not added yet — the file isn't sent. It is kept in the browser with a preview, and file shows it. When the admin adds the component, the module creates it and then uploads each kept file in its own request. Add and publish publishes only once every file has uploaded; if one fails, the component stays a draft and the field offers Retry.
handleInputDeleteFile — for a stored file, shows a confirmation dialog, then sends PATCH {iri} with { [filename]: null }. The API deletes the stored file. A file only kept in the browser is dropped without asking.
Multiple File Fields
There's no multi-file wrapper. Add one CwaUiFileUpload per field, each with its own field:
<template>
<CwaUiManagerTab>
<CwaUiFileUpload field="poster" label="Poster" accept="image/*" />
<CwaUiFileUpload field="thumbnail" label="Thumbnail" accept="image/*" />
</CwaUiManagerTab>
</template>
Each field reads and writes its own mediaObjects[<property>] key, so two fields on the same resource never couple. If fields do appear to share a file, it's a resource-data issue (a lost _metadata.mediaObjects key), not this composable.
Large Images Are Resized Before Upload
A photo straight from a phone can be 12 to 48 megapixels. The API decodes the whole image to build its Imagine filters, and no layout needs an image that large. So useCwaResourceUpload resizes large images in the browser before it sends them. This is on by default and needs no setup.
With the defaults, a JPEG, PNG or WebP whose longest edge is over 2560 px, or whose area is over 20 megapixels, is resized to fit both limits. The aspect ratio is kept. The admin sees no notice when this happens.
Options
| Option | Default | Description |
|---|---|---|
enabled | true | false uploads every file unchanged |
thresholdEdge | 2560 | Resize when the longest edge is over this many pixels |
thresholdPixels | 20000000 | Resize when width × height is over this |
maxEdge | 2560 | Longest edge of the result |
maxPixels | 20000000 | Width × height of the result |
quality | 0.85 | Encode quality for JPEG and WebP. Browsers ignore it for PNG |
Set these for the whole app with cwa.upload.image in nuxt.config.ts, or for one field with the fourth argument:
// A downloads field that must keep the original file
const download = useCwaResourceUpload(iri, 'file', 'File', { imageDownscale: { enabled: false } })
// A hero image that may be larger
const hero = useCwaResourceUpload(iri, 'hero', 'Image', { imageDownscale: { maxEdge: 4096 } })
Each option is taken from the call first, then from cwa.upload.image, then from the built-in default. An option set to undefined does not override. So { maxEdge: 4096 } changes only maxEdge.
Thresholds and Targets Are Separate
The threshold options decide whether an image is resized. The max options decide what size it becomes. A file is resized when it is over either threshold, and the result fits both targets.
They default to the same values, so any image over the target is resized. Keeping them separate lets you leave images alone unless they are well over the target. For example, with thresholdEdge: 4000 and maxEdge: 2560, a 3500 px image is sent unchanged and a 5000 px image is resized to 2560 px.
An image that is over a threshold but already within the targets is never enlarged. It is sent unchanged. So with maxEdge: 4096 and the default thresholdEdge, a 3000 px image is sent unchanged.
The edge rule and the pixel rule catch different images. A 48 MP phone photo is over both. A 20000 × 1000 panorama is only 20 MP, so only the edge rule catches it. With the default 2560 px edge, the pixel limit can never apply, because 2560 × 2560 is 6.6 MP. It is there for an app that raises maxEdge.
maxEdge or maxPixels, check that the API still accepts the result. If you lower the API's limits, lower these too. See Large Images and PHP Memory.What Is Kept and What Is Not
- The format is kept. A JPEG stays a JPEG and a PNG stays a PNG, so transparency survives. The file name is kept too.
- The original is sent if resizing doesn't make it smaller. For example, a palette PNG can grow when it is re-encoded.
- SVG, GIF and animated WebP are never resized. Nor is any other type, such as AVIF or a PDF. Only JPEG, PNG and still WebP are resized.
- An upload never fails because of this. If the browser can't decode or encode the image, the original is sent.
- EXIF metadata is removed from resized images. The photo is rotated to match its EXIF orientation first. The new file has no EXIF, so GPS location and camera details from phone photos are not published. That is good for privacy. If you need that metadata, turn resizing off for the field. Images that are not resized keep their metadata.
The API's own limits still apply. They also protect uploads that don't come through the admin, such as a direct API request.