Touch UI Validate Asset file names on Upload | Community
Skip to main content
Level 2
July 14, 2023
Solved

Touch UI Validate Asset file names on Upload

  • July 14, 2023
  • 3 replies
  • 1716 views

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.

http://experience-aem.blogspot.com/2018/11/aem-6420-touch-ui-validate-asset-file-names-on-upload.html

 

Please could anyone suggest how can i do that.

 

 

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tanika02

@user00181  - 

 

Develop a custom workflow process in AEM that intercepts the asset upload operation and performs the file name validation.

 

  • Implement a custom Java class that represents the workflow process step responsible for file name validation. This class should extend the AbstractWorkflowProcess or AbstractAssetWorkflowProcess class provided by AEM.

@user00181  - https://sourcedcode.com/blog/aem/aem-granite-ui-1-0-form-components-xml-cheat-sheet-reference-guide#granite-ui-pathfield 

3 replies

Tanika02
Level 7
July 14, 2023

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(); } }

 

user00181Author
Level 2
July 14, 2023

How I can call this class at the time of assert upload in Dam.

Tanika02
Level 7
July 14, 2023

@user00181  - 

 

Develop a custom workflow process in AEM that intercepts the asset upload operation and performs the file name validation.

 

  • Implement a custom Java class that represents the workflow process step responsible for file name validation. This class should extend the AbstractWorkflowProcess or AbstractAssetWorkflowProcess class provided by AEM.
DPrakashRaj
Community Advisor
Community Advisor
July 14, 2023
ManviSharma
Adobe Employee
Adobe Employee
July 14, 2023

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.