1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
package DBIx::Class::InflateColumn::File;
use strict;
use warnings;
use base 'DBIx::Class';
use File::Path;
use File::Copy;
use IO::File;
__PACKAGE__->load_components(qw/InflateColumn/);
sub register_column {
my ($self, $column, $info, @rest) = @_;
$self->next::method($column, $info, @rest);
return unless defined($info->{is_file_column});
$self->inflate_column(
$column =>
{
inflate => sub {
my ($value, $obj) = @_;
#$self->_inflate_file_column;
},
deflate => sub {
my ($value, $obj) = @_;
#my ( $file, @column_names ) = $self->_load_file_column_information;
#$self->_save_file_column( $file, $self, @column_names );
},
}
);
}
sub delete {
my ( $self, @rest ) = @_;
my @column_names = $self->columns;
for (@column_names) {
if ( $self->column_info($_)->{is_file_column} ) {
my $path =
File::Spec->catdir( $self->column_info($_)->{file_column_path},
$self->id );
rmtree( [$path], 0, 0 );
}
}
my $ret = $self->next::method(@rest);
return $ret;
}
sub _inflate_file_column {
my $self = shift;
my @column_names = $self->columns;
for(@column_names) {
if ( $self->column_info($_)->{is_file_column} ) {
# make sure everything checks out
unless (defined $self->$_) {
# if something is wrong set it to undef
$self->$_(undef);
next;
}
my $fs_file =
File::Spec->catfile( $self->column_info($_)->{file_column_path},
$self->id, $self->$_ );
$self->$_({handle => new IO::File($fs_file, "r"), filename => $self->$_});
}
}
}
sub _load_file_column_information {
my $self = shift;
my $file;
my @column_names;
@column_names = $self->columns;
for (@column_names) {
if ( $self->column_info($_)->{is_file_column} ) {
# make sure everything checks out
unless ((defined $self->$_) ||
(defined $self->$_->{filename} && defined $self->$_->{handle})) {
# if something is wrong set it to undef
$self->$_(undef);
next;
}
$file->{$_} = $self->$_;
$self->$_( $self->$_->{filename} );
}
}
return ( $file, @column_names );
}
sub _save_file_column {
my ( $self, $file, $ret, @column_names ) = @_;
for (@column_names) {
if ( $ret->column_info($_)->{is_file_column} ) {
next unless (defined $ret->$_);
my $file_path =
File::Spec->catdir( $ret->column_info($_)->{file_column_path},
$ret->id );
mkpath [$file_path];
my $outfile =
File::Spec->catfile( $file_path, $file->{$_}->{filename} );
File::Copy::copy( $file->{$_}->{handle}, $outfile );
$self->_file_column_callback($file->{$_},$ret,$_);
}
}
}
=head1 NAME
DBIx::Class::InflateColumn::File - map files from the Database to the filesystem.
=head1 SYNOPSIS
In your L<DBIx::Class> table class:
__PACKAGE__->load_components( "PK::Auto", "InflateColumn::File", "Core" );
# define your columns
__PACKAGE__->add_columns(
"id",
{
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
size => 4,
},
"filename",
{
data_type => "varchar",
is_file_column => 1,
file_column_path =>'/tmp/uploaded_files',
# or for a Catalyst application
# file_column_path => MyApp->path_to('root','static','files'),
default_value => undef,
is_nullable => 1,
size => 255,
},
);
In your L<Catalyst::Controller> class:
FileColumn requires a hash that contains L<IO::File> as handle and the file's
name as name.
my $entry = $c->model('MyAppDB::Articles')->create({
subject => 'blah',
filename => {
handle => $c->req->upload('myupload')->fh,
filename => $c->req->upload('myupload')->basename
},
body => '....'
});
$c->stash->{entry}=$entry;
And Place the following in your TT template
Article Subject: [% entry.subject %]
Uploaded File:
<a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a>
Body: [% entry.body %]
The file will be stored on the filesystem for later retrieval. Calling delete
on your resultset will delete the file from the filesystem. Retrevial of the
record automatically inflates the column back to the set hash with the
IO::File handle and filename.
=head1 DESCRIPTION
InflateColumn::File
=head1 METHODS
=head2 _file_column_callback ($file,$ret,$target)
method made to be overridden for callback purposes.
=cut
sub _file_column_callback {
my ($self,$file,$ret,$target) = @_;
}
=head1 AUTHOR
Victor Igumnov
=head1 LICENSE
This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|