Validating size of documents

Hi.

I have a form and a upload widget working fine. When submited, it sets the file in a document defined in my process. I need to validate de size of this document. I try to create a grovy script validator but i just have a instance of org.bonitasoft.engine.bpm.document interface and it not have methods to get lenght.
How can i validate the size of documents?

Thanks.

Hello,
Unfortunally, you can’t validate the size before the submit (so, in a validator). File is save in a temporary file on the server site, and you have no way (in Java or in Groovy) to access it.
After the submit, you can access the file in the next step (or in the output connector).

Note the engine don’t limit the upload size, but limit the document size to 15 mb.

I think i resolve the problem.

  1. Create a variable of type Document in my process (called documentFile)
  2. Add a operation in my task and assign my Document to my variable
  3. Create a Service Task to be called after my original task was submited
  4. In this task, search by content bytes of my Document and do the validations
import javax.activation.MimetypesFileTypeMap;

//here we can use the length of byte to validate the size of Document
byte byteFile= apiAccessor.getProcessAPI().getDocumentContent(documentFile.getContentStorageId());

//here we can validate the mimetype of file
InputStream is = new BufferedInputStream(new ByteArrayInputStream(byteFile));
String mimeFile = URLConnection.guessContentTypeFromStream(is);

if( !mimeFile .contains(“image/”) && !mimeFile .contains(“application/pdf”)){
return “Invalid file”;
}

return “”;

Well… this is bad :frowning:
Maybe i can add a task after the form to validate de size of file and if dont pass i can clean the document and send to the upload task. But, how can i access a file after submit the upload? I have a Document as a variable in my process.

Thanks Pierre-yves!

Maybe i can add a task after the form to validate de size of file and if dont pass i can clean the document and send to the upload task -
==> Exactly, that what I mean by “you can access the file in the next step”.
At this moment, the file uploaded is accessible in the Document, and you can do what you want.

That what you did if I look your last comment.