| 1 | package DBIx::Class::InflateColumn::File; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use base 'DBIx::Class'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use File::Path; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use File::Copy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Path::Class; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use DBIx::Class::Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | 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 | 2 | ▶ | unless $ENV{DBIC_IC_FILE_NOWARN}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | __PACKAGE__->load_components(qw/InflateColumn/); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | sub register_column { | ||||||||||||||||||||
| 27 | 2 | ▶ | my ($self, $column, $info, @rest) = @_; | ||||||||||||||||||
|
Subroutine
register_column: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 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 | inflate => sub { | ||||||||||||||||||||
| 33 | 3 | ▶ | my ($value, $obj) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 3 | $obj->_inflate_file_column($column, $value); | |||||||||||||||||||
| 35 | }, | ||||||||||||||||||||
| 36 | deflate => sub { | ||||||||||||||||||||
| 37 | 1 | ▶ | my ($value, $obj) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | $obj->_save_file_column($column, $value); | |||||||||||||||||||
| 39 | }, | ||||||||||||||||||||
| 40 | 1 | }); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub _file_column_file { | ||||||||||||||||||||
| 44 | 7 | ▶ | my ($self, $column, $filename) = @_; | ||||||||||||||||||
|
Subroutine
_file_column_file: called CC=4 SCAR=14.8
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 7 | my $column_info = $self->column_info($column); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 7 | ▶ | return unless $column_info->{is_file_column}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 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')
| |||||||||||||||||||||
| 51 | 'id required for filename generation' | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 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"}
| |||||||||||||||||||||
| 55 | return Path::Class::file( | ||||||||||||||||||||
| 56 | 7 | $column_info->{file_column_path}, $id, $filename, | |||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub delete { | ||||||||||||||||||||
| 61 | 1 | ▶ | my ( $self, @rest ) = @_; | ||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 1 | for ( $self->columns ) { | |||||||||||||||||||
| 64 | 2 | ▶ | if ( $self->column_info($_)->{is_file_column} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 1 | rmtree( [$self->_file_column_file($_)->dir], 0, 0 ); | |||||||||||||||||||
| 66 | 1 | last; # if we've deleted one, we've deleted them all | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | return $self->next::method(@rest); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub insert { | ||||||||||||||||||||
| 74 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
insert: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # cache our file columns so we can write them to the fs | ||||||||||||||||||||
| 77 | # -after- we have a PK | ||||||||||||||||||||
| 78 | 2 | my %file_column; | |||||||||||||||||||
| 79 | 2 | for ( $self->columns ) { | |||||||||||||||||||
| 80 | 4 | ▶ | if ( $self->column_info($_)->{is_file_column} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 2 | $file_column{$_} = $self->$_; | |||||||||||||||||||
| 82 | 2 | $self->store_column($_ => $self->$_->{filename}); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | $self->next::method(@_); | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # write the files to the fs | ||||||||||||||||||||
| 89 | 2 | ▶ | while ( my ($col, $file) = each %file_column ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 2 | $self->_save_file_column($col, $file); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | return $self; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | sub _inflate_file_column { | ||||||||||||||||||||
| 98 | 3 | ▶ | my ( $self, $column, $value ) = @_; | ||||||||||||||||||
|
Subroutine
_inflate_file_column: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 3 | my $fs_file = $self->_file_column_file($column, $value); | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 3 | return { handle => $fs_file->open('r'), filename => $value }; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub _save_file_column { | ||||||||||||||||||||
| 106 | 3 | ▶ | my ( $self, $column, $value ) = @_; | ||||||||||||||||||
|
Subroutine
_save_file_column: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 3 | ▶ | return unless ref $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 3 | my $fs_file = $self->_file_column_file($column, $value->{filename}); | |||||||||||||||||||
| 111 | 3 | mkpath [$fs_file->dir]; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 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 | 3 | File::Copy::copy($value->{handle}, $fs_file->stringify); | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 3 | $self->_file_column_callback($value, $self, $column); | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 3 | return $value->{filename}; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | DBIx::Class::InflateColumn::File - DEPRECATED (superseded by DBIx::Class::InflateColumn::FS) | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head2 Deprecation Notice | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 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 | ||||||||||||||||||||
| 134 | this warning. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 SYNOPSIS | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | In your L<DBIx::Class> table class: | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | use base 'DBIx::Class::Core'; | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | __PACKAGE__->load_components(qw/InflateColumn::File/); | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | # define your columns | ||||||||||||||||||||
| 145 | __PACKAGE__->add_columns( | ||||||||||||||||||||
| 146 | "id", | ||||||||||||||||||||
| 147 | { | ||||||||||||||||||||
| 148 | data_type => "integer", | ||||||||||||||||||||
| 149 | is_auto_increment => 1, | ||||||||||||||||||||
| 150 | is_nullable => 0, | ||||||||||||||||||||
| 151 | size => 4, | ||||||||||||||||||||
| 152 | }, | ||||||||||||||||||||
| 153 | "filename", | ||||||||||||||||||||
| 154 | { | ||||||||||||||||||||
| 155 | data_type => "varchar", | ||||||||||||||||||||
| 156 | is_file_column => 1, | ||||||||||||||||||||
| 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, | ||||||||||||||||||||
| 161 | is_nullable => 1, | ||||||||||||||||||||
| 162 | size => 255, | ||||||||||||||||||||
| 163 | }, | ||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | In your L<Catalyst::Controller> class: | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | FileColumn requires a hash that contains L<IO::File> as handle and the file's | ||||||||||||||||||||
| 170 | name as name. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | my $entry = $c->model('MyAppDB::Articles')->create({ | ||||||||||||||||||||
| 173 | subject => 'blah', | ||||||||||||||||||||
| 174 | filename => { | ||||||||||||||||||||
| 175 | handle => $c->req->upload('myupload')->fh, | ||||||||||||||||||||
| 176 | filename => $c->req->upload('myupload')->basename | ||||||||||||||||||||
| 177 | }, | ||||||||||||||||||||
| 178 | body => '....' | ||||||||||||||||||||
| 179 | }); | ||||||||||||||||||||
| 180 | $c->stash->{entry}=$entry; | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | And Place the following in your TT template | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | Article Subject: [% entry.subject %] | ||||||||||||||||||||
| 186 | Uploaded File: | ||||||||||||||||||||
| 187 | <a href="/static/files/[% entry.id %]/[% entry.filename.filename %]">File</a> | ||||||||||||||||||||
| 188 | Body: [% entry.body %] | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 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. | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head1 DESCRIPTION | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | InflateColumn::File | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =head1 METHODS | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head2 _file_column_callback ($file,$ret,$target) | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | Method made to be overridden for callback purposes. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | ▶ | sub _file_column_callback {} | |||||||||||||||||||
|
Subroutine
_file_column_callback: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head1 AUTHOR | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Victor Igumnov | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =head1 LICENSE | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | This library is free software, you can redistribute it and/or modify | ||||||||||||||||||||
| 216 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 2 | 1; | |||||||||||||||||||