Skip to content

Commit

Permalink
fix: Fix featured image is deleted in case of creating new page - EXO…
Browse files Browse the repository at this point in the history
…-75508 - Meeds-io/meeds#2614 (#1224)

Prior to this change, when create new page from draft with featured
image and try to delete the draft after saving the new page, the
featured image is deleted with the draft, which should not be the case
when the featured image is linked to the original saved page, this due
to wrong condition handling on the `removeNoteFeaturedImage` function in
case of draft of new page.
Same issue when delete draft, we don't have a check whether its featured
image was associated to saved page or not which may cause non expected
file deletion.

This PR treats the case by updating the condition to not delete the
featured image in case of draft of new page, knowing that in case of
delete orphan drafts the featured image deletion is treated out of this
function we have updated that case by checking if its linked image was
associated to a saved page or not before deleting its related featured
Image file.
  • Loading branch information
hakermi authored Nov 26, 2024
1 parent 0c10b60 commit dd05532
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1641,8 +1641,8 @@ public void removeNoteFeaturedImage(Long noteId,
if (isDraft) {
DraftPage draftPage = getDraftNoteById(String.valueOf(noteId),
identityManager.getIdentity(String.valueOf(userIdentityId)).getRemoteId());
if (draftPage != null && draftPage.getTargetPageId() != null
&& isOriginalFeaturedImage(draftPage, getNoteByIdAndLang(Long.valueOf(draftPage.getTargetPageId()), lang))) {
if (draftPage != null && (draftPage.getTargetPageId() == null
|| isOriginalFeaturedImage(draftPage, getNoteByIdAndLang(Long.valueOf(draftPage.getTargetPageId()), lang)))) {
removeFeaturedImageFile = false;
}
note = draftPage;
Expand Down Expand Up @@ -1925,6 +1925,20 @@ private void deleteNoteMetadataProperties(Page note, String lang, String objectT
lang,
true,
Long.parseLong(identityManager.getOrCreateUserIdentity(note.getOwner()).getId()));
} else if (note.isDraftPage()) {
// When delete a draft of non-existing page
// check if its featured image was linked to a saved page
List<MetadataItem> metadataItems =
metadataService.getMetadataItemsByMetadataNameAndTypeAndObjectAndMetadataItemProperty(NOTES_METADATA_TYPE.getName(),
NOTES_METADATA_TYPE.getName(),
NOTE_METADATA_PAGE_OBJECT_TYPE,
FEATURED_IMAGE_ID,
featuredImageId,
0,
0);
if (metadataItems == null || metadataItems.isEmpty()) {
fileService.deleteFile(Long.parseLong(featuredImageId));
}
} else {
fileService.deleteFile(Long.parseLong(featuredImageId));
}
Expand Down

0 comments on commit dd05532

Please sign in to comment.