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;
}

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.098 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )