Expand my Community achievements bar.

SOLVED

Touch UI Validate Asset file names on Upload

Avatar

Level 3

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.htm...

 

Please could anyone suggest how can i do that.

 

 

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
6 Replies

Avatar

Community Advisor

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

 

Avatar

Level 3

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

Avatar

Community Advisor

@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.

Avatar

Correct answer by
Community Advisor

Avatar

Employee Advisor

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.