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,65 @@
|
||||
import fs from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { fileExists, MEDIA_DIR, safeJoin, siteOrigin, WEB_PUBLIC_DIR } from './media-paths'
|
||||
|
||||
export type ResolvedLegacyUrl = {
|
||||
url: string
|
||||
/** Where the browser can load a preview from. */
|
||||
previewUrl: string
|
||||
/** Absolute file path on the server, if the link points to a local file. */
|
||||
localPath: string | null
|
||||
/** Human-readable location, e.g. "apps/web/public/hero/building.jpg". */
|
||||
where: string
|
||||
exists: boolean
|
||||
size: number | null
|
||||
remote: boolean
|
||||
}
|
||||
|
||||
const MEDIA_FILE_PREFIX = '/api/media/file/'
|
||||
|
||||
export async function resolveLegacyUrl(raw: string): Promise<ResolvedLegacyUrl | null> {
|
||||
const url = raw.trim()
|
||||
if (!url) return null
|
||||
|
||||
if (/^https?:\/\//i.test(url)) {
|
||||
return { url, previewUrl: url, localPath: null, where: url, exists: true, size: null, remote: true }
|
||||
}
|
||||
if (!url.startsWith('/')) return null
|
||||
|
||||
const clean = decodeURIComponent(url.split(/[?#]/)[0])
|
||||
let localPath: string | null
|
||||
let previewUrl: string
|
||||
if (clean.startsWith(MEDIA_FILE_PREFIX)) {
|
||||
localPath = safeJoin(MEDIA_DIR, clean.slice(MEDIA_FILE_PREFIX.length))
|
||||
previewUrl = url
|
||||
} else {
|
||||
localPath = safeJoin(WEB_PUBLIC_DIR, clean)
|
||||
previewUrl = `${siteOrigin()}${url}`
|
||||
}
|
||||
const exists = localPath ? await fileExists(localPath) : false
|
||||
const size = exists && localPath ? (await fs.stat(localPath)).size : null
|
||||
return {
|
||||
url,
|
||||
previewUrl,
|
||||
localPath,
|
||||
where: localPath ? path.relative(path.resolve(process.cwd(), '..', '..'), localPath) : url,
|
||||
exists,
|
||||
size,
|
||||
remote: false,
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_REMOTE_BYTES = 50 * 1024 * 1024
|
||||
|
||||
export async function fetchRemoteFile(url: string) {
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(20000), redirect: 'follow' })
|
||||
if (!res.ok) throw new Error(`Не удалось скачать файл (${res.status})`)
|
||||
const mimetype = (res.headers.get('content-type') || '').split(';')[0].trim()
|
||||
if (!/^(image|video)\//.test(mimetype)) throw new Error('По ссылке не картинка и не видео')
|
||||
const length = Number(res.headers.get('content-length') || 0)
|
||||
if (length > MAX_REMOTE_BYTES) throw new Error('Файл больше 50 МБ')
|
||||
const data = Buffer.from(await res.arrayBuffer())
|
||||
if (data.length > MAX_REMOTE_BYTES) throw new Error('Файл больше 50 МБ')
|
||||
const name = decodeURIComponent(new URL(url).pathname.split('/').pop() || 'image') || 'image'
|
||||
return { data, mimetype, name, size: data.length }
|
||||
}
|
||||
Reference in New Issue
Block a user