contentLib.removeAttachment is not removing attachments

Enonic version: 6.15.8

Hi!
I’m trying to remove an attachment from a content with the contentLib.removeAttachment-function.
The content look something like this:

const myContent = {
  "_id": "a20fec05-089e-4462-bbb8-280f84b79535",
  "_name": "xxx",
  "_path": "/test-site/xxx",
  "creator": "user:system:su",
  "modifier": "user:system:su",
  "createdTime": "2020-02-25T08:59:56.705Z",
  "modifiedTime": "2020-02-25T09:12:43.360Z",
  "owner": "user:system:su",
  "type": "no.project.name:YYY",
  "displayName": "XXX",
  "hasChildren": false,
  "language": "en",
  "valid": true,
  "data": {
    "media": {
      "attachment": "imageName.jpg",
      "focalPoint": {
        "x": 0.5,
        "y": 0.5
      }
    }
  },
  "x": {},
  "page": {},
  "attachments": {
    "imageName.jpg": {
      "name": "imageName.jpg",
      "label": "source",
      "size": 1573931,
      "mimeType": "image/jpeg"
    }
  },
  "publish": {
    "from": "2020-02-25T09:01:19.267Z",
    "first": "2020-02-25T09:01:19.267Z"
  }
}

And I’m trying to remove it with:

contentLib.removeAttachment({ key: myContent._id, name: myContent.data.media.attachment });

I’m doing this based on the documentation here: https://repo.enonic.com/public/com/enonic/xp/docs/6.15.8/docs-6.15.8-libdoc.zip!/module-content.html#.removeAttachment

Is this a bug, or am I doing something wrong? :slight_smile:

We have tested this on 7.2 and it looks like the binary attachment gets deleted but attachment property is not removed from data. So in case of your content it will be like this after deletion:

{
  "_id": "a20fec05-089e-4462-bbb8-280f84b79535",
  ...
  "data": {
    "media": {
      "attachment": "imageName.jpg",
      "focalPoint": {
        "x": 0.5,
        "y": 0.5
      }
    }
  },
  "attachments": {}
}

I’m pretty sure it’s the same behaviour on 6.15.x as well.

So you can either run an additional update (with contentLib.modify) which will remove the data property, or wait for the bugfix. Here’s the bug we registered.

1 Like

So, then it works on 7.2, but when I run it in 6.15.8, the result is the same as if the function was not run at all:

{
  "_id": "a20fec05-089e-4462-bbb8-280f84b79535",
  ...
  "data": {
    "media": {
      "attachment": "imageName.jpg",
      "focalPoint": {
        "x": 0.5,
        "y": 0.5
      }
    }
  },
  "attachments": {
    "imageName.jpg": {
      "name": "imageName.jpg",
      "label": "source",
      "size": 1573931,
      "mimeType": "image/jpeg"
    }
  }
}

So the binary is not removed either. I can update the data value (with contentLib.modify), if I just can find a solution to removing the attachment.

Do you add an attachment with contentLib as well? Or via Content Studio?

Oh, I am not sure. Maybe it is because the content used to be Enonic CMS content, and now is used in Enonic XP after we converted it with your https://github.com/enonic/cms2xp ?

I noticed a new thing! After running reprocessing on the content with toolbox, it works the same way as you described for XP 7.2.

We tested it a bit more and currently behaviour is as follows:

  1. If you upload an attachment from Content Studio then it will create a binary attachment AND a data property. When you remove the attachment from CS, it will remove both the binary and the data property.
  2. If you upload an attachment via contentLib.addAttachment, it will only create a binary attachment. In this case attachment won’t be visible in Content Studio. Again, if you remove it via contentLib.removeAttachment it will only remove the binary. Visually nothing will change in CS because attachment wasn’t visible in the first place.

So, if you uploaded an attachment as described in 1 (or it was already there after migration), but remove it as described in 2, then data property will remain and the attachment will look as present in CS even though binary is removed by contentLib.

This is inconsistent and we’ll discuss how to improve this. Meanwhile, you can run a script (contentLib.modify) right after executing removeAttachment which will remove data property with the same name.

2 Likes

Thank you!

To summarize my initial problem: the content in question used to be Enonic CMS content, and was converted to Enonic XP content. This conversion did not handle the images properly, but by reprocessing the content with toolbox (with https://xp.readthedocs.io/en/stable/reference/toolbox/reprocess.html#reprocess) the contentLib.removeAttachment works as if the content was uploaded via contentLib.addAttachment(your case number 2). So, I’ll run contentLib.modify after removing the attachment to remove the data property as well.

1 Like

Not sure if this is directly inconsistent, N but it is exposing node api rather than content API. Content api should support upload of attatchments via the specific atrachment input