1 package DBIx::Class::InflateColumn::File;
5 use base 'DBIx::Class';
10 __PACKAGE__->load_components(qw/InflateColumn/);
14 my ($self, $column, $info, @rest) = @_;
15 $self->next::method($column, $info, @rest);
16 return unless defined($info->{is_file_column});
17 $self->inflate_column(
21 my ($value, $obj) = @_;
22 #$self->_inflate_file_column;
25 my ($value, $obj) = @_;
26 #my ( $file, @column_names ) = $self->_load_file_column_information;
27 #$self->_save_file_column( $file, $self, @column_names );
35 my ( $self, @rest ) = @_;
37 my @column_names = $self->columns;
39 if ( $self->column_info($_)->{is_file_column} ) {
41 File::Spec->catdir( $self->column_info($_)->{file_column_path},
43 rmtree( [$path], 0, 0 );
47 my $ret = $self->next::method(@rest);
52 sub _inflate_file_column {
55 my @column_names = $self->columns;
57 if ( $self->column_info($_)->{is_file_column} ) {
58 # make sure everything checks out
59 unless (defined $self->$_) {
60 # if something is wrong set it to undef
65 File::Spec->catfile( $self->column_info($_)->{file_column_path},
66 $self->id, $self->$_ );
67 $self->$_({handle => new IO::File($fs_file, "r"), filename => $self->$_});
72 sub _load_file_column_information {
78 @column_names = $self->columns;
80 if ( $self->column_info($_)->{is_file_column} ) {
81 # make sure everything checks out
82 unless ((defined $self->$_) ||
83 (defined $self->$_->{filename} && defined $self->$_->{handle})) {
84 # if something is wrong set it to undef
88 $file->{$_} = $self->$_;
89 $self->$_( $self->$_->{filename} );
93 return ( $file, @column_names );
96 sub _save_file_column {
97 my ( $self, $file, $ret, @column_names ) = @_;
100 if ( $ret->column_info($_)->{is_file_column} ) {
101 next unless (defined $ret->$_);
103 File::Spec->catdir( $ret->column_info($_)->{file_column_path},
108 File::Spec->catfile( $file_path, $file->{$_}->{filename} );
109 File::Copy::copy( $file->{$_}->{handle}, $outfile );
111 $self->_file_column_callback($file->{$_},$ret,$_);
121 =head2 _file_column_callback ($file,$ret,$target)
123 method made to be overridden for callback purposes.
127 sub _file_column_callback {
128 my ($self,$file,$ret,$target) = @_;
133 DBIx::Class::InflateColumn::File - map files from the Database to the filesystem.
141 In your L<DBIx::Class> table class:
143 __PACKAGE__->load_components( "PK::Auto", "InflateColumn::File", "Core" );
145 # define your columns
146 __PACKAGE__->add_columns(
149 data_type => "integer",
150 is_auto_increment => 1,
156 data_type => "varchar",
158 file_column_path =>'/tmp/uploaded_files',
159 # or for a Catalyst application
160 # file_column_path => MyApp->path_to('root','static','files'),
161 default_value => undef,
168 In your L<Catalyst::Controller> class:
170 FileColumn requires a hash that contains L<IO::File> as handle and the file's name as name.
172 my $entry = $c->model('MyAppDB::Articles')->create({
175 handle => $c->req->upload('myupload')->fh,
176 filename => $c->req->upload('myupload')->basename
180 $c->stash->{entry}=$entry;
183 And Place the following in your TT template
185 Article Subject: [% entry.subject %]
187 <a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a>
188 Body: [% entry.body %]
190 The file will be stored on the filesystem for later retrieval.
191 Calling delete on your resultset will delete the file from the filesystem.
192 Retrevial of the record automatically inflates the column back to the set hash with the IO::File handle and filename.
200 This library is free software, you can redistribute it and/or modify
201 it under the same terms as Perl itself.