From a32a0746c2ae41dfca264883e3da9864a95915d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20B=C3=BCren?= Date: Wed, 30 Sep 2015 19:03:59 +0200 Subject: [PATCH] Webdav::File.pm->store um Parameter file erweitert MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Falls man ein pdf nicht im Program als Objekt hat, sondern eine wirkliche Datei im Dateisystem vorliegen hat, muss diese nicht erst als Objekt geladen werden, sondern kann simpel mit: $webdav_file->store(file => $path_to_file); beim Anlegen eines neuen Webdav-Files (übergeben) kopiert werden. --- SL/Webdav/File.pm | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/SL/Webdav/File.pm b/SL/Webdav/File.pm index 8ad78838c..313c9ff31 100644 --- a/SL/Webdav/File.pm +++ b/SL/Webdav/File.pm @@ -4,6 +4,8 @@ use strict; use parent qw(Rose::Object); use File::Spec; +use File::Copy (); +use Carp; use Rose::Object::MakeMethods::Generic ( scalar => [ qw(webdav filename loaded) ], @@ -36,14 +38,19 @@ sub load { sub store { my ($self, %params) = @_; + croak 'Invalid call. Only data or file can be set' if ($params{data} && $params{file}); + $self->load unless $self->loaded; my $last = $self->latest_version; my $object; if (!$last) { + my ($basename, undef, $extension) = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav)->parse_filename; my $new_version = $self->webdav->version_scheme->first_version; - $object = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav); + my $sep = $self->webdav->version_scheme->separator; + my $new_filename = $basename . $sep . $new_version . "." . $extension; + $object = SL::Webdav::Object->new(filename => $new_filename, webdav => $self->webdav); $self->add_objects($object); } else { @@ -63,11 +70,18 @@ sub store { } } - open my $fh, '>:raw', $object->full_filedescriptor or die "could not open " . $object->filename . ": $!"; + if ($params{file}) { + croak 'No valid file' unless -f $params{file}; + File::Copy::copy($params{file}, $object->full_filedescriptor) or croak "Copy failed from $params{file} to @{[ $object->filename ]}: $!"; + } else { + + open my $fh, '>:raw', $object->full_filedescriptor or die "could not open " . $object->filename . ": $!"; + + $fh->print(${ $params{data} }); - $fh->print(${ $params{data} }); + close $fh; + } - close $fh; return $object; } @@ -98,6 +112,9 @@ SL::Webdav::File - Webdav file manipulation my $data = SL::Helper::CreatePDF->create_pdf(...); my $webdav_object = $webdav_file->store(data => \$data); + # use file instead of data + my $webdav_object = $webdav_file->store(file => $path_to_file); + # force new version my $webdav_object = $webdav_file->store(data => \$data, new_version => 1); @@ -126,8 +143,12 @@ Loads objects from disk. =item C -Store a new version on disk. C is expected to contain a reference to the -data to be written in raw encoding. +Store a new version on disk. If C is present, it is expected to contain a +reference to the data to be written in raw encoding. + +If C is a valid filename then it will be copied. + +C and C are exclusive. If param C is set, force a new version, even if the versioning scheme would keep the old one. -- 2.20.1