coolaj86 / formaline

formaline is a module for handling form requests ( HTTP POSTs / PUTs ) and for fast parsing of file uploads.
Apache License 2.0
239 stars 16 forks source link

MIME type and size #17

Closed ochawin closed 13 years ago

ochawin commented 13 years ago

Where can I perform type and size check (in 'load' or 'fileprogress' event ?) Using formidable, I have a difficulty in checking and stopping the form to parse when those conditions are met. Any suggestions?

Thanks in advance.

rootslab commented 13 years ago

Hi winladen!

With 'load' event you can read the size and the type of a file inside the JSON returned, json.value.size / json.value.type . When a file was received, json.value is an object, instead if a field was received, it is a simple string ( see the listeners signatures in the Readme ) .

Quite the same thing is for the 'fileprogress' event ( you have to enable the 'emitFileProgress' option in configuration object ), json.value.size contains the bytes of data currently received for a certain file.

Hence, I didn't understand well, do you want stopping to parse the stream when a certain file was received ?

ochawin commented 13 years ago

Thanks rootslab In 'fileprogress' event, I check for the size and type. If it's incorrect, I want to tell 'loadend' not to progress further or should I throw error in fileprogress and tell form to stop parsing.

rootslab commented 13 years ago

Ok, for the file size you can use the config option 'maxFileSize', formaline stops writing the file to disk if the size exceeds maxFileSize ( it also throws a message event), and it continues to parse other fields/files, without breaking the stream .

You can specify 'removeIncompleteFiles' if you want to auto remove this incomplete file ( when 'loadend' will thrown ) .

Hence, the 'loadend' event is always thrown and will list all the file received or incomplete.

Only for saying, If you want to rely on the file type returned in the JSON, you have to known that this value is setted from the browser with the current extension type ( .jpg, .png, ..), there isn't any deep MIME-type checking ! .

For now, there is not an option to brutally stop the stream, you can try with form.closeConnection(), or to remove all the 'data' listeners for request.

I hope I helped you!

Let me know if you need more help ;)

ochawin commented 13 years ago

Thank you so much!

rootslab commented 13 years ago

;)