From f8138d1721521673da949aeac440011dfe0a22c8 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 5 May 2009 07:42:48 +0000 Subject: [PATCH] =?utf8?q?Jeden=20DATEV-Export=20in=20ein=20eigenes,=20ein?= =?utf8?q?deutig=20benanntes=20Verzeichnis=20schreiben,=20damit=20sich=20D?= =?utf8?q?ATEV-Exporte=20nicht=20gegenseitig=20=C3=BCberschreiben.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Temporäre Verzeichnisse werden beim nächsten Aufruf gelöscht, sofern sie älter als acht Stunden sind. Fix für Bug 924. --- SL/DATEV.pm | 76 +++++++++++++++++++++++++++++++++++++++----- bin/mozilla/datev.pl | 61 +++++++++++++++++++++-------------- 2 files changed, 106 insertions(+), 31 deletions(-) diff --git a/SL/DATEV.pm b/SL/DATEV.pm index d6dd7c629..d600b20ff 100644 --- a/SL/DATEV.pm +++ b/SL/DATEV.pm @@ -30,6 +30,66 @@ use SL::DBUtils; use SL::DATEV::KNEFile; use Data::Dumper; +use File::Path; +use Time::HiRes qw(gettimeofday); + +sub _get_export_path { + $main::lxdebug->enter_sub(); + + my ($a, $b) = gettimeofday(); + my $path = get_path_for_download_token("${a}-${b}-${$}"); + + mkpath($path) unless (-d $path); + + $main::lxdebug->leave_sub(); + + return $path; +} + +sub get_path_for_download_token { + $main::lxdebug->enter_sub(); + + my $token = shift; + my $path; + + if ($token =~ m|^(\d+)-(\d+)-(\d+)$|) { + $path = "${main::userspath}/datev-export-${1}-${2}-${3}"; + } + + $main::lxdebug->leave_sub(); + + return $path; +} + +sub get_download_token_for_path { + $main::lxdebug->enter_sub(); + + my $path = shift; + my $token; + + if ($path =~ m|.*datev-export-(\d+)-(\d+)-(\d+)/?$|) { + $token = "${1}-${2}-${3}"; + } + + $main::lxdebug->leave_sub(); + + return $token; +} + +sub clean_temporary_directories { + $main::lxdebug->enter_sub(); + + foreach my $path (glob "${main::userspath}/datev-export-*") { + next unless (-d $path); + + my $mtime = (stat($path))[9]; + next if ((time() - $mtime) < 8 * 60 * 60); + + rmtree $path; + } + + $main::lxdebug->leave_sub(); +} sub _fill { $main::lxdebug->enter_sub(); @@ -109,17 +169,17 @@ sub kne_export { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - my @rc; + my $result; if ($form->{exporttype} == 0) { - @rc = &kne_buchungsexport($myconfig, $form); + $result = kne_buchungsexport($myconfig, $form); } else { - @rc = &kne_stammdatenexport($myconfig, $form); + $result = kne_stammdatenexport($myconfig, $form); } $main::lxdebug->leave_sub(); - return @rc; + return $result; } sub obe_export { @@ -560,7 +620,7 @@ sub kne_buchungsexport { my @filenames; - my $export_path = $main::userspath . "/"; + my $export_path = _get_export_path() . "/"; my $filename = "ED00000"; my $evfile = "EV01"; my @ed_versionsets; @@ -724,7 +784,7 @@ sub kne_buchungsexport { ### $main::lxdebug->leave_sub(); - return @filenames; + return { 'download_token' => get_download_token_for_path($export_path), 'filenames' => \@filenames }; } sub kne_stammdatenexport { @@ -741,7 +801,7 @@ sub kne_stammdatenexport { my @filenames; - my $export_path = $main::userspath . "/"; + my $export_path = _get_export_path() . "/"; my $filename = "ED00000"; my $evfile = "EV01"; my @ed_versionsets; @@ -839,7 +899,7 @@ sub kne_stammdatenexport { $main::lxdebug->leave_sub(); - return @filenames; + return { 'download_token' => get_download_token_for_path($export_path), 'filenames' => \@filenames }; } 1; diff --git a/bin/mozilla/datev.pl b/bin/mozilla/datev.pl index 6d89a21b9..5f8c61e43 100644 --- a/bin/mozilla/datev.pl +++ b/bin/mozilla/datev.pl @@ -331,28 +331,33 @@ sub export3 { $auth->assert('datev_export'); + DATEV::clean_temporary_directories(); + DATEV->save_datev_stamm(\%myconfig, \%$form); - my $link = "datev.pl?action=download"; + my $link = "datev.pl?action=download&download_token="; if ($form->{kne}) { - my @filenames = DATEV->kne_export(\%myconfig, \%$form); - if (@filenames) { - print(qq|
| . $locale->text('KNE-Export erfolgreich!') . qq|
|); - $link .= "&filenames=" . $form->escape(join(":", @filenames)); - print(qq|
Download|); + my $result = DATEV->kne_export(\%myconfig, \%$form); + if ($result && @{ $result->{filenames} }) { + $link .= Q($result->{download_token}); + + print(qq|
| . $locale->text('KNE-Export erfolgreich!') . qq|

Download|); + } else { $form->error("KNE-Export schlug fehl."); } } else { - my @filenames = DATEV->obe_export(\%myconfig, \%$form); - if (@filenames) { - print(qq|
| . $locale->text('OBE-Export erfolgreich!') . qq|
|); - $link .= "&filenames=" . $form->escape(join(":", @filenames)); - print(qq|
Download|); - } else { - $form->error("OBE-Export schlug fehl."); - } + # OBE-Export nicht implementiert. + + # my @filenames = DATEV->obe_export(\%myconfig, \%$form); + # if (@filenames) { + # print(qq|
| . $locale->text('OBE-Export erfolgreich!') . qq|
|); + # $link .= "&filenames=" . $form->escape(join(":", @filenames)); + # print(qq|
Download|); + # } else { + # $form->error("OBE-Export schlug fehl."); + # } } print(""); @@ -366,21 +371,29 @@ sub download { $auth->assert('datev_export'); my $tmp_name = Common->tmpname(); - my $zip_name = strftime("lx-office-datev-export-%Y%m%d.zip", - localtime(time())); + my $zip_name = strftime("lx-office-datev-export-%Y%m%d.zip", localtime(time())); my $cwd = getcwd(); - chdir("users") || die("chdir users"); - my @filenames = split(/:/, $form->{"filenames"}); - map({ s|.*/||; $form->error("Eine der KNE-Exportdateien wurde nicht " . - "gefunden. Wurde der Export bereits " . - "durchgeführt?") unless (-f $_); } - @filenames); + my $path = DATEV::get_path_for_download_token($form->{download_token}); + if (!$path) { + $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant.")); + } + + chdir($path) || die("chdir $path"); + + my @filenames = glob "*"; + + if (!@filenames) { + chdir($cwd); + DATEV::clean_temporary_directories(); + $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant.")); + } my $zip = Archive::Zip->new(); - map({ $zip->addFile($_); } @filenames); + map { $zip->addFile($_); } @filenames; $zip->writeToFileNamed($tmp_name); + chdir($cwd); open(IN, $tmp_name) || die("open $tmp_name"); @@ -393,5 +406,7 @@ sub download { unlink($tmp_name); + DATEV::clean_temporary_directories(); + $lxdebug->leave_sub(); } -- 2.20.1