From: Martin Helmling martin.helmling@octosoft.eu Date: Mon, 4 Sep 2017 07:56:21 +0000 (+0200) Subject: FileManagement: Konsistenzprüfung zwischen Backend und Datenbank, hier Backend Filesystem X-Git-Tag: release-3.5.4~836 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=cff4d3332201b41a504878734475f7c9328a15be;p=kivitendo-erp.git FileManagement: Konsistenzprüfung zwischen Backend und Datenbank, hier Backend Filesystem 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 --- diff --git a/SL/File.pm b/SL/File.pm index 79ca3fbeb..5efb403bd 100644 --- a/SL/File.pm +++ b/SL/File.pm @@ -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); } diff --git a/SL/File/Backend/Filesystem.pm b/SL/File/Backend/Filesystem.pm index 68263b88e..70d956e0a 100644 --- a/SL/File/Backend/Filesystem.pm +++ b/SL/File/Backend/Filesystem.pm @@ -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 index 000000000..26626e1b2 --- /dev/null +++ b/scripts/sync_files_from_backend.pl @@ -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;