cancel
Showing results for 
Search instead for 
Did you mean: 

How to get noderef of file during file creation in share

hardik_thakkar
Star Contributor
Star Contributor

Hi Team,

I have requirement to restrict from creation of exe file.

User is creating plain text file from share UI and entering name like test.exe.

I have created rule that checks if file name ends with exe, it throws exception. But rule is not executing somehow.

Then i have created behaviour that executes on node creation, I am getting parentref using below code.

public void init() {
		policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeCreateNodePolicy.QNAME,
				ContentModel.TYPE_CONTENT,
				new JavaBehaviour(this, "beforeCreateNode", Behaviour.NotificationFrequency.EVERY_EVENT));
	}
	
	@Override
	public void beforeCreateNode(NodeRef parentRef, QName assocTypeQName, QName assocQName, QName nodeTypeQName) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Started executing FileCreationRestrictionBehaviour ..");
			
			LOGGER.debug("parentref --> "+parentRef);
			
			LOGGER.debug("parentref NAME --> "+nodeService.getProperty(parentRef, ContentModel.PROP_NAME));
			
			System.out.println("parentref --> "+parentRef + 
					"parentref NAME --> "+nodeService.getProperty(parentRef, ContentModel.PROP_NAME));
			
			
		}
		
	}

So,my requirement is to restrict file creation, if users enters .exe in file name, how can i get the noderef/filename of that file, so, i can throw exception.

Thanks,
Hardik

1 ACCEPTED ANSWER

abhinavmishra14
World-Class Innovator
World-Class Innovator

Try : org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy

You can do something like this:

public void onCreateNode(final ChildAssociationRef childAssocRef) {
	final NodeRef nodeRef = childAssocRef.getChildRef();//node which is being created.
	if (nodeService.exists(nodeRef)) {
		final String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
		final String mimetype = mimetypeService.guessMimetype(fileName); //find mimetype by extension of the file.
		if (restrictedMimeList.contains(mimetype)) {// check whether the result is one of the restricted mime types from the list?
			throw new AlfrescoRuntimeException("Mimetype " + mimetype + " is unsupported");
		}
	}
}
~Abhinav
(ACSCE, AWS SAA, Azure Admin)

View answer in original post

4 REPLIES 4

abhinavmishra14
World-Class Innovator
World-Class Innovator

Try : org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy

You can do something like this:

public void onCreateNode(final ChildAssociationRef childAssocRef) {
	final NodeRef nodeRef = childAssocRef.getChildRef();//node which is being created.
	if (nodeService.exists(nodeRef)) {
		final String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
		final String mimetype = mimetypeService.guessMimetype(fileName); //find mimetype by extension of the file.
		if (restrictedMimeList.contains(mimetype)) {// check whether the result is one of the restricted mime types from the list?
			throw new AlfrescoRuntimeException("Mimetype " + mimetype + " is unsupported");
		}
	}
}
~Abhinav
(ACSCE, AWS SAA, Azure Admin)

@hardik_thakkar  you can also try this add-on which covers several other scenarios while blocking blacklisted mimetypes.

https://github.com/abhinavmishra14/alfresco-mimetype-blocker/releases

originally forked from https://github.com/keensoft/alfresco-mimetype-blocker

~Abhinav
(ACSCE, AWS SAA, Azure Admin)

Hi @abhinavmishra14 

I have configured that add-on and that works perfectly during file upload part.

Written behaviour for file creation part as per your suggestion and that's working fine.

Thanks,

Hardik

Good to hear that everything works fine for you. Good luck  @hardik_thakkar 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)