Revision a32a0746
Von Jan Büren vor mehr als 9 Jahren hinzugefügt
SL/Webdav/File.pm | ||
---|---|---|
4 | 4 |
use parent qw(Rose::Object); |
5 | 5 |
|
6 | 6 |
use File::Spec; |
7 |
use File::Copy (); |
|
8 |
use Carp; |
|
7 | 9 |
|
8 | 10 |
use Rose::Object::MakeMethods::Generic ( |
9 | 11 |
scalar => [ qw(webdav filename loaded) ], |
... | ... | |
36 | 38 |
sub store { |
37 | 39 |
my ($self, %params) = @_; |
38 | 40 |
|
41 |
croak 'Invalid call. Only data or file can be set' if ($params{data} && $params{file}); |
|
42 |
|
|
39 | 43 |
$self->load unless $self->loaded; |
40 | 44 |
|
41 | 45 |
my $last = $self->latest_version; |
42 | 46 |
my $object; |
43 | 47 |
|
44 | 48 |
if (!$last) { |
49 |
my ($basename, undef, $extension) = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav)->parse_filename; |
|
45 | 50 |
my $new_version = $self->webdav->version_scheme->first_version; |
46 |
$object = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav); |
|
51 |
my $sep = $self->webdav->version_scheme->separator; |
|
52 |
my $new_filename = $basename . $sep . $new_version . "." . $extension; |
|
53 |
$object = SL::Webdav::Object->new(filename => $new_filename, webdav => $self->webdav); |
|
47 | 54 |
|
48 | 55 |
$self->add_objects($object); |
49 | 56 |
} else { |
... | ... | |
63 | 70 |
} |
64 | 71 |
} |
65 | 72 |
|
66 |
open my $fh, '>:raw', $object->full_filedescriptor or die "could not open " . $object->filename . ": $!"; |
|
73 |
if ($params{file}) { |
|
74 |
croak 'No valid file' unless -f $params{file}; |
|
75 |
File::Copy::copy($params{file}, $object->full_filedescriptor) or croak "Copy failed from $params{file} to @{[ $object->filename ]}: $!"; |
|
76 |
} else { |
|
77 |
|
|
78 |
open my $fh, '>:raw', $object->full_filedescriptor or die "could not open " . $object->filename . ": $!"; |
|
79 |
|
|
80 |
$fh->print(${ $params{data} }); |
|
67 | 81 |
|
68 |
$fh->print(${ $params{data} }); |
|
82 |
close $fh; |
|
83 |
} |
|
69 | 84 |
|
70 |
close $fh; |
|
71 | 85 |
|
72 | 86 |
return $object; |
73 | 87 |
} |
... | ... | |
98 | 112 |
my $data = SL::Helper::CreatePDF->create_pdf(...); |
99 | 113 |
my $webdav_object = $webdav_file->store(data => \$data); |
100 | 114 |
|
115 |
# use file instead of data |
|
116 |
my $webdav_object = $webdav_file->store(file => $path_to_file); |
|
117 |
|
|
101 | 118 |
# force new version |
102 | 119 |
my $webdav_object = $webdav_file->store(data => \$data, new_version => 1); |
103 | 120 |
|
... | ... | |
126 | 143 |
|
127 | 144 |
=item C<store PARAMS> |
128 | 145 |
|
129 |
Store a new version on disk. C<data> is expected to contain a reference to the |
|
130 |
data to be written in raw encoding. |
|
146 |
Store a new version on disk. If C<data> is present, it is expected to contain a |
|
147 |
reference to the data to be written in raw encoding. |
|
148 |
|
|
149 |
If C<file> is a valid filename then it will be copied. |
|
150 |
|
|
151 |
C<file> and C<data> are exclusive. |
|
131 | 152 |
|
132 | 153 |
If param C<new_version> is set, force a new version, even if the versioning |
133 | 154 |
scheme would keep the old one. |
Auch abrufbar als: Unified diff
Webdav::File.pm->store um Parameter file erweitert
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.