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(
20 inflate => $self->_inflate_file_column,
22 my ( $file, @column_names ) = $self->_load_file_column_information;
23 $self->_save_file_column( $file, $self, @column_names );
31 my ( $self, @rest ) = @_;
33 my @column_names = $self->columns;
35 if ( $self->column_info($_)->{is_file_column} ) {
37 File::Spec->catdir( $self->column_info($_)->{file_column_path},
39 rmtree( [$path], 0, 0 );
43 my $ret = $self->next::method(@rest);
48 sub _inflate_file_column {
51 my @column_names = $self->columns;
53 if ( $self->column_info($_)->{is_file_column} ) {
54 # make sure everything checks out
55 unless (defined $self->$_) {
56 # if something is wrong set it to undef
61 File::Spec->catfile( $self->column_info($_)->{file_column_path},
62 $self->id, $self->$_ );
63 $self->$_({handle => new IO::File($fs_file, "r"), filename => $self->$_});
68 sub _load_file_column_information {
74 @column_names = $self->columns;
76 if ( $self->column_info($_)->{is_file_column} ) {
77 # make sure everything checks out
78 unless ((defined $self->$_) ||
79 (defined $self->$_->{filename} && defined $self->$_->{handle})) {
80 # if something is wrong set it to undef
84 $file->{$_} = $self->$_;
85 $self->$_( $self->$_->{filename} );
89 return ( $file, @column_names );
92 sub _save_file_column {
93 my ( $self, $file, $ret, @column_names ) = @_;
96 if ( $ret->column_info($_)->{is_file_column} ) {
97 next unless (defined $ret->$_);
99 File::Spec->catdir( $ret->column_info($_)->{file_column_path},
104 File::Spec->catfile( $file_path, $file->{$_}->{filename} );
105 File::Copy::copy( $file->{$_}->{handle}, $outfile );
107 $self->_file_column_callback($file->{$_},$ret,$_);
117 =head2 _file_column_callback ($file,$ret,$target)
119 method made to be overridden for callback purposes.
123 sub _file_column_callback {
124 my ($self,$file,$ret,$target) = @_;
129 DBIx::Class::InflateColumn::File - map files from the Database to the filesystem.
137 In your L<DBIx::Class> table class:
139 __PACKAGE__->load_components( "PK::Auto", "InflateColumn::File", "Core" );
141 # define your columns
142 __PACKAGE__->add_columns(
145 data_type => "integer",
146 is_auto_increment => 1,
152 data_type => "varchar",
154 file_column_path =>'/tmp/uploaded_files',
155 # or for a Catalyst application
156 # file_column_path => MyApp->path_to('root','static','files'),
157 default_value => undef,
164 In your L<Catalyst::Controller> class:
166 FileColumn requires a hash that contains L<IO::File> as handle and the file's name as name.
168 my $entry = $c->model('MyAppDB::Articles')->create({
171 handle => $c->req->upload('myupload')->fh,
172 filename => $c->req->upload('myupload')->basename
176 $c->stash->{entry}=$entry;
179 And Place the following in your TT template
181 Article Subject: [% entry.subject %]
183 <a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a>
184 Body: [% entry.body %]
186 The file will be stored on the filesystem for later retrieval.
187 Calling delete on your resultset will delete the file from the filesystem.
188 Retrevial of the record automatically inflates the column back to the set hash with the IO::File handle and filename.
196 This library is free software, you can redistribute it and/or modify
197 it under the same terms as Perl itself.