| 1 | package Yancy::Plugin::File; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 20 | our $VERSION = '1.088'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Manage file uploads, attachments, and other assets | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 7 | #pod # Write a file | ||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod $c->yancy->file->write( $c->param( 'upload' ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod B<Note:> This module is C<EXPERIMENTAL> and its API may change before | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod Yancy v2.000 is released. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod This plugin manages file uploads. Files are stored in the C<file_root> by | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod This plugin API is meant to be subclassed by other asset storage | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod mechanisms such as Hadoop or Amazon S3. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =head2 Cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod Files are B<NOT> immediately deleted after they are no longer needed. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod Instead, a L</cleanup> method exists to periodically clean up any files | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod that are not referenced. You should schedule this to run daily or weekly | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod in cron: | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod # Clean up files every week | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod 0 0 * * 0 ./myapp.pl eval 'app->yancy->file->cleanup( app->yancy->backend, app->yancy->schema )' | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod This plugin has the following configuration options. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod =head2 file_root | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod The root path to store files. Defaults to C<public/uploads> in the application's home | ||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod directory. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod =head2 url_root | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod The URL used to reach the C<file_root>. Defaults to C</uploads>. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod =head2 moniker | ||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod The name to use for the helper. Defaults to C<file> (creating a C<yancy.file> helper). | ||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod Change this to add multiple file plugins. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod L<Yancy> | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 20 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 20 | ▶ | use Yancy::Util qw( currym is_type ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 20 | ▶ | use Digest; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 20 | ▶ | use Mojo::Asset::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | ▶ | use Mojo::File qw( path ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 20 | has file_root =>; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | has url_root =>; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 20 | has digest_type => 'SHA-1'; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 20 | has moniker => 'file'; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 53 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 53 | my ( $self, $app, $config ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 53 | ▶ | my $file_root = $config->{file_root} ? path( $config->{file_root} ) : $app->home->child( 'public/uploads' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 53 | $self->file_root( $file_root ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 53 | ▶ | my $url_root = $config->{url_root} // '/uploads'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"url_root"} // "/uploads"
MC/DC100%1/1
$config->{"url_root"} // "/uploads"
$config->{"url_root"}
Truth table100%2/2
A
A= $config->{"url_root"}
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 53 | $self->url_root( $url_root ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 53 | ▶ | my $moniker = $config->{moniker} // 'file'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"moniker"} // "file"
MC/DC0%0/1
$config->{"moniker"} // "file"
$config->{"moniker"}
Truth table50%1/2
A
A= $config->{"moniker"}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 53 | $self->moniker( $moniker ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 53 | ▶ | $app->helper( 'yancy.' . $moniker, sub { $self } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod =method write | ||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod $url_path = $c->yancy->file->write( $upload ); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod $url_path = $c->yancy->file->write( $name, $asset ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 81 | #pod Write a file into storage. C<$upload> is a L<Mojo::Upload> object. C<$name> | ||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod is a filename and C<$asset> is a L<Mojo::Asset> object. Returns the URL | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod of the uploaded file. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my ( $self, $name, $asset ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | if ( ref $name eq 'Mojo::Upload' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | $asset = $name->asset; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | $name = $name->filename; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my $digest = $self->_digest_file( $asset ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | my @path_parts = grep $_, split /(..)/, $digest, 3; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | my $root = $self->file_root; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | my $path = $root->child( @path_parts )->make_path; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my $file_path = $path->child( $name ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | $file_path->spurt( $asset->slurp ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | return join '/', $self->url_root, $file_path->to_rel( $root ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | #pod =method cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 104 | #pod $app->yancy->file->cleanup( $app->yancy->backend ); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | #pod $app->yancy->file->cleanup( $app->yancy->backend, $app->yancy->schema ); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 107 | #pod Clean up any files that do not exist in the given backend. Call this daily | ||||||||||||||||||||||||||||||||||||||||||
| 108 | #pod or weekly to remove files that aren't needed anymore. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 110 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub cleanup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cleanup: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | my ( $self, $backend, $schema ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | $schema ||= $backend->schema; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$schema ||= $backend->schema
MC/DC0%0/2
$schema ||= $backend->schema
$schema
$backend->schema
Truth table33%1/3
A ||= B
A= $schemaB= $backend->schema
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Clean up any unlinked files by scanning the entire database for | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # files and then leaving only those files. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | my ( %files, %linked ); | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # List all the files | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | for my $path ( $self->file_root->list_tree->each ) { | |||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | $files{ $path }++; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | # Find all the linked files | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | for my $schema_name ( keys %$schema ) { | |||||||||||||||||||||||||||||||||||||||||
| 126 | 6 | my @path_fields; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | for my $property_name ( keys %{ $schema->{$schema_name}{properties} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 28 | my $prop = $schema->{$schema_name}{properties}{$property_name}; | |||||||||||||||||||||||||||||||||||||||||
| 129 | # ; use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # ; say "Checking prop $property_name: " . Dumper $prop; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 28 | ▶ | if ( is_type( $prop->{type}, 'string' ) && $prop->{format} && $prop->{format} eq 'filepath' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} eq "filepath"
is_type($prop->{"type"}, "string") and $prop->{"format"}
MC/DC100%3/3
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} eq "filepath"
is_type($prop->{"type"}, "string")
$prop->{"format"}
$prop->{"format"} eq "filepath"
Truth table100%4/4
A and B and C
A= is_type($prop->{"type"}, "string")B= $prop->{"format"}C= $prop->{"format"} eq "filepath"
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | push @path_fields, $property_name; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # ; say "Got path fields: @path_fields"; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | next if !@path_fields; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | # Fetch the rows with values in the path, slowly so that we | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # don't try to take up all the memory in the database | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | my $per_page = 50; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | my $file_root = $self->file_root; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my $url_root = $self->url_root; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | my $items = $backend->list( $schema_name, {}, { limit => $per_page } ); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | while ( $i < $items->{total} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | for my $item ( @{ $items->{items} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | for my $field ( @path_fields ) { | |||||||||||||||||||||||||||||||||||||||||
| 149 | # Add to linked records | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | my $path = $item->{ $field }; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | $path =~ s{^$url_root}{$file_root}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | $linked{ $path }++; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | $i += @{ $items->{items} }; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | $items = $backend->list( $schema_name, {}, { offset => $i, limit => $per_page } ); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | # Any file that does not have a link must be deleted | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | delete $files{ $_ } for keys %linked; | |||||||||||||||||||||||||||||||||||||||||
| 162 | # ; use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # ; say "Linked: " . Dumper [ keys %linked ]; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # ; say "Deleting: " . Dumper [ keys %files ]; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | for my $path ( keys %files ) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | path( $path )->dirname->dirname->dirname->remove_tree; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 4 | ▶ | sub _digest_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_digest_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | my ( $self, $asset ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 174 | # Using hex instead of base64 to support case-insensitive file | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # systems | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | my $digest = Digest->new( $self->digest_type )->add( $asset->slurp )->hexdigest; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | return $digest; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 20 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||||||||||||||||