]> wagnertech.de Git - kivitendo-erp.git/commitdiff
FileManagement: Konsistenzprüfung zwischen Backend und Datenbank, hier Backend Filesystem
authorMartin Helmling martin.helmling@octosoft.eu <martin.helmling@octosoft.eu>
Mon, 4 Sep 2017 07:56:21 +0000 (09:56 +0200)
committerMartin Helmling martin.helmling@octosoft.eu <martin.helmling@octosoft.eu>
Mon, 4 Sep 2017 07:56:21 +0000 (09:56 +0200)
Das script 'scripts/sync_files_from_backend.pl' prüft, ob die Dateien im Backend noch vorhanden sind.
Dabei wird nach der aktuellsten Version gesucht, ist diese vorhanden ist ok,
ist diese nicht vorhanden werden die Versionen davor gesucht und ggf. die Version in der DB heruntergesetzt.
Ist keine Dateimehr vorhanden wird der Datenbankrecord gelöscht.

fixt #275

SL/File.pm
SL/File/Backend/Filesystem.pm
scripts/sync_files_from_backend.pl [new file with mode: 0755]

index 79ca3fbeb3dcea8d7d4b4532e36f189ec76fb575..5efb403bd52619a24b0b36c9a5b0a7bbc9ef6cc3 100644 (file)
@@ -307,7 +307,7 @@ sub sync_from_backend {
   return unless $params{file_type};
   my $file = SL::DB::File->new;
   $file->file_type($params{file_type});
-  my $backend = $self->_get_backend(dbfile => $file->backend);
+  my $backend = $self->_get_backend($self->_get_backend_by_file_type($file));
   return unless $backend;
   $backend->sync_from_backend(%params);
 }
index 68263b88e6130f67ff66f18ba8b90e88c3334989..70d956e0a6b08f74a1f08d1bf7e8e86ecddf006b 100644 (file)
@@ -111,6 +111,34 @@ sub enabled {
   return 1;
 }
 
+sub sync_from_backend {
+  my ($self, %params) = @_;
+  my @query = (file_type => $params{file_type});
+  push @query, (file_name => $params{file_name}) if $params{file_name};
+  push @query, (mime_type => $params{mime_type}) if $params{mime_type};
+  push @query, (source    => $params{source})    if $params{source};
+
+  my $sortby = $params{sort_by} || 'itime DESC,file_name ASC';
+
+  my @files = @{ SL::DB::Manager::File->get_all(query => [@query], sort_by => $sortby) };
+  for (@files) {
+    $main::lxdebug->message(LXDebug->DEBUG2(), "file id=" . $_->id." version=".$_->backend_data);
+    my $newversion = $_->backend_data;
+    for my $version ( reverse 1 .. $_->backend_data ) {
+      my $path = $self->_filesystem_path($_, $version);
+      $main::lxdebug->message(LXDebug->DEBUG2(), "path=".$path." exists=".( -f $path?1:0));
+      last if -f $path;
+      $newversion = $version - 1;
+    }
+    $main::lxdebug->message(LXDebug->DEBUG2(), "newversion=".$newversion." version=".$_->backend_data);
+    if ( $newversion < $_->backend_data ) {
+      $_->backend_data($newversion);
+      $_->save   if $newversion >  0;
+      $_->delete if $newversion <= 0;
+    }
+  }
+
+}
 
 #
 # internals
diff --git a/scripts/sync_files_from_backend.pl b/scripts/sync_files_from_backend.pl
new file mode 100755 (executable)
index 0000000..26626e1
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/perl
+
+BEGIN {
+  if (! -d "bin" || ! -d "SL") {
+    print("This tool must be run from the kivitendo ERP base directory.\n");
+    exit(1);
+  }
+
+  unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
+  push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
+}
+
+
+use strict;
+use warnings;
+use utf8;
+use English '-no_match_vars';
+use POSIX qw(setuid setgid);
+use Text::CSV_XS;
+
+use Config::Std;
+use DBI;
+use SL::LXDebug;
+use SL::LxOfficeConf;
+
+use SL::DBUtils;
+use SL::Auth;
+use SL::Form;
+use SL::User;
+use SL::Locale;
+use SL::File;
+use SL::InstanceConfiguration;
+use Getopt::Long;
+use Pod::Usage;
+use Term::ANSIColor;
+
+my %config;
+
+sub parse_args {
+  my ($options) = @_;
+  GetOptions(
+    'client=s'          => \ my $client,
+  );
+
+  $options->{client}   = $client;
+}
+
+sub setup {
+
+  SL::LxOfficeConf->read;
+
+  my $client = $config{client} || $::lx_office_conf{devel}{client};
+
+  if (!$client) {
+    error("No client found in config. Please provide a client:");
+    usage();
+  }
+
+  $::lxdebug      = LXDebug->new();
+  $::locale       = Locale->new("de");
+  $::form         = new Form;
+  $::auth         = SL::Auth->new();
+
+  if (!$::auth->set_client($client)) {
+    error("No client with ID or name '$client' found in config. Please provide a client:");
+    usage();
+  }
+  $::instance_conf = SL::InstanceConfiguration->new;
+  $::instance_conf->init;
+}
+
+sub error {
+  print STDERR colored(shift, 'red'), $/;
+}
+
+sub usage {
+  print STDERR "scripts/sync_files_from_backend.pl --client name-or-id\n" ;
+  exit 1;
+}
+
+parse_args(\%config);
+setup();
+
+SL::File->sync_from_backend( file_type => 'document');
+SL::File->sync_from_backend( file_type => 'attachments');
+SL::File->sync_from_backend( file_type => 'images');
+
+1;