Getting web content image in Liferay 7.2.1 DXP

Rafał Pydyniak, modified 5 Years ago. New Member Posts: 16 Join Date: 6/24/20 Recent Posts
Hello,
I have an issue after migrating part of my project to Liferay 7.2.1 DXP from 7.0.6. Basically we used to get the web content image path using following code (more or less)emoticonocument myDocument = SAXReaderUtil.read(article.getContentByLocale(locale.toString()));
Node imageField = NodeContent.getSingleNode(document, "Image");
NodeContent.getStringContentValue(imageField)

And it worked fine but unfortunately it doesn't in 7.2.1 DXP. After trying to figure out what's going on I found that the XML looks different for images in web contents in Liferay 7 and Liferay 7.2.
Basically in Liferay 7 I have something like:
        <dynamic-content language-id="en_US" alt="" name="XXX.png" title="XXX.png" type="document" [url=http://fileEntryId="12345"><![CDATA[/documents/111/222/XXX.png/QQQQQQ-WWWW-EEEE-CCCC-DDDDDDD?t=1588696601862]]></dynamic-content>
And in Liferay 7.2.1 I have:
        <dynamic-content [url=http://language-id="en_US"><![CDATA[{"groupId":"126435","alt":"","name":"XXX.png","title":"XXX.png","type":"journal","uuid":"QQQQ-WWW-EEE-CCC-ec48068bbf39","fileEntryId":"716022","resourcePrimKey":"716460"}]]></dynamic-content>
So before the image source was there and that's why my code worked. Now instead of the source I have JSON with informations about the image.
I guess it might have changed because of the adaptive images but anyway I'd like to know how can I get the image source from the code? I guess I could parse the JSON and fetch the image based on the informations there but is there any better way? Parsing JSON String to get values for getting image path seems a little bit complicated comparing to an old way
Best regards
thumbnail
Krzysztof Gołębiowski, modified 5 Years ago. Liferay Master Posts: 549 Join Date: 6/25/11 Recent Posts
Hi Rafał,First of all, it's good to see a new Liferay face from Poland emoticon

They changed the way of storing document data in Web Contents at some point, I think now it's actually better because it gives you more data about stored file right in the template context. I retrieved the image from Web Content in one of the FTL templates in the following way:

&lt;#assign JSONFactoryUtil = staticUtil["com.liferay.portal.kernel.json.JSONFactoryUtil"] /&gt;
&lt;#assign DLAppLocalServiceUtil = staticUtil["com.liferay.document.library.kernel.service.DLAppLocalServiceUtil"]&gt;
&lt;#assign DLUtil = staticUtil["com.liferay.document.library.kernel.util.DLUtil"] /&gt;

...

&lt;#assign jsonDlFileEntryAsString = docXml.valueOf("//dynamic-element[@name='library_document']/dynamic-content/text()") /&gt;
&lt;#assign jsonDlFileEntry = JSONFactoryUtil.createJSONObject(jsonDlFileEntryAsString) /&gt;
&lt;#assign fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonDlFileEntry.getString("uuid"),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonDlFileEntry.getString("groupId")?number) &gt;

&lt;#assign viewURL = DLUtil.getDownloadURL(fileEntry, fileEntry.getLatestFileVersion(true), themeDisplay, "") /&gt;

It is easily translatable to java, as it does not use any freemarker-specific features, only regular Java stuff. All the URL generation magic happens in getDownloadURL method.

KG
Rafał Pydyniak, modified 5 Years ago. New Member Posts: 16 Join Date: 6/24/20 Recent Posts
Hello,
haha yes, Liferay is not that popular in Poland but I can see it gets some popularity recently (I myself use it for almost four years now).
Thank you for the answer. I just found a blog article there https://liferay.dev/blogs/-/blogs/oh-no-my-urls-disappeared-and-how-to-get-them-back- (even with your comments inside with which I totally agree!) and just came back to this thread to post this as an answer
Thank you anyway
thumbnail
Krzysztof Gołębiowski, modified 5 Years ago. Liferay Master Posts: 549 Join Date: 6/25/11 Recent Posts
Ah, I completely forgot about that article. Then it's much better to follow Pavel's advice as he's a Liferay employee and he actually worked on that matter.