feat(media): add admin media CRUD preview and storage cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises"
|
||||
import { mkdir, rm, writeFile } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
|
||||
import { buildMediaStorageKey } from "@/lib/media/storage-key"
|
||||
@@ -15,7 +15,7 @@ type StoredUpload = {
|
||||
storageKey: string
|
||||
}
|
||||
|
||||
function resolveBaseDirectory(): string {
|
||||
export function resolveLocalMediaBaseDirectory(): string {
|
||||
const configured = process.env.CMS_MEDIA_LOCAL_STORAGE_DIR?.trim()
|
||||
|
||||
if (configured) {
|
||||
@@ -33,7 +33,7 @@ export async function storeUploadLocally(params: StoreLocalUploadParams): Promis
|
||||
variant: params.variant,
|
||||
fileName: params.file.name,
|
||||
})
|
||||
const baseDirectory = resolveBaseDirectory()
|
||||
const baseDirectory = resolveLocalMediaBaseDirectory()
|
||||
const outputPath = path.join(baseDirectory, storageKey)
|
||||
|
||||
await mkdir(path.dirname(outputPath), { recursive: true })
|
||||
@@ -43,3 +43,24 @@ export async function storeUploadLocally(params: StoreLocalUploadParams): Promis
|
||||
|
||||
return { storageKey }
|
||||
}
|
||||
|
||||
export async function deleteLocalStorageObject(storageKey: string): Promise<boolean> {
|
||||
const baseDirectory = resolveLocalMediaBaseDirectory()
|
||||
const outputPath = path.join(baseDirectory, storageKey)
|
||||
|
||||
try {
|
||||
await rm(outputPath)
|
||||
return true
|
||||
} catch (error) {
|
||||
const code =
|
||||
typeof error === "object" && error !== null && "code" in error
|
||||
? String((error as { code?: unknown }).code)
|
||||
: ""
|
||||
|
||||
if (code === "ENOENT") {
|
||||
return false
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user