|
| 1 | +package dev.dsf.bpe.v2.service; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.InputStream; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +public interface MimeTypeService |
| 10 | +{ |
| 11 | + Logger logger = LoggerFactory.getLogger(MimeTypeService.class); |
| 12 | + |
| 13 | + record ValidationResult(String declaredBaseType, String declaredSubType, String detectedBaseType, |
| 14 | + String detectedSubType) |
| 15 | + { |
| 16 | + public String declared() |
| 17 | + { |
| 18 | + return declaredBaseType + "/" + declaredSubType; |
| 19 | + } |
| 20 | + |
| 21 | + public String detected() |
| 22 | + { |
| 23 | + return detectedBaseType + "/" + detectedSubType; |
| 24 | + } |
| 25 | + |
| 26 | + public boolean mimetypesMatch() |
| 27 | + { |
| 28 | + return declared().equals(detected()); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME |
| 34 | + * type. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result can be |
| 35 | + * used to drive custom logic based on whether the detected type matches the declared type. |
| 36 | + * |
| 37 | + * @param stream |
| 38 | + * input stream of which the MIME type should be detected |
| 39 | + * @param declared |
| 40 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 41 | + * @return {@link ValidationResult} containing the declared and detected MIME types. |
| 42 | + */ |
| 43 | + ValidationResult validateWithResult(InputStream stream, String declared); |
| 44 | + |
| 45 | + /** |
| 46 | + * Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME |
| 47 | + * type. Returns a {@link ValidationResult} containing both the declared and detected MIME types. This result can be |
| 48 | + * used to drive custom logic based on whether the detected type matches the declared type. |
| 49 | + * |
| 50 | + * @param data |
| 51 | + * byte array of which the MIME type should be detected |
| 52 | + * @param declared |
| 53 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 54 | + * @return {@link ValidationResult} containing the declared and detected MIME types. |
| 55 | + */ |
| 56 | + default ValidationResult validateWithResult(byte[] data, String declared) |
| 57 | + { |
| 58 | + return validateWithResult(new ByteArrayInputStream(data), declared); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME |
| 63 | + * type. Returns <code>true</code> if the full MIME type matches, <code>false</code> otherwise. |
| 64 | + * |
| 65 | + * @param stream |
| 66 | + * input stream of which the MIME type should be detected |
| 67 | + * @param declared |
| 68 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 69 | + * @return <code>true</code> if the full MIME type matches, <code>false</code> otherwise |
| 70 | + */ |
| 71 | + default boolean validateWithBoolean(InputStream stream, String declared) |
| 72 | + { |
| 73 | + return validateWithResult(stream, declared).mimetypesMatch(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME |
| 78 | + * type. Returns <code>true</code> if the full MIME type matches, <code>false</code> otherwise. |
| 79 | + * |
| 80 | + * @param data |
| 81 | + * byte array of which the MIME type should be detected |
| 82 | + * @param declared |
| 83 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 84 | + * @return <code>true</code> if the full MIME type matches, <code>false</code> otherwise |
| 85 | + */ |
| 86 | + default boolean validateWithBoolean(byte[] data, String declared) |
| 87 | + { |
| 88 | + return validateWithResult(new ByteArrayInputStream(data), declared).mimetypesMatch(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Detects the MIME type of the provided input stream and validates if the detected MIME type equals the declared |
| 93 | + * MIME type. Logs a warning if the full MIME types do not match, throws a {@link RuntimeException} if the base MIME |
| 94 | + * types do not match. |
| 95 | + * |
| 96 | + * @param stream |
| 97 | + * input stream of which the MIME type should be detected |
| 98 | + * @param declared |
| 99 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 100 | + * @throws RuntimeException |
| 101 | + * if the detected and the declared base MIME type do not match |
| 102 | + */ |
| 103 | + default void validateWithException(InputStream stream, String declared) |
| 104 | + { |
| 105 | + ValidationResult result = validateWithResult(stream, declared); |
| 106 | + |
| 107 | + if (!result.mimetypesMatch()) |
| 108 | + logger.warn("Declared full MIME type {} does not match detected full MIME type {}", result.declared(), |
| 109 | + result.detected()); |
| 110 | + |
| 111 | + if (!result.declaredBaseType().equals(result.detectedBaseType())) |
| 112 | + { |
| 113 | + throw new RuntimeException("Declared base MIME type of '" + result.declared() |
| 114 | + + "' does not match detected base MIME type of '" + result.detected() + "'"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Detects the MIME type of the provided byte array and validates if the detected MIME type equals the declared MIME |
| 120 | + * type. Logs a warning if the full MIME types do not match, throws a {@link RuntimeException} if the base MIME |
| 121 | + * types do not match. |
| 122 | + * |
| 123 | + * @param data |
| 124 | + * byte array of which the MIME type should be detected |
| 125 | + * @param declared |
| 126 | + * the declared MIME type of the data, e.g. <code>"application/pdf"</code> |
| 127 | + * @throws RuntimeException |
| 128 | + * if the detected and the declared base MIME type do not match |
| 129 | + */ |
| 130 | + default void validateWithException(byte[] data, String declared) |
| 131 | + { |
| 132 | + validateWithException(new ByteArrayInputStream(data), declared); |
| 133 | + } |
| 134 | +} |
0 commit comments