Dancer2-Plugin-EditFile

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Dancer2-Plugin-EditFile

0.005   Sat Feb 18 11:13:34 GMT 2017
        Save code with backups

0.004   Sat Feb 18 10:25:06 GMT 2017
        First build

0.003   Fri Jan 27 01:08:37 GMT 2017
        Added cpanfile

0.002   Wed Jan 25 00:37:01 GMT 2017
        Edited POD information
        New Config Entries

lib/Dancer2/Plugin/EditFile.pm  view on Meta::CPAN

=back

=head1 CONFIGURATION

You may specify the routes and access to files.  The plugin will only read/write files if it has access to them.  The following configuration will generate two routes: '/editfile/display' and '/editfile/save'.  

A sample HTML page with Bootstrap and jQuery is included in the samples directory.  Use it as an example  to build your own page.

  plugins:
    EditFile:
      backup:     1
      backup_dir: '/var/application/backups'
      display:
        method:    'get'
        route:     '/editfile/display'
        template:  'editfile.tt'
        layout:    'nomenu.tt'
      save:
        method:    'get'
        route:     '/editfile/save'
      files:
        id1:      

lib/Dancer2/Plugin/EditFile.pm  view on Meta::CPAN

          heading:  'View config script config2'
          file:    '/var/application/config2.txt'
          save:    0
        id3:      
          heading: 'View XML file config3.xml'
          file:    '/var/application/config3.xml'
          save:    0
      
=over 4

B<Note> the  user B<must> have read/write access to the file being edited and the backup directory.  

=back 

=over 4

=item I<backup>

Specify if original file should be saved.  Default = 0 (do not save)

=item I<backup_dir>

Directory where original files should be saved.  Default = /tmp

=item I<display>

Defines display settings.

=over 4

=item I<method>

lib/Dancer2/Plugin/EditFile.pm  view on Meta::CPAN


Specifies if file may be saved.  Default = 0 (view only)

=back 

=cut 

#
# Accessors
#
has backup => (
  is          => 'ro',
  isa         => Bool,
  from_config => 1,
  default     => sub { 0 }
);

has backup_dir => (
  is          => 'ro',
  isa         => Str,
  from_config => 1,
  default     => sub { '/tmp' }
);

has display_method => (
  is          => 'ro',
  isa         => Str,
  from_config => 'display.method',

lib/Dancer2/Plugin/EditFile.pm  view on Meta::CPAN


  my $status_message = "";

  my $file_id    = $app->request->params->{id};
  my $editedfile = $app->request->params->{editfile};
  my $files      = $plugin->files;

  if ( ! $files->{$file_id}->{file} ) {
    $status_message = "The specified id: $file_id is not properly defined in your configuration.";
  
  } elsif ( $plugin->{backup} && $plugin->{backup_dir} ) { 
    if ( -d $plugin->{backup_dir} ) {

      my $basename        = basename( $files->{$file_id}->{file} );
      my $backup_filename = $plugin->{backup_dir} 
                          . '/' 
                          . $basename 
                          . '.' 
                          . time;
       
      eval {
        copy($basename, $backup_filename);
      };
      if ( $@ ) {
        $status_message = "Could not save backup";
      } 

    }
  }

  # Write it if there are no errors
  if ( $status_message eq '' ) { 
    eval { 
      open ( my $EDITFILE_OUT, ">", $files->{$file_id}->{file} ) ;
      print $EDITFILE_OUT $editedfile;



( run in 1.251 second using v1.01-cache-2.11-cpan-49f99fa48dc )