App-CSE
view release on metacpan or search on metacpan
lib/App/CSE/File.pm view on Meta::CPAN
package App::CSE::File;
$App::CSE::File::VERSION = '0.016';
use Moose;
use App::CSE;
use Class::Load;
use Encode;
use File::Basename;
use File::Slurp;
use File::stat qw//;
use DateTime;
use String::CamelCase;
use Log::Log4perl;
my $LOGGER = Log::Log4perl->get_logger();
has 'cse' => ( is => 'ro' , isa => 'App::CSE' , required => 1 );
has 'mime_type' => ( is => 'ro', isa => 'Str', required => 1);
has 'file_path' => ( is => 'ro', isa => 'Str', required => 1);
has 'dir' => ( is => 'ro' , isa => 'Str' , required => 1, lazy_build => 1);
has 'encoding' => ( is => 'ro' , isa => 'Str', lazy_build => 1 );
has 'content' => ( is => 'ro' , isa => 'Maybe[Str]', required => 1, lazy_build => 1 );
has 'raw_content' => ( is => 'ro' , isa => 'Maybe[Str]', required => 1 , lazy_build => 1);
has 'decl' => ( is => 'ro' , isa => 'ArrayRef[Str]' , lazy_build => 1);
has 'call' => ( is => 'ro' , isa => 'ArrayRef[Str]' , lazy_build => 1);
has 'stat' => ( is => 'ro' , isa => 'File::stat' , lazy_build => 1 );
has 'mtime' => ( is => 'ro' , isa => 'DateTime' , lazy_build => 1);
sub _build_decl{
return [];
}
sub _build_call{
return [];
}
sub _build_dir{
my ($self) = @_;
return File::Basename::dirname($self->file_path());
}
sub _build_stat{
my ($self) = @_;
return File::stat::stat($self->file_path());
}
sub _build_mtime{
my ($self) = @_;
return DateTime->from_epoch( epoch => $self->stat->mtime() );
}
sub _build_encoding{
my ($self) = @_;
## This is the default. Override that in specific file types
return 'UTF-8';
}
sub _build_raw_content{
my ($self) = @_;
if( $self->stat()->size() > $self->cse()->max_size() ){
return undef;
}
return scalar( File::Slurp::read_file($self->file_path(), binmode => ':raw') );
}
sub _build_content{
my ($self) = @_;
my $raw_content = $self->raw_content();
unless( defined($raw_content) ){ return undef; }
my $decoded = eval{ Encode::decode($self->encoding(), $raw_content, Encode::FB_CROAK ); };
unless( $decoded ){
$LOGGER->debug("File ".$self->file_path()." failed to be decoded as ".$self->encoding().": ".$@);
return;
}
return $decoded;
}
sub effective_object{
my ($self) = @_;
return $self;
}
=head2 requalify
Requalifies this object into the given mimetype.
=cut
sub requalify{
my ($self, $mimetype) = @_;
my $class = __PACKAGE__->class_for_mime($mimetype, $self->file_path());
unless( $class ){
confess("Cannot requalify in $mimetype. No class found");
}
return $class->new({ cse => $self->cse(),
file_path => $self->file_path(),
mime_type => $mimetype,
( $self->has_dir() ? ( dir => $self->dir() ) : () ),
( $self->has_content() ? ( content => $self->content() ) : () )
});
}
=head2 class_for_mime
Returns the File subclass that goes well with the given mimetype.
( run in 0.479 second using v1.01-cache-2.11-cpan-39bf76dae61 )