Skip to content

Instantly share code, notes, and snippets.

@benvanstaveren
Created June 24, 2011 14:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benvanstaveren/1044959 to your computer and use it in GitHub Desktop.
Save benvanstaveren/1044959 to your computer and use it in GitHub Desktop.
file upload to mongodb
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
};
}
@dod38fr
Copy link

dod38fr commented Sep 17, 2012

According to POSIX man page, tmpnam should not be used for security reason. The module File::Temp should be used instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment