Skip to content

Commit

Permalink
#29209 include in 23.10.24 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Aug 14, 2024
1 parent b76ef2c commit 3e48f50
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 51 deletions.
3 changes: 2 additions & 1 deletion dotCMS/hotfix_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ This maintenance release includes the following code fixes:
133. https://github.com/dotCMS/core/issues/26546 : Enable better logging for getPageByPath in HTMLPageAssetAPIImpl.java #26546
134. https://github.com/dotCMS/core/issues/28366 : Uploaded images in another language than the default one do not inherit permissions #28366
135. https://github.com/dotCMS/core/issues/28838 : Category Child Permissions Not Loading #28838
136. https://github.com/dotCMS/core/issues/29079 : fileAsset Required Error while importing FileAsset through CSV #29079
136. https://github.com/dotCMS/core/issues/29079 : fileAsset Required Error while importing FileAsset through CSV #29079
137. https://github.com/dotCMS/core/issues/29209 : Wrong url when exporting file asset #29209
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dotcms.api.system.event.message.SystemMessageEventUtil;
import com.dotcms.api.system.event.message.builder.SystemMessage;
import com.dotcms.api.system.event.message.builder.SystemMessageBuilder;
import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotcms.business.CloseDBIfOpened;
import com.dotcms.contenttype.business.ContentTypeAPI;
import com.dotcms.contenttype.model.field.*;
Expand Down Expand Up @@ -68,6 +69,7 @@
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.portlets.contentlet.model.ResourceLink;
import com.dotmarketing.portlets.contentlet.struts.ContentletForm;
import com.dotmarketing.portlets.contentlet.struts.EventAwareContentletForm;
import com.dotmarketing.portlets.htmlpageasset.model.IHTMLPage;
Expand Down Expand Up @@ -327,30 +329,7 @@ public void processAction(ActionMapping mapping, ActionForm form, PortletConfig
_editWebAsset(req, res, config, form, user);

} catch (Exception ae) {
if ((referer != null) && (referer.length() != 0)) {
if (null != ae.getMessage() && ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) {
// The web asset edit threw an exception because it's
// locked so it should redirect back with message
java.util.Map<String, String[]> params = new java.util.HashMap<>();
params.put("struts_action", new String[] { "/ext/director/direct" });
params.put("cmd", new String[] { "editContentlet" });
params.put("contentlet", new String[] { req.getParameter("inode") });
params.put("container",
new String[] { (req.getParameter("contentcontainer_inode") != null) ? req
.getParameter("contentcontainer_inode") : "0" });
params.put("htmlPage", new String[] { (req.getParameter("htmlpage_inode") != null) ? req
.getParameter("htmlpage_inode") : "0" });
params.put("referer", new String[] { java.net.URLEncoder.encode(referer, "UTF-8") });

String directorURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq,
WindowState.MAXIMIZED.toString(), params);

_sendToReferral(req, res, directorURL);
} else {
_handleException(ae, req);
}
} else
_handleException(ae, req);
handleEditAssetException(ae, req, res, referer, httpReq);
return;
}
}
Expand All @@ -368,32 +347,7 @@ public void processAction(ActionMapping mapping, ActionForm form, PortletConfig
_newContent(req, res, config, form, user);

} catch (Exception ae) {
if ((referer != null) && (referer.length() != 0)) {
if (null != ae.getMessage() && ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) {
// The web asset edit threw an exception because it's
// locked so it should redirect back with message
java.util.Map<String, String[]> params = new java.util.HashMap<>();
params.put("struts_action", new String[] { "/ext/director/direct" });
params.put("cmd", new String[] { "editContentlet" });
params.put("contentlet", new String[] { req.getParameter("inode") });
params.put("container",
new String[] { (req.getParameter("contentcontainer_inode") != null) ? req
.getParameter("contentcontainer_inode") : "0" });
params.put("htmlPage", new String[] { (req.getParameter("htmlpage_inode") != null) ? req
.getParameter("htmlpage_inode") : "0" });
params.put("referer", new String[] { java.net.URLEncoder.encode(referer, "UTF-8") });

String directorURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq,
WindowState.MAXIMIZED.toString(), params);

_sendToReferral(req, res, directorURL);
} else if (null != ae.getMessage() && ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) {
_sendToReferral(req, res, referer);
} else {
_handleException(ae, req);
}
} else
_handleException(ae, req);
handleEditAssetException(ae, req, res, referer, httpReq);
return;
}
}
Expand Down Expand Up @@ -2031,6 +1985,10 @@ public void downloadToExcel(final HttpServletResponse response, final User user,
if (null != id && UtilMethods.isSet(id.getId())) {
text = id.getPath();
}
} else if (field instanceof BinaryField){
final HttpServletRequest request = HttpServletRequestThreadLocal.INSTANCE.getRequest();
final String fileLink = new ResourceLink.ResourceLinkBuilder().getFileLink(request, user, content, field.variable());
text = fileLink;
} else{
if (value instanceof Date || value instanceof Timestamp) {
if (field instanceof DateField) {
Expand Down Expand Up @@ -2093,6 +2051,31 @@ public void downloadToExcel(final HttpServletResponse response, final User user,
}
}

private void handleEditAssetException(Exception ae, ActionRequest req, ActionResponse res, String referer, HttpServletRequest httpReq) throws Exception {
if ((referer != null) && (referer.length() != 0)) {
if (ae.getMessage() != null && ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) {
// The web asset edit threw an exception because it's locked so it should redirect back with message
java.util.Map<String, String[]> params = new java.util.HashMap<>();
params.put("struts_action", new String[]{"/ext/director/direct"});
params.put("cmd", new String[]{"editContentlet"});
params.put("contentlet", new String[]{req.getParameter("inode")});
params.put("container", new String[]{(req.getParameter("contentcontainer_inode") != null) ? req.getParameter("contentcontainer_inode") : "0"});
params.put("htmlPage", new String[]{(req.getParameter("htmlpage_inode") != null) ? req.getParameter("htmlpage_inode") : "0"});
params.put("referer", new String[]{java.net.URLEncoder.encode(referer, "UTF-8")});

String directorURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq, WindowState.MAXIMIZED.toString(), params);

_sendToReferral(req, res, directorURL);
} else if (ae.getMessage() != null && ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) {
_sendToReferral(req, res, referer);
} else {
_handleException(ae, req);
}
} else {
_handleException(ae, req);
}
}

/**
* Builds the list of related records separated by comma
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ boolean isDownloadPermissionBasedRestricted(final Contentlet contentlet, final U
return ResourceLink.isDownloadPermissionBasedRestricted(contentlet, user);
}

/**
* Generates the url for a file asset based on specific structure FileLink, which is the host with its prefix + the path with the id + language id
* This method is aiming to be used when exporting, so the binary field could have a valid url when imported.
*
* @param request
* @param user
* @param contentlet
* @param field
*
* @return the url (File Link) for the file asset
*/
public String getFileLink(final HttpServletRequest request, final User user, final Contentlet contentlet, final String field) throws DotDataException, DotSecurityException {

ResourceLink link = this.build(request, user, contentlet, field);
final Host host = getHost(contentlet.getHost(), user);

final StringBuilder hostUrlBuilder = new StringBuilder(request.isSecure()? HTTPS_PREFIX:HTTP_PREFIX);

hostUrlBuilder.append(host.getHostname());
hostUrlBuilder.append(link.getConfiguredImageURL());
return hostUrlBuilder.toString();
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,44 @@ public void test_new_contentlet_no_inode() throws Exception{

}

/**
* Method to test: Test the resource link when the inode is null
* Given Scenario: When executing getFileLink method, an url should be returned
* ExpectedResult: Expected an url with the same structure as FileLink resource url that is showed in the info button of a content
*
*/
@Test
public void test_ResourceLink_get_fileLink() throws Exception{

final String mimeType = "text/html";
final String hostName = "demo.dotcms.com";
final long languageId = 1L;
final boolean isSecure = false;

final File file = FileUtil.createTemporaryFile("comments-list", "html", "This is a test temporal file");

final String htmlFileName = file.getName() + "&languageId=1";
final User adminUser = mockAdminUser();

final Contentlet contentlet = mock(Contentlet.class);
when(contentlet.getContentType()).thenReturn(mockFileAssetContentType());
when(contentlet.getIdentifier()).thenReturn(UUIDGenerator.generateUuid());
when(contentlet.getInode()).thenReturn(UUIDGenerator.generateUuid());
when(contentlet.isFileAsset()).thenReturn(true);
when(contentlet.getStringProperty(FileAssetAPI.FILE_NAME_FIELD)).thenReturn(htmlFileName);
when(contentlet.getLanguageId()).thenReturn(languageId);
when(contentlet.isNew()).thenReturn(false);
when(contentlet.getBinaryMetadata(FileAssetAPI.BINARY_FIELD)).thenReturn(mockMetadata(file));

final HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getAttribute(ResourceLink.HOST_REQUEST_ATTRIBUTE)).thenReturn(HOST_ID);
when(request.isSecure()).thenReturn(isSecure);
when(request.getServerPort()).thenReturn(80);

final ResourceLinkBuilder resourceLinkBuilder = getResourceLinkBuilder(hostName, null, mimeType, htmlFileName);
final String link = resourceLinkBuilder.getFileLink(request, adminUser, contentlet, "fileAsset");

assertEquals("http://demo.dotcms.com/dA/"+ contentlet.getInode() + "/" + htmlFileName,link);

}
}

0 comments on commit 3e48f50

Please sign in to comment.