23 lines
735 B
TypeScript
23 lines
735 B
TypeScript
|
|
import { mkdir, writeFile, unlink } from 'fs/promises'
|
||
|
|
import path from 'path'
|
||
|
|
|
||
|
|
const UPLOAD_DIR = path.join(process.cwd(), 'public', 'uploads')
|
||
|
|
|
||
|
|
export async function saveImage(
|
||
|
|
productId: string,
|
||
|
|
buffer: Buffer,
|
||
|
|
originalFilename: string
|
||
|
|
): Promise<string> {
|
||
|
|
const dir = path.join(UPLOAD_DIR, productId)
|
||
|
|
await mkdir(dir, { recursive: true })
|
||
|
|
const safeName = originalFilename.replace(/[^a-z0-9._-]/gi, '_').toLowerCase()
|
||
|
|
const filename = `${Date.now()}-${safeName}`
|
||
|
|
await writeFile(path.join(dir, filename), buffer)
|
||
|
|
return `/uploads/${productId}/${filename}`
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteImageFile(url: string): Promise<void> {
|
||
|
|
const filePath = path.join(process.cwd(), 'public', url)
|
||
|
|
await unlink(filePath)
|
||
|
|
}
|