App-CSE

 view release on metacpan or  search on metacpan

lib/App/CSE/File.pm  view on Meta::CPAN

    $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.
This is a Class method.

You can also give a file name to make the diagnostic easier in case no
class is found.

Usage:

  my $class = App::CSE::File->class_for_mime('application/x-perl');
  my $class = App::CSE::File->class_for_mime('application/x-perl' , 'the/file/name.something');

=cut

sub class_for_mime{
  my ($class, $mime_type , $file_name) = @_;

  $file_name ||= 'unknown_file_name';

  my $half_camel = $mime_type; $half_camel =~ s/\W/_/g;
  my $file_class_name = 'App::CSE::File::'.String::CamelCase::camelize($half_camel);
  # Protect against unsecure class name
  ( $file_class_name ) = ( $file_class_name =~ /^([\w:]+)$/ );
  my $file_class = eval{ Class::Load::load_class($file_class_name); };
  unless( $file_class ){
    # A bit dirty, but well..
    $LOGGER->debug(App::CSE->instance()->colorizer->colored("No class '$file_class_name' for mimetype $mime_type ($file_name)",'red bold'));
    return undef;
  }
  return $file_class;
}

__PACKAGE__->meta->make_immutable();

__END__

=head1 NAME

App::CSE::File - A general file

=head1 METHODS

=head2 effective_object

Effective Object. Some classes can choose to return something different.

=cut



( run in 1.474 second using v1.01-cache-2.11-cpan-d8267643d1d )