Created
June 24, 2011 14:59
-
-
Save benvanstaveren/1044959 to your computer and use it in GitHub Desktop.
file upload to mongodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(my $upload = $self->req->upload('myfile')) { | |
my $filename = $upload->filename; | |
my $asset; | |
if(ref($upload->asset) ne 'Mojo::Upload::File') { | |
$asset = Mojo::Asset::File->new(path => POSIX::tmpnam); | |
$asset->add_chunk($upload->asset->slurp); | |
} else { | |
$asset = $upload->asset; | |
} | |
$asset->cleanup(1); | |
my $gfs = $self->db->get_gridfs('attachments'); | |
my $fh = FileHandle->new(); | |
my $id; | |
try { | |
$fh->open(sprintf('<%s', $asset->path)) || die "could not open attachment file\n"; | |
$id = $gfs->insert($fh, { filename => $filename }); | |
} catch { | |
# deal with the error | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to POSIX man page, tmpnam should not be used for security reason. The module File::Temp should be used instead