X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FInflateColumn%2FFile.pm;h=34db2ed02313f9344dccf19eaff73cac39fe39f5;hb=b83736a7d3235d2f50fe5695550eb3637432d960;hp=19011875a9dd81621fd3219a58e482a21176bf4a;hpb=d4daee7b54e38e4b3d3d0a77759bddc1a4ede6e5;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/InflateColumn/File.pm b/lib/DBIx/Class/InflateColumn/File.pm index 1901187..34db2ed 100644 --- a/lib/DBIx/Class/InflateColumn/File.pm +++ b/lib/DBIx/Class/InflateColumn/File.pm @@ -2,10 +2,30 @@ package DBIx::Class::InflateColumn::File; use strict; use warnings; + +# check deps +BEGIN { + require DBIx::Class::Optional::Dependencies; + if (my $missing = DBIx::Class::Optional::Dependencies->req_missing_for ('ic_file') ) { + die "The following extra modules are required for DBIx::Class::InflateColumn::File: $missing\n"; + } +} + use base 'DBIx::Class'; -use File::Path; use File::Copy; -use Path::Class; +use DBIx::Class::Carp; +use namespace::clean; + +carp 'InflateColumn::File has entered a deprecation cycle. This component ' + .'has a number of architectural deficiencies that can quickly drive ' + .'your filesystem and database out of sync and is not recommended ' + .'for further use. It will be retained for backwards ' + .'compatibility, but no new functionality patches will be accepted. ' + .'Please consider using the much more mature and actively maintained ' + .'DBIx::Class::InflateColumn::FS. You can set the environment variable ' + .'DBIC_IC_FILE_NOWARN to a true value to disable this warning.' +unless $ENV{DBIC_IC_FILE_NOWARN}; + __PACKAGE__->load_components(qw/InflateColumn/); @@ -15,7 +35,7 @@ sub register_column { return unless defined($info->{is_file_column}); $self->inflate_column($column => { - inflate => sub { + inflate => sub { my ($value, $obj) = @_; $obj->_inflate_file_column($column, $value); }, @@ -29,10 +49,14 @@ sub register_column { sub _file_column_file { my ($self, $column, $filename) = @_; - my $column_info = $self->column_info($column); + my $column_info = $self->result_source->columns_info->{$column}; return unless $column_info->{is_file_column}; + # DO NOT CHANGE + # This call to id() is generally incorrect - will not DTRT on + # multicolumn key. However changing this may introduce + # backwards-comp regressions, thus leaving as is my $id = $self->id || $self->throw_exception( 'id required for filename generation' ); @@ -46,9 +70,11 @@ sub _file_column_file { sub delete { my ( $self, @rest ) = @_; - for ( $self->columns ) { - if ( $self->column_info($_)->{is_file_column} ) { - rmtree( [$self->_file_column_file($_)->dir], 0, 0 ); + my $colinfos = $self->result_source->columns_info; + + for ( keys %$colinfos ) { + if ( $colinfos->{$_}{is_file_column} ) { + $self->_file_column_file($_)->dir->rmtree; last; # if we've deleted one, we've deleted them all } } @@ -61,9 +87,11 @@ sub insert { # cache our file columns so we can write them to the fs # -after- we have a PK + my $colinfos = $self->result_source->columns_info; + my %file_column; - for ( $self->columns ) { - if ( $self->column_info($_)->{is_file_column} ) { + for ( keys %$colinfos ) { + if ( $colinfos->{$_}{is_file_column} ) { $file_column{$_} = $self->$_; $self->store_column($_ => $self->$_->{filename}); } @@ -94,7 +122,7 @@ sub _save_file_column { return unless ref $value; my $fs_file = $self->_file_column_file($column, $value->{filename}); - mkpath [$fs_file->dir]; + $fs_file->dir->mkpath; # File::Copy doesn't like Path::Class (or any for that matter) objects, # thus ->stringify (http://rt.perl.org/rt3/Public/Bug/Display.html?id=59650) @@ -107,13 +135,25 @@ sub _save_file_column { =head1 NAME -DBIx::Class::InflateColumn::File - map files from the Database to the filesystem. +DBIx::Class::InflateColumn::File - DEPRECATED (superseded by DBIx::Class::InflateColumn::FS) + +=head2 Deprecation Notice + + This component has a number of architectural deficiencies that can quickly + drive your filesystem and database out of sync and is not recommended for + further use. It will be retained for backwards compatibility, but no new + functionality patches will be accepted. Please consider using the much more + mature and actively supported DBIx::Class::InflateColumn::FS. You can set + the environment variable DBIC_IC_FILE_NOWARN to a true value to disable + this warning. =head1 SYNOPSIS In your L table class: - __PACKAGE__->load_components( "PK::Auto", "InflateColumn::File", "Core" ); + use base 'DBIx::Class::Core'; + + __PACKAGE__->load_components(qw/InflateColumn::File/); # define your columns __PACKAGE__->add_columns( @@ -129,7 +169,7 @@ In your L table class: data_type => "varchar", is_file_column => 1, file_column_path =>'/tmp/uploaded_files', - # or for a Catalyst application + # or for a Catalyst application # file_column_path => MyApp->path_to('root','static','files'), default_value => undef, is_nullable => 1, @@ -143,11 +183,11 @@ In your L class: FileColumn requires a hash that contains L as handle and the file's name as name. - my $entry = $c->model('MyAppDB::Articles')->create({ + my $entry = $c->model('MyAppDB::Articles')->create({ subject => 'blah', - filename => { - handle => $c->req->upload('myupload')->fh, - filename => $c->req->upload('myupload')->basename + filename => { + handle => $c->req->upload('myupload')->fh, + filename => $c->req->upload('myupload')->basename }, body => '....' }); @@ -157,7 +197,7 @@ name as name. And Place the following in your TT template Article Subject: [% entry.subject %] - Uploaded File: + Uploaded File: File Body: [% entry.body %] @@ -174,20 +214,22 @@ InflateColumn::File =head2 _file_column_callback ($file,$ret,$target) -method made to be overridden for callback purposes. +Method made to be overridden for callback purposes. =cut sub _file_column_callback {} -=head1 AUTHOR +=head1 FURTHER QUESTIONS? -Victor Igumnov +Check the list of L. -=head1 LICENSE +=head1 COPYRIGHT AND LICENSE -This library is free software, you can redistribute it and/or modify -it under the same terms as Perl itself. +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. =cut