Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Can not delete programmatically uploaded files (DLFileEntry)
Hi everybody.
In Liferay 7.3, I created a portlet with a simple MVCAction to upload files to Documents & Media. It is working fine, the files are uploaded successfully and available via the Documents & Media portlet. But it is not possible to move these files to the recycle bin via the Documents & Media portlet (or control panel section). I just receive an error message, but I can't see anything related within the logs.
But, if I rename the file, I'm able to delete it afterwards.
So, I'm quite sure I'm missing something, e.g., setting permissions. This is how my code looks like:
DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(user.getUserId(), groupId, groupId,
dlFileFolder.getFolderId(), fileName, mimeType, fileName, fileJson.toString(), "", 0, new HashMap<>(), submittedFile,
null, fileSize, serviceContext);
dlFileEntry = DLFileEntryLocalServiceUtil.updateStatus(user.getUserId(), dlFileEntry.getFileVersion().getFileVersionId(), WorkflowConstants.STATUS_APPROVED, serviceContext, new HashMap<>());
If any further information is needed, please feel free to ask. Thanks!
I vaguely recall seeing something like this with programmatically added blogs. It might be that you need to add these permissions to the serviceContext.
serviceContext.setAddGuestPermissions(true);
serviceContext.setAddGroupPermissions(true);
Thanks for your reply. setAddGroupPermissions was quite helpful, because this is also something I had to do (guest permissions I explicitely don't set), but it does not solve my current issue.
I set the log level for com.liferay to DEBUG (after setting it for com.liferay.document.library did not lead to further logs), and now there's a hint:
2022-05-10 13:46:10.702 DEBUG [http-nio-8080-exec-8][LiferayPortlet:125] com.liferay.asset.kernel.exception.NoSuchEntryException: No AssetEntry exists with the key {classNameId=20011, classPK=43621}
com.liferay.asset.kernel.exception.NoSuchEntryException: No AssetEntry exists with the key {classNameId=20011, classPK=43621}
at com.liferay.portlet.asset.service.persistence.impl.AssetEntryPersistenceImpl.findByC_C(AssetEntryPersistenceImpl.java:3609)
at com.liferay.portlet.asset.service.impl.AssetEntryLocalServiceImpl.updateVisible(AssetEntryLocalServiceImpl.java:978)
at jdk.internal.reflect.GeneratedMethodAccessor540.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:66)
at com.sun.proxy.$Proxy817.updateVisible(Unknown Source)
[...]
2022-05-10 13:46:10.918 DEBUG [http-nio-8080-exec-8][SessionErrors:153] Adding key com.liferay.asset.kernel.exception.NoSuchEntryException to portlet com_liferay_document_library_web_portlet_DLAdminPortlet_LAYOUT_1 with value com.li
feray.asset.kernel.exception.NoSuchEntryException: No AssetEntry exists with the key {classNameId=20011, classPK=43621}
com.liferay.asset.kernel.exception.NoSuchEntryException: No AssetEntry exists with the key {classNameId=20011, classPK=43621}
at com.liferay.portlet.asset.service.persistence.impl.AssetEntryPersistenceImpl.findByC_C(AssetEntryPersistenceImpl.java:3609)
at com.liferay.portlet.asset.service.impl.AssetEntryLocalServiceImpl.updateVisible(AssetEntryLocalServiceImpl.java:978)
at jdk.internal.reflect.GeneratedMethodAccessor540.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:66)
at com.sun.proxy.$Proxy817.updateVisible(Unknown Source)
Do I have to create an Asset related to my FileEntry as well? Shouldn't this be done by the DLFileEntryLocalServiceUtil automatically?
It should be done for you but that's ionstructive. Check the DLFileEntryLocalServiceImpl for the method you're calling. Fiorst make sure you're calling the right add method (that will add the asset entry), and that you're supplying everything the asset logic needs. That's why I keyed in on the service context, since I thought there was something opaque that you might not have added to it but that would be needed in the impl.
Also instructive is that the LocalService's update method that's called from the UI is fixing the entity for you by providing whatever call or information that you're missing from your attempt to add it.
Ok, after I found that log, I did some further research on adding files to documents and media. So, I figured out DLFileEntryLocalServiceUtil is the wrong (or deprecated?) way to do this.
Following this manual, I was able to implement the file upload without any Asset-related issues: https://help.liferay.com/hc/en-us/articles/360020197211-Creating-Files
Basically, I just have to use DLAppService:
@Reference
private DLAppService _dlAppService;
And this is how my upload code looks like:
FileEntry fileEntry =
_dlAppService.addFileEntry(groupId, dlFileFolder.getFolderId(), fileName, mimeType,
fileName, fileJson.toString(), "", submittedFile, serviceContext);
Ahh, very nice!
Powered by Liferay™