Expand my Community achievements bar.

AEM Cloud Service - Dynamic Media folder path length validation, prevent deep nesting | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

AEM Cloud Service - Dynamic Media folder path length validation, prevent deep nesting by Sreekanth Choudry Nalabotu

Abstract

Goal
AEM Cloud Version : 2021.2.4944.20210221T230729Z-210225 (Feb 21, 2021)

When the Scene7 sync path (including account name) is more than 255 chars, asset upload to AEM works fine, however sync to Scene7 fails with error in s7 job console Failed to insert to DB. This post is on providing folder path validation (240 chars) so users do not upload assets in a deep nested structure and corrupt the repo with DM sync failures...

Solution
1) Create a filter apps.experienceaem.assets.core.filters.FolderNestingCheck, to provide server side validation of folder path lengths...

package apps.experienceaem.assets.core.filters;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

@Component(
service = Filter.class,
immediate = true,
name = "Experience AEM folder nesting check",
property = {
Constants.SERVICE_RANKING + ":Integer=-99",
"sling.filter.scope=COMPONENT",
"sling.filter.pattern=((.*.initiateUpload.json)|(.*.createasset.html))",
}
)
public class FolderNestingCheck implements Filter {
private static Logger log = LoggerFactory.getLogger(FolderNestingCheck.class);

private static int S7_FOLDER_LENGTH_MAX = 240;
private static String CONTENT_DAM_PATH = "/content/dam";
private static String INITIATE_UPLOAD_JSON = ".initiateUpload.json";
private static String CREATE_ASSET_HTML = ".createasset.html";

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try{
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response;

String uri = slingRequest.getRequestURI();

if(!uri.endsWith(INITIATE_UPLOAD_JSON) && !uri.endsWith(CREATE_ASSET_HTML)){
chain.doFilter(request, response);
return;
}

if(uri.contains(CONTENT_DAM_PATH)){
String folderPath = uri.substring(uri.indexOf(CONTENT_DAM_PATH) + CONTENT_DAM_PATH.length());

if(folderPath.endsWith(INITIATE_UPLOAD_JSON)){
folderPath = folderPath.substring(0, folderPath.lastIndexOf(INITIATE_UPLOAD_JSON));
}else if(folderPath.endsWith(CREATE_ASSET_HTML)){
folderPath = folderPath.substring(0, folderPath.lastIndexOf(CREATE_ASSET_HTML));
}

if(folderPath.length() > S7_FOLDER_LENGTH_MAX){
log.info("Uploading to deep nested folders not allowed : " + uri);
slingResponse.sendError(SlingHttpServletResponse.SC_FORBIDDEN, "Uploading to deep nested folders not allowed: " + uri);
return;
}
}

chain.doFilter(request, response);
}catch(Exception e){
log.error("Error checking folder nesting", e);
}
}

@Override
public void destroy() {
}
}
2) For showing the validation alert in browser, create a clientlib /apps/eaem-cs-no-deep-nesting/clientlibs/no-deep-nesting with categories dam.gui.coral.fileupload

3) Add JS logic for validation in /apps/eaem-cs-no-deep-nesting/clientlibs/no-deep-nesting/deep-nesting-upload.js

Read Full Blog

AEM Cloud Service - Dynamic Media folder path length validation, prevent deep nesting

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies