Hi All,
I want to validate asset file names at the time of upload asset in dam.
When uploading new asset in DAM can there be a validation script which will only allow the supported (by AEM and Dynamic Media) characters within the file name. file name only allow these a-z A-Z - _ 0-9.
I have try one solution but that is not working.
Please could anyone suggest how can i do that.
Thanks.
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @user00181 -
You can write/customize the below script further as per your use-case :
package com.yourpackage;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.regex.Pattern;
public class CustomAssetUploadServlet extends SlingAllMethodsServlet implements Servlet {
private static final Pattern FILENAME_PATTERN = Pattern.compile("^[a-zA-Z0-9-_]+$");
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
// Get the uploaded file name
String fileName = request.getRequestPathInfo().getSuffix();
// Validate the file name against supported characters
if (!isValidFileName(fileName)) {
response.sendError(SlingHttpServletResponse.SC_BAD_REQUEST, "Invalid file name. File names can only contain a-z, A-Z, 0-9, hyphen (-), and underscore (_).");
return;
}
// Continue with the default asset upload logic
super.doPost(request, response);
}
private boolean isValidFileName(String fileName) {
return StringUtils.isNotBlank(fileName) && FILENAME_PATTERN.matcher(fileName).matches();
}
}
How I can call this class at the time of assert upload in Dam.
Develop a custom workflow process in AEM that intercepts the asset upload operation and performs the file name validation.
Hi,
o validate asset file names during upload in AEM DAM, develop a custom OSGi bundle implementing AssetUploadNameValidator interface. Override the validate method to check for unsupported characters. Register the bundle as an OSGi service and configure it in AEM's OSGi settings.
Views
Likes
Replies