Modeling & Development MMH: MIME Type Validation

Feature
The MIME type validation allows for detecting the media type of an attachment and restricts its upload, if the MIME type is not allowed in the application.

In the Project Template, profile pictures can be uploaded. Hence the allowed media types are “image/png” and “image/jpeg”. For every other file types, which are added in the form, the validation will be triggered and an error will be displayed. On server-side the allowed types are enabled via one property.

Screenshot 2024-10-24 at 16.12.15

Implementation

:information_source: The full code will be available with the recording of the session on the elearning plattform.

The following code has been introduce in the backend. This provides the actual MIME type logic for validating if the attachment satisfies the allowed media types.

File: server/app/src/main/java/com/mgmtp/a12/template/server/event/AttachmentEventListener.java

package com.mgmtp.a12.template.server.event;

// ... imports

@Service
public class AttachmentEventListener {
    private final MimeTypeValidator mimeTypeValidator;

    public AttachmentEventListener(MimeTypeValidator mimeTypeValidator) {
        this.mimeTypeValidator = mimeTypeValidator;
    }

    @CommonDataServicesEventListener
    public void beforeCreate(ContentTypeDetectedEvent contentTypeDetectedEvent) {
        boolean hasName = (contentTypeDetectedEvent.getFilename() != null);

        //Assuming the event with null filename is a thumbnail, for which it is unnecessary to validate the MIME type.
        if (hasName) {
            mimeTypeValidator.validateMimeType(contentTypeDetectedEvent.getDetectedMimeType());
        }
    }
}

File: server/app/src/main/java/com/mgmtp/a12/template/server/attachment/MimeTypeValidator.java

package com.mgmtp.a12.template.server.attachment;

// ... imports

import java.util.List;

import static com.mgmtp.a12.dataservices.exception.ExceptionKeys.ATTACHMENT_INVALID_TYPE_ERROR_KEY;

@Component
public class MimeTypeValidator {
    @Value("${mgmtp.a12.template.server.attachment.allowedMimeTypes:*}")
    private List<String> allowedMimeTypes;

    public void validateMimeType(String mimeType) {
        String detectedMimeGroup = mimeType.substring(0, mimeType.indexOf('/') + 1) + '*';

        if (!allowedMimeTypes.contains("*") && !allowedMimeTypes.contains(detectedMimeGroup)
                && !allowedMimeTypes.contains(mimeType)) {
            throw new InvalidInputException(ATTACHMENT_INVALID_TYPE_ERROR_KEY, "Invalid MIME type.");
        }
    }
}

File: server/app/src/main/resources/config/application-shared.properties

# List of allowed MIME types for the attachment upload
mgmtp.a12.template.server.attachment.allowedMimeTypes=image/png,image/jpeg

The following code has been introduced in the frontend. This handles the localization keys to translate the text depending on the chosen locale in the application.

File: client/src/localization/keys.ts

...
export const RESOURCE_KEYS = {
    ...,
    error: {
        ...,
        attachment: {
            invalidType: ""
        }
    }
};
...

File: client/src/localization/keys.ts

...
export const RESOURCE_KEYS = {
    ...,
    error: {
        ...,
        attachment: {
            invalidType: ""
        }
    }
};
...

File: client/src/localization/resources/de_DE.ts

...
export const RESOURCE_KEYS = {
    ...,
    error: {
        ...,
        attachment: {
            invalidType: "Ungültiger MIME-Typ."
        }
    }
};
...

File: client/src/localization/resources/en_US.ts

...
export const RESOURCE_KEYS = {
    ...,
    error: {
        ...,
        attachment: {
            invalidType: "Invalid MIME type."
        }
    }
};
...

Pitfalls
:warning: At the moment the implementation is not compatible with the Excel Export feature of CDMs.
The following has to be modified, to work with Excel Export as well:

These properties have to be added.
File: server/app/src/main/resources/config/application-shared.properties

# Handling of MIME types detection with Data Services tools
mgmtp.a12.dataservices.attachments.mimeType.probeMimeType.enabled=true
mgmtp.a12.dataservices.contentstore.server.api.mimeType.trustExternalMi

This behaviour will be implemented and available with the Project Template version 202406.2.0 (2024.06-ext2).