新增 阿拉伯语 和 俄语
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { existsSync } from 'node:fs'
|
||||
import { mkdir, unlink, writeFile } from 'node:fs/promises'
|
||||
import { extname, join } from 'node:path'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
const ALLOWED_EXT = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.avif'])
|
||||
const ALLOWED_MIME = new Set([
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'image/webp',
|
||||
'image/avif',
|
||||
'application/octet-stream',
|
||||
])
|
||||
|
||||
export const MAX_IMAGE_SIZE = 5 * 1024 * 1024
|
||||
export const UPLOAD_THUMBNAIL_PREFIX = '/images/uploads/'
|
||||
|
||||
export function resolvePublicDir(): string {
|
||||
// 与 .output 分离,避免重新部署时覆盖构建产物导致上传文件丢失
|
||||
return join(process.cwd(), 'public')
|
||||
}
|
||||
|
||||
export function isAllowedImage(filename: string, mime?: string): boolean {
|
||||
const ext = extname(filename).toLowerCase()
|
||||
if (!ALLOWED_EXT.has(ext)) return false
|
||||
if (!mime) return true
|
||||
return ALLOWED_MIME.has(mime.toLowerCase())
|
||||
}
|
||||
|
||||
export async function saveUploadedImage(data: Buffer, originalName: string): Promise<string> {
|
||||
const publicDir = resolvePublicDir()
|
||||
|
||||
const ext = extname(originalName).toLowerCase()
|
||||
const uploadDir = join(publicDir, 'images', 'uploads')
|
||||
await mkdir(uploadDir, { recursive: true })
|
||||
|
||||
const filename = `${Date.now()}-${randomUUID().slice(0, 8)}${ext}`
|
||||
await writeFile(join(uploadDir, filename), data)
|
||||
|
||||
return `/images/uploads/${filename}`
|
||||
}
|
||||
|
||||
export function isManagedUploadThumbnail(url?: string | null): boolean {
|
||||
if (!url) return false
|
||||
|
||||
const normalized = url.trim().replace(/\\/g, '/')
|
||||
if (!normalized.startsWith(UPLOAD_THUMBNAIL_PREFIX)) return false
|
||||
|
||||
const relative = normalized.slice(UPLOAD_THUMBNAIL_PREFIX.length)
|
||||
return Boolean(relative) && !relative.includes('..')
|
||||
}
|
||||
|
||||
export function resolveUploadThumbnailPaths(url: string): string[] {
|
||||
if (!isManagedUploadThumbnail(url)) return []
|
||||
|
||||
const relative = url.trim().replace(/\\/g, '/').slice(UPLOAD_THUMBNAIL_PREFIX.length)
|
||||
if (!relative || relative.includes('..')) return []
|
||||
|
||||
const cwd = process.cwd()
|
||||
return [
|
||||
join(cwd, 'public', 'images', 'uploads', relative),
|
||||
join(cwd, '.output', 'public', 'images', 'uploads', relative),
|
||||
]
|
||||
}
|
||||
|
||||
export function resolveExistingUploadThumbnailPath(url: string): string | null {
|
||||
return resolveUploadThumbnailPaths(url).find((filePath) => existsSync(filePath)) ?? null
|
||||
}
|
||||
|
||||
/** @deprecated 使用 resolveExistingUploadThumbnailPath */
|
||||
export function resolveUploadThumbnailPath(url: string): string | null {
|
||||
return resolveExistingUploadThumbnailPath(url)
|
||||
}
|
||||
|
||||
/** 删除后台上传的缩略图;失败时静默忽略,不影响主流程 */
|
||||
export async function tryDeleteUploadedThumbnail(url?: string | null): Promise<void> {
|
||||
if (!url) return
|
||||
|
||||
try {
|
||||
for (const filePath of resolveUploadThumbnailPaths(url)) {
|
||||
if (!existsSync(filePath)) continue
|
||||
await unlink(filePath)
|
||||
}
|
||||
} catch {
|
||||
// 缩略图清理失败不应阻断内容删除
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user