playframework - Uploading images with MYSQL blob type -
let's have model:
@entity public class picture extends model { public blob image; ... }
when type in mysql database path attachments folder (varchar). there way change save binary data mysql (blob) using play?
i achieve this:
create table picture ( ...... image blob, ... );
using jdbc set image:
"psmnt.setbinarystream(3, (inputstream)fis, (int)(image.length()));"
i don't know if makes sense @ if not please explain me why! why having attachments folder play project?
in play blob type stores hash reference file plus mime type. reason databases don't big blobs (for internal reason) , it's practice store files aside. avoid headaches related encodings , backups, trust me (and more importantly, trust play developers!)
as alternative, can store image as:
@entity public class yourclass extends model { @basic(fetch = fetchtype.lazy) @lob public byte[] image; public string mime_type; public string file_name; }
you need store mime type separately (blob type stores in database in field) able work file, , might want store original file name given you. detect mime type recommend use mime-util.
as last note, aware if use play's blob, when delete field (via crud or api) file not removed file system. need create job checks unused files time time, free space.
this happens (in gillaume's words) due impossibility of having safe 2-phase transaction between database , file system. relevant, depending on application might find file system filled unused images :)
Comments
Post a Comment