1 package DBIx::Class::InflateColumn::File;
5 use base 'DBIx::Class';
12 carp 'InflateColumn::File has entered a deprecation cycle. This component '
13 .'has a number of architectural deficiencies that can quickly drive '
14 .'your filesystem and database out of sync and is not recommended '
15 .'for further use. It will be retained for backwards '
16 .'compatibility, but no new functionality patches will be accepted. '
17 .'Please consider using the much more mature and actively maintained '
18 .'DBIx::Class::InflateColumn::FS. You can set the environment variable '
19 .'DBIC_IC_FILE_NOWARN to a true value to disable this warning.'
20 unless $ENV{DBIC_IC_FILE_NOWARN};
24 __PACKAGE__->load_components(qw/InflateColumn/);
27 my ($self, $column, $info, @rest) = @_;
28 $self->next::method($column, $info, @rest);
29 return unless defined($info->{is_file_column});
31 $self->inflate_column($column => {
33 my ($value, $obj) = @_;
34 $obj->_inflate_file_column($column, $value);
37 my ($value, $obj) = @_;
38 $obj->_save_file_column($column, $value);
43 sub _file_column_file {
44 my ($self, $column, $filename) = @_;
46 my $column_info = $self->column_info($column);
48 return unless $column_info->{is_file_column};
50 my $id = $self->id || $self->throw_exception(
51 'id required for filename generation'
54 $filename ||= $self->$column->{filename};
55 return Path::Class::file(
56 $column_info->{file_column_path}, $id, $filename,
61 my ( $self, @rest ) = @_;
63 for ( $self->columns ) {
64 if ( $self->column_info($_)->{is_file_column} ) {
65 rmtree( [$self->_file_column_file($_)->dir], 0, 0 );
66 last; # if we've deleted one, we've deleted them all
70 return $self->next::method(@rest);
76 # cache our file columns so we can write them to the fs
77 # -after- we have a PK
79 for ( $self->columns ) {
80 if ( $self->column_info($_)->{is_file_column} ) {
81 $file_column{$_} = $self->$_;
82 $self->store_column($_ => $self->$_->{filename});
86 $self->next::method(@_);
88 # write the files to the fs
89 while ( my ($col, $file) = each %file_column ) {
90 $self->_save_file_column($col, $file);
97 sub _inflate_file_column {
98 my ( $self, $column, $value ) = @_;
100 my $fs_file = $self->_file_column_file($column, $value);
102 return { handle => $fs_file->open('r'), filename => $value };
105 sub _save_file_column {
106 my ( $self, $column, $value ) = @_;
108 return unless ref $value;
110 my $fs_file = $self->_file_column_file($column, $value->{filename});
111 mkpath [$fs_file->dir];
113 # File::Copy doesn't like Path::Class (or any for that matter) objects,
114 # thus ->stringify (http://rt.perl.org/rt3/Public/Bug/Display.html?id=59650)
115 File::Copy::copy($value->{handle}, $fs_file->stringify);
117 $self->_file_column_callback($value, $self, $column);
119 return $value->{filename};
124 DBIx::Class::InflateColumn::File - DEPRECATED (superseded by DBIx::Class::InflateColumn::FS)
126 =head2 Deprecation Notice
128 This component has a number of architectural deficiencies that can quickly
129 drive your filesystem and database out of sync and is not recommended for
130 further use. It will be retained for backwards compatibility, but no new
131 functionality patches will be accepted. Please consider using the much more
132 mature and actively supported DBIx::Class::InflateColumn::FS. You can set
133 the environment variable DBIC_IC_FILE_NOWARN to a true value to disable
138 In your L<DBIx::Class> table class:
140 use base 'DBIx::Class::Core';
142 __PACKAGE__->load_components(qw/InflateColumn::File/);
144 # define your columns
145 __PACKAGE__->add_columns(
148 data_type => "integer",
149 is_auto_increment => 1,
155 data_type => "varchar",
157 file_column_path =>'/tmp/uploaded_files',
158 # or for a Catalyst application
159 # file_column_path => MyApp->path_to('root','static','files'),
160 default_value => undef,
167 In your L<Catalyst::Controller> class:
169 FileColumn requires a hash that contains L<IO::File> as handle and the file's
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. Calling delete
191 on your resultset will delete the file from the filesystem. Retrevial of the
192 record automatically inflates the column back to the set hash with the
193 IO::File handle and filename.
201 =head2 _file_column_callback ($file,$ret,$target)
203 Method made to be overridden for callback purposes.
207 sub _file_column_callback {}
215 This library is free software, you can redistribute it and/or modify
216 it under the same terms as Perl itself.