How to check the MIME Type before uploading?
Hi,
I've a scenario for security check. When i'm submitting the file to dam, the content type for pdf, docx,doc is "application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/msword".
I've created a config file for this and whenever i upload a file i'll check for MIME_Type and then i upload.
Code:-
private String[] multiString;
private static final Logger LOG = LoggerFactory.getLogger(PODServlet.class);
@Property(value={"application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/msword"}, unbounded = PropertyUnbounded.ARRAY, label = "MIME TYPE", cardinality = 50, description = "Multi field config for MIME Type")
private static final String KEY_MULTI_FIELD_PODSERVLET = "PODServlet.MIMEType";
@Activate protected void activate(@SuppressWarnings("rawtypes") final Map context) {
this.multiString = PropertiesUtil.toStringArray(context.get(KEY_MULTI_FIELD_PODSERVLET));
LOG.info("multiString----------@Ativate----------------->"+multiString);
....................
resolver = resolverFactory.getAdministrativeResourceResolver(null);
Session session = (Session) resolver.adaptTo(Session.class);
AssetManager assetManager = (AssetManager) resolver.adaptTo(AssetManager.class);
Asset asset = assetManager.getAssetForBinary(DamUtil.assetToBinaryPath(assetPath));
String mimeType = this.mimeTypeService.getMimeType(fileName);
for(int i=0;i<multiString.length;i++){
if(StringUtils.equals(mimeType, multiString[i]))
{
asset = assetManager.createAsset(assetPath, file1, mimeType, true);
Resource metadataRes = asset.adaptTo(Resource.class).getChild("jcr:content/metadata");
ModifiableValueMap map = metadataRes.adaptTo(ModifiableValueMap.class);
map.put("dc:podname", podname);
map.put("dc:actualDeliveryDate", actualDeliveryDate);
metadataRes.getResourceResolver().commit();
}
}
but when the check for security purpose , its stopping for .pdf and .docx, like it will upload the file but when tried to open it will throw an error message. but this is not happening for .doc file extension.
How can this be resolved ? Or is any other way to check for MIME_Type when third party user try to interrupt the content type and throw an error message ?
Regards,
Nandhini M