Lock down access control and add image previews, file locations and cropping to the CMS.
Security: - only admins can assign user roles; profile updates are whitelisted - SBP payment confirmation requires an HMAC-signed webhook or an admin, and is idempotent - patient receipts and documents are private media owned by the patient, downloaded via the site - patients cannot change booking price or payment status, forge invoices or write for other accounts CMS images: - every upload field shows a large preview, focal point, server file path and public URL - crop/resize keeps the original in media/originals with a "restore original" action - legacy image URL fields show where the file really lives and can be moved into the media library - specialties get a visual icon picker (Untitled UI) and an optional custom icon upload Fixes: saving the cabinet profile, receipt PDF generation, uploaded doctor photos on the site. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
959e693dcc
commit
0b447e67bf
@@ -0,0 +1,132 @@
|
||||
'use client'
|
||||
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { toast, useField } from '@payloadcms/ui'
|
||||
import { formatBytes, MediaCard } from './media-ui/MediaCard'
|
||||
|
||||
type Resolved = {
|
||||
url: string
|
||||
previewUrl: string
|
||||
localPath: string | null
|
||||
where: string
|
||||
exists: boolean
|
||||
size: number | null
|
||||
remote: boolean
|
||||
}
|
||||
|
||||
const MIME_BY_EXT: Record<string, string> = {
|
||||
jpg: 'image/jpeg',
|
||||
jpeg: 'image/jpeg',
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
gif: 'image/gif',
|
||||
svg: 'image/svg+xml',
|
||||
avif: 'image/avif',
|
||||
mp4: 'video/mp4',
|
||||
webm: 'video/webm',
|
||||
mov: 'video/quicktime',
|
||||
}
|
||||
|
||||
function guessMime(url: string) {
|
||||
const ext = url.split(/[?#]/)[0].split('.').pop()?.toLowerCase() || ''
|
||||
return MIME_BY_EXT[ext] || 'image/jpeg'
|
||||
}
|
||||
|
||||
/**
|
||||
* Shown under legacy "URL фото" fields: preview of the linked file, where it really lives
|
||||
* (apps/web/public/... or the remote address) and a button that moves it into the media library
|
||||
* so it gets the normal preview and editor.
|
||||
*/
|
||||
export function LegacyUrlPreview({
|
||||
path,
|
||||
field,
|
||||
}: {
|
||||
path: string
|
||||
field?: { admin?: { custom?: { uploadField?: string } } }
|
||||
}) {
|
||||
const { value, setValue } = useField<string>({ path })
|
||||
const uploadName = field?.admin?.custom?.uploadField
|
||||
const uploadPath = uploadName ? path.replace(/[^.]+$/, uploadName) : ''
|
||||
const upload = useField<unknown>({ path: uploadPath || path })
|
||||
const [resolved, setResolved] = useState<Resolved | null>(null)
|
||||
const [error, setError] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const url = (value || '').trim()
|
||||
setResolved(null)
|
||||
setError('')
|
||||
if (!url) return
|
||||
const timer = setTimeout(async () => {
|
||||
const res = await fetch(`/api/custom/media-tools/resolve-url?url=${encodeURIComponent(url)}`, {
|
||||
credentials: 'include',
|
||||
})
|
||||
const data = await res.json().catch(() => ({}))
|
||||
if (res.ok) setResolved(data as Resolved)
|
||||
else setError(data.error || 'Не удалось разобрать ссылку')
|
||||
}, 300)
|
||||
return () => clearTimeout(timer)
|
||||
}, [value])
|
||||
|
||||
if (!value) return null
|
||||
if (error) return <p style={{ fontSize: 12, color: 'var(--theme-error-500)' }}>{error}</p>
|
||||
if (!resolved) return null
|
||||
|
||||
const where = resolved.remote ? 'Внешняя ссылка' : resolved.exists ? 'Где лежит файл' : 'Файл не найден'
|
||||
|
||||
return (
|
||||
<MediaCard
|
||||
info={{
|
||||
previewUrl: resolved.exists ? resolved.previewUrl : null,
|
||||
publicUrl: resolved.previewUrl,
|
||||
storagePath: null,
|
||||
mimeType: guessMime(resolved.url),
|
||||
filesize: resolved.size,
|
||||
}}
|
||||
actions={
|
||||
uploadPath ? (
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn--style-primary btn--size-small"
|
||||
style={{ margin: 0 }}
|
||||
disabled={busy || (!resolved.remote && !resolved.exists)}
|
||||
onClick={async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const res = await fetch('/api/custom/media-tools/import-url', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url: resolved.url }),
|
||||
})
|
||||
const data = await res.json().catch(() => ({}))
|
||||
if (!res.ok) throw new Error(data.error || 'Не удалось перенести файл')
|
||||
upload.setValue(data.doc.id)
|
||||
setValue('')
|
||||
toast.success('Файл перенесён в медиатеку. Сохраните документ.')
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : 'Ошибка')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}}
|
||||
>
|
||||
Перенести в медиатеку
|
||||
</button>
|
||||
) : null
|
||||
}
|
||||
notice={
|
||||
<div style={{ marginTop: 10, display: 'grid', gap: 4, fontSize: 12 }}>
|
||||
<span style={{ color: 'var(--theme-elevation-500)' }}>{where}</span>
|
||||
<code style={{ wordBreak: 'break-all' }}>
|
||||
{resolved.remote ? resolved.url : resolved.localPath || resolved.where}
|
||||
</code>
|
||||
{resolved.size ? <span style={{ color: 'var(--theme-elevation-500)' }}>{formatBytes(resolved.size)}</span> : null}
|
||||
<span style={{ color: 'var(--theme-elevation-500)' }}>
|
||||
Это старая ссылка на файл. Чтобы обрезать фото или поменять точку фокуса, перенесите его в медиатеку.
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user