| 1 | package DBIx::Class::InflateColumn::File; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use base 'DBIx::Class'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use File::Path; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use File::Copy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use Path::Class; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use DBIx::Class::Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 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 | 3 | ▶ | unless $ENV{DBIC_IC_FILE_NOWARN}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 3 | __PACKAGE__->load_components(qw/InflateColumn/); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | ▶ | sub register_column { | ||||||||||||||||||
|
Subroutine
register_column: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 27 | 2 | my ($self, $column, $info, @rest) = @_; | |||||||||||||||||||
| 28 | 2 | $self->next::method($column, $info, @rest); | |||||||||||||||||||
| 29 | 2 | ▶ | return unless defined($info->{is_file_column}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | $self->inflate_column($column => { | ||||||||||||||||||||
| 32 | 3 | ▶ | inflate => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 3 | my ($value, $obj) = @_; | |||||||||||||||||||
| 34 | 3 | $obj->_inflate_file_column($column, $value); | |||||||||||||||||||
| 35 | }, | ||||||||||||||||||||
| 36 | 1 | ▶ | deflate => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | my ($value, $obj) = @_; | |||||||||||||||||||
| 38 | 1 | $obj->_save_file_column($column, $value); | |||||||||||||||||||
| 39 | }, | ||||||||||||||||||||
| 40 | 1 | }); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 7 | ▶ | sub _file_column_file { | ||||||||||||||||||
|
Subroutine
_file_column_file: called CC=4 SCAR=14.8
| |||||||||||||||||||||
| 44 | 7 | my ($self, $column, $filename) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 7 | my $column_info = $self->result_source->column_info($column); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 7 | ▶ | return unless $column_info->{is_file_column}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # DO NOT CHANGE | ||||||||||||||||||||
| 51 | # This call to id() is generally incorrect - will not DTRT on | ||||||||||||||||||||
| 52 | # multicolumn key. However changing this may introduce | ||||||||||||||||||||
| 53 | # backwards-comp regressions, thus leaving as is | ||||||||||||||||||||
| 54 | 7 | ▶ | my $id = $self->id || $self->throw_exception( | ||||||||||||||||||
|
Condition33%1/3
$self->id || $self->throw_exception('id required for filename generation')
MC/DC0%0/2
$self->id || $self->throw_exception('id required for filename generation')
$self->id
$self->throw_exception('id required for filename generation')
Truth table33%1/3
A || B
A= $self->idB= $self->throw_exception('id required for filename generation')
| |||||||||||||||||||||
| 55 | 'id required for filename generation' | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 7 | ▶ | $filename ||= $self->$column->{filename}; | ||||||||||||||||||
|
Condition66%2/3
$filename ||= $self->$column->{"filename"}
MC/DC0%0/2
$filename ||= $self->$column->{"filename"}
$filename
$self->$column->{"filename"}
Truth table66%2/3
A ||= B
A= $filenameB= $self->$column->{"filename"}
| |||||||||||||||||||||
| 59 | return Path::Class::file( | ||||||||||||||||||||
| 60 | 7 | $column_info->{file_column_path}, $id, $filename, | |||||||||||||||||||
| 61 | ); | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub delete { | ||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 65 | 1 | my ( $self, @rest ) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | my $colinfos = $self->result_source->columns_info; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | for ( keys %$colinfos ) { | |||||||||||||||||||
| 70 | 2 | ▶ | if ( $colinfos->{$_}{is_file_column} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 1 | rmtree( [$self->_file_column_file($_)->dir], 0, 0 ); | |||||||||||||||||||
| 72 | 1 | last; # if we've deleted one, we've deleted them all | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | return $self->next::method(@rest); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 2 | ▶ | sub insert { | ||||||||||||||||||
|
Subroutine
insert: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # cache our file columns so we can write them to the fs | ||||||||||||||||||||
| 83 | # -after- we have a PK | ||||||||||||||||||||
| 84 | 2 | my $colinfos = $self->result_source->columns_info; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | my %file_column; | |||||||||||||||||||
| 87 | 2 | for ( keys %$colinfos ) { | |||||||||||||||||||
| 88 | 4 | ▶ | if ( $colinfos->{$_}{is_file_column} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 2 | $file_column{$_} = $self->$_; | |||||||||||||||||||
| 90 | 2 | $self->store_column($_ => $self->$_->{filename}); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 2 | $self->next::method(@_); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # write the files to the fs | ||||||||||||||||||||
| 97 | 2 | ▶ | while ( my ($col, $file) = each %file_column ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 2 | $self->_save_file_column($col, $file); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 2 | return $self; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 3 | ▶ | sub _inflate_file_column { | ||||||||||||||||||
|
Subroutine
_inflate_file_column: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 3 | my ( $self, $column, $value ) = @_; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 3 | my $fs_file = $self->_file_column_file($column, $value); | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 3 | return { handle => $fs_file->open('r'), filename => $value }; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 3 | ▶ | sub _save_file_column { | ||||||||||||||||||
|
Subroutine
_save_file_column: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 114 | 3 | my ( $self, $column, $value ) = @_; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 3 | ▶ | return unless ref $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 3 | my $fs_file = $self->_file_column_file($column, $value->{filename}); | |||||||||||||||||||
| 119 | 3 | mkpath [$fs_file->dir]; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # File::Copy doesn't like Path::Class (or any for that matter) objects, | ||||||||||||||||||||
| 122 | # thus ->stringify (http://rt.perl.org/rt3/Public/Bug/Display.html?id=59650) | ||||||||||||||||||||
| 123 | 3 | File::Copy::copy($value->{handle}, $fs_file->stringify); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 3 | $self->_file_column_callback($value, $self, $column); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 3 | return $value->{filename}; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 NAME | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | DBIx::Class::InflateColumn::File - DEPRECATED (superseded by DBIx::Class::InflateColumn::FS) | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 Deprecation Notice | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | This component has a number of architectural deficiencies that can quickly | ||||||||||||||||||||
| 137 | drive your filesystem and database out of sync and is not recommended for | ||||||||||||||||||||
| 138 | further use. It will be retained for backwards compatibility, but no new | ||||||||||||||||||||
| 139 | functionality patches will be accepted. Please consider using the much more | ||||||||||||||||||||
| 140 | mature and actively supported DBIx::Class::InflateColumn::FS. You can set | ||||||||||||||||||||
| 141 | the environment variable DBIC_IC_FILE_NOWARN to a true value to disable | ||||||||||||||||||||
| 142 | this warning. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =head1 SYNOPSIS | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | In your L<DBIx::Class> table class: | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | use base 'DBIx::Class::Core'; | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | __PACKAGE__->load_components(qw/InflateColumn::File/); | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | # define your columns | ||||||||||||||||||||
| 153 | __PACKAGE__->add_columns( | ||||||||||||||||||||
| 154 | "id", | ||||||||||||||||||||
| 155 | { | ||||||||||||||||||||
| 156 | data_type => "integer", | ||||||||||||||||||||
| 157 | is_auto_increment => 1, | ||||||||||||||||||||
| 158 | is_nullable => 0, | ||||||||||||||||||||
| 159 | size => 4, | ||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||
| 161 | "filename", | ||||||||||||||||||||
| 162 | { | ||||||||||||||||||||
| 163 | data_type => "varchar", | ||||||||||||||||||||
| 164 | is_file_column => 1, | ||||||||||||||||||||
| 165 | file_column_path =>'/tmp/uploaded_files', | ||||||||||||||||||||
| 166 | # or for a Catalyst application | ||||||||||||||||||||
| 167 | # file_column_path => MyApp->path_to('root','static','files'), | ||||||||||||||||||||
| 168 | default_value => undef, | ||||||||||||||||||||
| 169 | is_nullable => 1, | ||||||||||||||||||||
| 170 | size => 255, | ||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | In your L<Catalyst::Controller> class: | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | FileColumn requires a hash that contains L<IO::File> as handle and the file's | ||||||||||||||||||||
| 178 | name as name. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | my $entry = $c->model('MyAppDB::Articles')->create({ | ||||||||||||||||||||
| 181 | subject => 'blah', | ||||||||||||||||||||
| 182 | filename => { | ||||||||||||||||||||
| 183 | handle => $c->req->upload('myupload')->fh, | ||||||||||||||||||||
| 184 | filename => $c->req->upload('myupload')->basename | ||||||||||||||||||||
| 185 | }, | ||||||||||||||||||||
| 186 | body => '....' | ||||||||||||||||||||
| 187 | }); | ||||||||||||||||||||
| 188 | $c->stash->{entry}=$entry; | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | And Place the following in your TT template | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Article Subject: [% entry.subject %] | ||||||||||||||||||||
| 194 | Uploaded File: | ||||||||||||||||||||
| 195 | <a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a> | ||||||||||||||||||||
| 196 | Body: [% entry.body %] | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | The file will be stored on the filesystem for later retrieval. Calling delete | ||||||||||||||||||||
| 199 | on your resultset will delete the file from the filesystem. Retrevial of the | ||||||||||||||||||||
| 200 | record automatically inflates the column back to the set hash with the | ||||||||||||||||||||
| 201 | IO::File handle and filename. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 DESCRIPTION | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | InflateColumn::File | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 METHODS | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head2 _file_column_callback ($file,$ret,$target) | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Method made to be overridden for callback purposes. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 3 | ▶ | sub _file_column_callback {} | ||||||||||||||||||
|
Subroutine
_file_column_callback: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head1 FURTHER QUESTIONS? | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> | ||||||||||||||||||||
| 224 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can | ||||||||||||||||||||
| 225 | redistribute it and/or modify it under the same terms as the | ||||||||||||||||||||
| 226 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 3 | 1; | |||||||||||||||||||