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,$_);
118 DBIx::Class::InflateColumn::File - map files from the Database to the filesystem.
122 In your L<DBIx::Class> table class:
124 __PACKAGE__->load_components( "PK::Auto", "InflateColumn::File", "Core" );
126 # define your columns
127 __PACKAGE__->add_columns(
130 data_type => "integer",
131 is_auto_increment => 1,
137 data_type => "varchar",
139 file_column_path =>'/tmp/uploaded_files',
140 # or for a Catalyst application
141 # file_column_path => MyApp->path_to('root','static','files'),
142 default_value => undef,
149 In your L<Catalyst::Controller> class:
151 FileColumn requires a hash that contains L<IO::File> as handle and the file's
154 my $entry = $c->model('MyAppDB::Articles')->create({
157 handle => $c->req->upload('myupload')->fh,
158 filename => $c->req->upload('myupload')->basename
162 $c->stash->{entry}=$entry;
165 And Place the following in your TT template
167 Article Subject: [% entry.subject %]
169 <a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a>
170 Body: [% entry.body %]
172 The file will be stored on the filesystem for later retrieval. Calling delete
173 on your resultset will delete the file from the filesystem. Retrevial of the
174 record automatically inflates the column back to the set hash with the
175 IO::File handle and filename.
183 =head2 _file_column_callback ($file,$ret,$target)
185 method made to be overridden for callback purposes.
189 sub _file_column_callback {
190 my ($self,$file,$ret,$target) = @_;
199 This library is free software, you can redistribute it and/or modify
200 it under the same terms as Perl itself.