Fix broken reference deletion

This commit is contained in:
Justin Edmund 2025-06-17 08:45:09 +01:00
parent 090e29f9d9
commit 1867187635
2 changed files with 38 additions and 11 deletions

View file

@ -247,29 +247,38 @@ export async function cleanupBrokenReferences(publicIds: string[]): Promise<{
})
for (const media of mediaToClean) {
let updated = false
const updates: any = {}
let shouldDelete = false
let updateThumbnail = false
// Check if the main URL is broken
if (media.url?.includes('cloudinary.com')) {
const publicId = extractPublicId(media.url)
if (publicId && publicIds.includes(publicId)) {
updates.url = null
updated = true
// If the main URL is broken, we need to delete the entire record
// since url is a required field
shouldDelete = true
}
}
if (media.thumbnailUrl?.includes('cloudinary.com')) {
// Check if only the thumbnail is broken
if (!shouldDelete && media.thumbnailUrl?.includes('cloudinary.com')) {
const publicId = extractPublicId(media.thumbnailUrl)
if (publicId && publicIds.includes(publicId)) {
updates.thumbnailUrl = null
updated = true
updateThumbnail = true
}
}
if (updated) {
if (shouldDelete) {
// Delete the media record entirely since the main URL is broken
await prisma.media.delete({
where: { id: media.id }
})
results.cleanedMedia++
} else if (updateThumbnail) {
// Only update the thumbnail to null if it's broken
await prisma.media.update({
where: { id: media.id },
data: updates
data: { thumbnailUrl: null }
})
results.cleanedMedia++
}

View file

@ -465,8 +465,14 @@
</div>
<div class="cleanup-confirmation">
<p>Are you sure you want to clean up {auditData?.missingReferences.length || 0} broken references?</p>
<p class="warning">⚠️ This will remove Cloudinary URLs from database records where the files no longer exist.</p>
<p>This action cannot be undone.</p>
<p class="warning">⚠️ This will:</p>
<ul class="cleanup-actions">
<li>Delete Media records where the main file no longer exists in Cloudinary</li>
<li>Remove broken thumbnail URLs from Media records</li>
<li>Remove broken image URLs from Projects and Posts</li>
<li>Remove broken images from galleries and attachments</li>
</ul>
<p class="warning">This action cannot be undone.</p>
</div>
<div class="modal-actions">
<Button variant="secondary" onclick={() => {
@ -888,6 +894,18 @@
font-weight: 500;
margin: 1rem 0;
}
.cleanup-actions {
margin: 0.75rem 0 0.75rem 1.5rem;
padding: 0;
list-style-type: disc;
color: $grey-30;
font-size: 0.875rem;
li {
margin: 0.25rem 0;
}
}
}
.modal-header {