Fix broken reference deletion
This commit is contained in:
parent
090e29f9d9
commit
1867187635
2 changed files with 38 additions and 11 deletions
|
|
@ -247,29 +247,38 @@ export async function cleanupBrokenReferences(publicIds: string[]): Promise<{
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const media of mediaToClean) {
|
for (const media of mediaToClean) {
|
||||||
let updated = false
|
let shouldDelete = false
|
||||||
const updates: any = {}
|
let updateThumbnail = false
|
||||||
|
|
||||||
|
// Check if the main URL is broken
|
||||||
if (media.url?.includes('cloudinary.com')) {
|
if (media.url?.includes('cloudinary.com')) {
|
||||||
const publicId = extractPublicId(media.url)
|
const publicId = extractPublicId(media.url)
|
||||||
if (publicId && publicIds.includes(publicId)) {
|
if (publicId && publicIds.includes(publicId)) {
|
||||||
updates.url = null
|
// If the main URL is broken, we need to delete the entire record
|
||||||
updated = true
|
// 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)
|
const publicId = extractPublicId(media.thumbnailUrl)
|
||||||
if (publicId && publicIds.includes(publicId)) {
|
if (publicId && publicIds.includes(publicId)) {
|
||||||
updates.thumbnailUrl = null
|
updateThumbnail = true
|
||||||
updated = 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({
|
await prisma.media.update({
|
||||||
where: { id: media.id },
|
where: { id: media.id },
|
||||||
data: updates
|
data: { thumbnailUrl: null }
|
||||||
})
|
})
|
||||||
results.cleanedMedia++
|
results.cleanedMedia++
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -465,8 +465,14 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="cleanup-confirmation">
|
<div class="cleanup-confirmation">
|
||||||
<p>Are you sure you want to clean up {auditData?.missingReferences.length || 0} broken references?</p>
|
<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 class="warning">⚠️ This will:</p>
|
||||||
<p>This action cannot be undone.</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>
|
||||||
<div class="modal-actions">
|
<div class="modal-actions">
|
||||||
<Button variant="secondary" onclick={() => {
|
<Button variant="secondary" onclick={() => {
|
||||||
|
|
@ -888,6 +894,18 @@
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
margin: 1rem 0;
|
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 {
|
.modal-header {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue