Sunday, 15 April 2012

How to properly validate .stl file upload in Symfony2.3 -



How to properly validate .stl file upload in Symfony2.3 -

my question concern uploading .stl. here quick description of .stl taken wikipedia :

stl (stereolithography) file format native stereolithography [...]

the stl format specifies both ascii , binary representations. binary files more common, since more compact.

so application must handle binary , ascii representation (both .stl extension). binary file command returns :

$ file foo.stl -i foo.stl: application/octet-stream; charset=binary

the problem form accept files (images, documents, ...) , .stl files bin.

i followed symfony cookbook on file uploads , created model class, wich looks :

class model { /** * model file * * @var file * * @assert\file( * maxsize = "50m", * mimetypes = { "application/octet-stream"}, * maxsizemessage = "the maxmimum allowed file size 50mb.", * ) */ protected $file; (...) /** * * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if (null !== $this->file) { $filename = sha1(uniqid(mt_rand(), true)); $this->path = $filename.'.stl'; } } (...) }

you can see forcefulness extension .stl (it's not pretty it's try).

here related formtype :

class modeltype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('file', 'file', array('attr' => array('accept' => '*.stl'))); } (...) }

and controller (model part of product, producttype have new modeltype in buildform method) :

public function createaction() { $user = $this->get('security.context')->gettoken()->getuser(); $product = new product; $form = $this->createform(new producttype, $product); $request = $this->get('request'); if ($request->ismethod('post')) { $form->bind($request); if ($form->isvalid()) { //always valid file (... persist, flush , redirection) } } homecoming $this->render('mybundle:product:create.html.twig', array('form' => $form->createview())); }

it seems assert (even take attribute in form) doesn't work here because i'm redirected. read lot of things lot concerning symfony1.

do have ideas symfony2 style?

thank reading.

file symfony2 upload binary

No comments:

Post a Comment