diff --git a/components/form.js b/components/form.js index 7deeb164..4c99b392 100644 --- a/components/form.js +++ b/components/form.js @@ -222,8 +222,12 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, setH preview - - console.log('icon click')} /> + { + console.log('image uploaded to', url) + }} + > + { + const img = new window.Image() + img.src = window.URL.createObjectURL(file) + img.onload = async () => { + let data + try { + ({ data } = await getSignedPOST({ + variables: { + type: file.type, + size: file.size, + width: img.width, + height: img.height + } + })) + } catch (e) { + toaster.danger(e.message || e.toString?.()) + return + } + + const form = new FormData() + Object.keys(data.getSignedPOST.fields).forEach(key => form.append(key, data.getSignedPOST.fields[key])) + form.append('Content-Type', file.type) + form.append('Cache-Control', 'max-age=31536000') + form.append('acl', 'public-read') + form.append('file', file) + + const res = await fetch(data.getSignedPOST.url, { + method: 'POST', + body: form + }) + + if (!res.ok) { + // TODO make sure this is actually a helpful error message and does not expose anything to the user we don't want + toaster.danger(res.statusText) + return + } + + const url = `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${data.getSignedPOST.fields.key}` + onSuccess?.(url) + } + }, [toaster, getSignedPOST]) + return ( <> t.replace('image/', '')).join(', ')}`) return } - onSelect?.(file) + s3Upload(file) // TODO find out if this is needed and if so, why (copied from components/upload.js) e.target.value = null }}