Expand my Community achievements bar.

SOLVED

Restricting asset based on file name pattern

Avatar

Level 2

We've a requirement to restrict the asset upload based on certain file name pattern on AEMaaCS. We tried using the fileupload.js illegal charset. It is not working. Custom clientlibs with the file upload validation that is also not working on cloud service. OOTB in Cloud we've validation for MIME type but there is not restriction on the file name validation. Can someone help if they've come across such issue and implementation any solution?

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Level 2

Everyone - We should update the list of the chars to be restricted by overlaying this file. /libs/dam/gui/coral/components/admin/clientlibs/damutil/js/util.js.

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @PrajwalRe ,

To configure Experience Manager to restrict users to upload files of specific MIME types:

  1. Navigate to Tools > Assets > Assets Configurations.

  2. Click Upload Restrictions.

  3. Click Add to define the allowed MIME types.

  4. Specify the MIME type in the text box. You can click Add again to specify more allowed MIME types. You can also click to delete to delete any MIME type from the list.

  5. Click Save.

Link to check out:
https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/assets/admin/con....


Thanks
Tarun

Avatar

Level 2

Thanks @TarunKumar  for the reply. We're looking to restrict the file patterns rather than mime types.

Avatar

Community Advisor

HI @PrajwalRe 

 

Create a custom workflow as below and it should work

 

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.Session;

@Component(
service = JobConsumer.class,
property = {
JobConsumer.PROPERTY_TOPICS + "=dam/event/asset"
}
)
public class AssetUploadRestrictionService implements JobConsumer {

@Reference
private ResourceResolverFactory resourceResolverFactory;

@Override
public JobResult process(Job job) {
try {
ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
Session session = resourceResolver.adaptTo(Session.class);

String path = (String) job.getProperty("path");
String name = session.getNode(path).getName();

if (!name.matches("your desired pattern")) {
// Move the asset to a quarantine folder or delete it
// session.getNode(path).remove();
// session.save();
}

return JobResult.OK;
} catch (Exception e) {
return JobResult.FAILED;
}
}
}

Avatar

Community Advisor

Hi, 

You can follow a similar approach to this: http://experience-aem.blogspot.com/2021/05/aem-cloud-service-configure-file-upload-max-size-per-asse... and update the code to check the name instead of the size of an asset.

Hope this helps



Esteban Bustamante

Avatar

Community Advisor

hi @PrajwalRe ,

If you need to restrict asset uploads based on certain file name patterns in Adobe Experience Manager as a Cloud Service (AEMaaCS), you might need to approach this differently since some traditional methods may not work in the cloud environment. Here's a suggestion on how you might achieve this:

  1. Custom Workflow Step: Implement a custom workflow step that checks the file name pattern of assets during the upload process. This custom workflow step can be triggered after the asset is uploaded but before it's persisted in the repository.

  2. Custom Workflow Process Step Implementation: Create a Java class that implements the custom workflow process step. In this class, you can access the uploaded asset and check its file name against the required pattern. If the file name matches the pattern, proceed with the workflow. Otherwise, halt the workflow and reject the asset upload.

  3. Workflow Configuration: Configure the workflow model to include your custom workflow step. This ensures that the custom step is executed whenever an asset is uploaded.

  4. Handling Rejections: Decide how you want to handle rejected uploads. You might want to send notifications to users or log the rejected uploads for review.

  5. Testing: Thoroughly test the custom workflow step to ensure that it correctly identifies assets with the specified file name patterns and rejects uploads as expected.

Here's a basic example of what the custom workflow step implementation might look like:

 

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobProcessor;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

@Component(
    service = JobProcessor.class,
    property = {
        JobProcessor.PROPERTY_TOPICS + "=" + "your/custom/workflow/step/topic"
    }
)
public class FileNamePatternValidationStep implements JobProcessor {

    @Override
    public boolean process(Job job) {
        // Extract asset information from the job
        String assetPath = (String) job.getProperty("assetPath");
        String fileName = (String) job.getProperty("fileName");

        // Check if the file name matches the required pattern
        if (!isValidFileName(fileName)) {
            // Reject the asset upload
            // You can log a message or send a notification here
            return false;
        }

        // Proceed with the workflow
        // You can perform additional actions here if needed
        return true;
    }

    private boolean isValidFileName(String fileName) {
        // Implement logic to check if the file name matches the required pattern
        // Return true if the file name is valid, false otherwise
    }
}

 

In this example, FileNamePatternValidationStep is a custom workflow step that implements the JobProcessor interface. It checks the file name of the uploaded asset against a specified pattern and rejects the upload if the file name does not match the pattern.

Remember to deploy and configure the custom workflow step properly in your AEMaaCS environment. Additionally, ensure that the necessary permissions are set up to execute the custom workflow step.

Avatar

Correct answer by
Level 2

Everyone - We should update the list of the chars to be restricted by overlaying this file. /libs/dam/gui/coral/components/admin/clientlibs/damutil/js/util.js.