| 1 | package Silki::Schema::File; | ||
| 2 | { | ||
| 3 | $Silki::Schema::File::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use File::MimeInfo qw( describe ); | ||
| 11 | use List::AllUtils qw( any ); | ||
| 12 | use Silki::Config; | ||
| 13 | use Silki::I18N qw( loc ); | ||
| 14 | use Silki::Schema; | ||
| 15 | use Silki::Types qw( Bool ); | ||
| 16 | |||
| 17 | use Fey::ORM::Table; | ||
| 18 | |||
| 19 | with 'Silki::Role::Schema::URIMaker'; | ||
| 20 | |||
| 21 | with 'Silki::Role::Schema::SystemLogger' => { methods => ['delete'] }; | ||
| 22 | |||
| 23 | with 'Silki::Role::Schema::DataValidator' => { | ||
| 24 | steps => [ | ||
| 25 | '_filename_is_unique_for_page', | ||
| 26 | ], | ||
| 27 | }; | ||
| 28 | |||
| 29 | my $Schema = Silki::Schema->Schema(); | ||
| 30 | |||
| 31 | has_policy 'Silki::Schema::Policy'; | ||
| 32 | |||
| 33 | has_table( $Schema->table('File') ); | ||
| 34 | |||
| 35 | has_one( $Schema->table('User') ); | ||
| 36 | |||
| 37 | has_one page => ( | ||
| 38 | table => $Schema->table('Page'), | ||
| 39 | handles => ['wiki'], | ||
| 40 | ); | ||
| 41 | |||
| 42 | has is_displayable_in_browser => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => Bool, | ||
| 45 | init_arg => undef, | ||
| 46 | lazy => 1, | ||
| 47 | builder => '_build_is_displayable_in_browser', | ||
| 48 | ); | ||
| 49 | |||
| 50 | has is_browser_displayable_image => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => Bool, | ||
| 53 | init_arg => undef, | ||
| 54 | lazy => 1, | ||
| 55 | builder => '_build_is_browser_displayable_image', | ||
| 56 | ); | ||
| 57 | |||
| 58 | with 'Silki::Role::Schema::File'; | ||
| 59 | |||
| 60 | with 'Silki::Role::Schema::Serializes' => { | ||
| 61 | skip => ['contents'], | ||
| 62 | }; | ||
| 63 | |||
| 64 | sub _system_log_values_for_delete { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my $msg | ||
| 68 | = 'Deleted file, ' | ||
| 69 | . $self->filename() | ||
| 70 | . ', attached to ' | ||
| 71 | . $self->page()->title() . ' in ' | ||
| 72 | . $self->wiki()->title(); | ||
| 73 | |||
| 74 | return ( | ||
| 75 | page_id => $self->page_id(), | ||
| 76 | message => $msg, | ||
| 77 | data_blob => { | ||
| 78 | filename => $self->filename(), | ||
| 79 | mime_type => $self->mime_type(), | ||
| 80 | file_size => $self->file_size(), | ||
| 81 | }, | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _filename_is_unique_for_page { | ||
| 86 | my $self = shift; | ||
| 87 | my $p = shift; | ||
| 88 | my $is_insert = shift; | ||
| 89 | |||
| 90 | return | ||
| 91 | if !$is_insert | ||
| 92 | && exists $p->{filename} | ||
| 93 | && $p->{filename} eq $self->filename(); | ||
| 94 | |||
| 95 | return unless exists $p->{filename}; | ||
| 96 | |||
| 97 | return | ||
| 98 | unless __PACKAGE__->new( | ||
| 99 | filename => $p->{filename}, | ||
| 100 | page_id => $p->{page_id}, | ||
| 101 | ); | ||
| 102 | |||
| 103 | return { | ||
| 104 | message => loc( | ||
| 105 | 'The filename you provided is already in use for another file on this page.' | ||
| 106 | ), | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _base_uri_path { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | return $self->wiki()->_base_uri_path() . '/file/' . $self->file_id(); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub mime_type_description_for_lang { | ||
| 117 | my $self = shift; | ||
| 118 | my $lang = shift; | ||
| 119 | |||
| 120 | my $desc = describe( $self->mime_type(), $lang ); | ||
| 121 | $desc ||= describe( $self->mime_type() ); | ||
| 122 | |||
| 123 | return $desc; | ||
| 124 | } | ||
| 125 | |||
| 126 | { | ||
| 127 | my %browser_image = map { $_ => 1 } qw( image/gif image/jpeg image/png ); | ||
| 128 | |||
| 129 | sub _build_is_browser_displayable_image { | ||
| 130 | return $browser_image{ $_[0]->mime_type() }; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | { | ||
| 135 | my @displayable = ( | ||
| 136 | qr{^text/}, | ||
| 137 | qr{^application/ecmascript$}, | ||
| 138 | qr{^application/javascript$}, | ||
| 139 | qr{^application/x-httpd-php.*}, | ||
| 140 | qr{^application/x-perl$}, | ||
| 141 | qr{^application/x-ruby$}, | ||
| 142 | qr{^application/x-shellscript$}, | ||
| 143 | qr{^application/sgml}, | ||
| 144 | qr{^application/xml}, | ||
| 145 | qr{^application/.+\+xml$} | ||
| 146 | ); | ||
| 147 | |||
| 148 | sub _build_is_displayable_in_browser { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | my $type = $self->mime_type(); | ||
| 152 | |||
| 153 | return $self->is_browser_displayable_image() | ||
| 154 | || any { $type =~ $_ } @displayable; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | around _build_small_image_file => sub { | ||
| 159 | my $orig = shift; | ||
| 160 | my $self = shift; | ||
| 161 | |||
| 162 | return unless $self->is_browser_displayable_image(); | ||
| 163 | |||
| 164 | return $self->$orig(@_); | ||
| 165 | }; | ||
| 166 | |||
| 167 | around _build_thumbnail_file => sub { | ||
| 168 | my $orig = shift; | ||
| 169 | my $self = shift; | ||
| 170 | |||
| 171 | return unless $self->is_browser_displayable_image(); | ||
| 172 | |||
| 173 | return $self->$orig(@_); | ||
| 174 | }; | ||
| 175 | |||
| 176 | __PACKAGE__->meta()->make_immutable(); | ||
| 177 | |||
| 178 | 1; | ||
| 179 | |||
| 180 | # ABSTRACT: Represents a file | ||
| 181 | |||
| 182 | __END__ |