UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

9 lines • 3.8 kB
/** * The account console's upload component: a same-origin script authkit loads (with the page's CSP * nonce) for a `component` block of provider `media-upload`. It adds a file picker to the mount * point, sends the file to the media upload endpoint with the block's ticket, then submits the * block's form with the new file's id (`field_media`) so authkit runs the `upload` action and shows * its message. Without JavaScript the block's fallback text stays. */ export declare const UPLOAD_SCRIPT = "(() => {\n 'use strict';\n const mounts = document.querySelectorAll('[data-block=\"component\"][data-provider=\"media-upload\"]');\n for (const mount of mounts) {\n if (mount.getAttribute('data-mounted') === '1') continue;\n mount.setAttribute('data-mounted', '1');\n let props;\n try {\n props = JSON.parse(document.getElementById(mount.getAttribute('data-props')).textContent);\n } catch (e) {\n continue;\n }\n const labels = props.labels || {};\n const form = mount.querySelector('form[data-component-form]');\n const fallback = mount.querySelector('[data-component-fallback]');\n if (fallback) fallback.hidden = true;\n const row = document.createElement('div');\n row.className = 'flex flex-wrap items-center gap-2';\n const input = document.createElement('input');\n input.type = 'file';\n input.className = 'text-sm text-gray-700';\n input.setAttribute('aria-label', labels.choose || 'File');\n if (props.accept) input.accept = props.accept;\n const button = document.createElement('button');\n button.type = 'button';\n button.className = 'rounded-md bg-gray-900 px-3 py-2 text-sm font-medium text-white';\n button.textContent = labels.upload || 'Upload';\n const status = document.createElement('p');\n status.className = 'mt-2 text-sm text-gray-500';\n status.setAttribute('role', 'status');\n row.appendChild(input);\n row.appendChild(button);\n mount.appendChild(row);\n mount.appendChild(status);\n button.addEventListener('click', async () => {\n const file = input.files && input.files[0];\n if (!file) {\n status.textContent = labels.pick || 'Choose a file first.';\n return;\n }\n if (props.maxBytes && file.size > props.maxBytes) {\n status.textContent = labels.tooLarge || 'This file is too large.';\n return;\n }\n button.disabled = true;\n status.textContent = labels.uploading || 'Uploading\u2026';\n const headers = {\n 'content-type': file.type || 'application/octet-stream',\n 'x-file-name': encodeURIComponent(file.name),\n 'x-media-upload-ticket': props.ticket,\n accept: 'application/json',\n };\n const csrf = form && form.querySelector('input[name=\"_csrf\"]');\n if (csrf && props.sameOrigin) headers['x-csrf-token'] = csrf.value;\n try {\n const res = await fetch(props.endpoint, {\n method: 'POST',\n headers,\n body: file,\n credentials: props.sameOrigin ? 'same-origin' : 'omit',\n });\n const body = await res.json().catch(() => ({}));\n if (!res.ok || !body.id) throw new Error(body.error || labels.failed || 'Upload failed.');\n if (!form) {\n window.location.reload();\n return;\n }\n const field = document.createElement('input');\n field.type = 'hidden';\n field.name = 'field_media';\n field.value = body.id;\n form.appendChild(field);\n form.submit();\n } catch (error) {\n button.disabled = false;\n status.textContent = error && error.message ? error.message : labels.failed || 'Upload failed.';\n }\n });\n }\n})();\n"; //# sourceMappingURL=upload_script.d.ts.map