1 package SL::Webdav::File;
4 use parent qw(Rose::Object);
10 use Rose::Object::MakeMethods::Generic (
11 scalar => [ qw(webdav filename loaded) ],
14 add_objects => { interface => 'push', hash_key => 'objects' },
19 $_[0]->load unless $_[0]->loaded;
20 my $cmp = $_[0]->webdav->version_scheme->cmp;
21 sort { $cmp->($a, $b) } $_[0]->objects;
30 my @objects = $self->webdav->get_all_objects;
31 my $ref = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav);
32 my ($ref_basename, undef, $ref_extension) = $ref->parse_filename;
34 $self->objects(grep { $_->basename eq $ref_basename && $_->extension eq $ref_extension } @objects);
39 my ($self, %params) = @_;
41 croak 'Invalid call. Only data or file can be set' if ($params{data} && $params{file});
43 $self->load unless $self->loaded;
45 my $last = $self->latest_version;
49 my ($basename, undef, $extension) = SL::Webdav::Object->new(filename => $self->filename, webdav => $self->webdav)->parse_filename;
50 my $new_version = $self->webdav->version_scheme->first_version;
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);
55 $self->add_objects($object);
57 if (!$self->webdav->version_scheme->keep_last_version($last)) {
58 $params{new_version} = 1;
61 if ($params{new_version}) {
62 my $new_version = $self->webdav->version_scheme->next_version($last);
63 my $sep = $self->webdav->version_scheme->separator;
64 my $new_filename = $last->basename . $sep . $new_version . "." . $last->extension;
65 $object = SL::Webdav::Object->new(filename => $new_filename, webdav => $self->webdav);
67 $self->add_objects($object);
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 ]}: $!";
78 open my $fh, '>:raw', $object->full_filedescriptor or die "could not open " . $object->filename . ": $!";
80 $fh->print(${ $params{data} });
97 SL::Webdav::File - Webdav file manipulation
101 use SL::Webdav::File;
103 my $webdav_file = SL::Webdav::File->new(
104 webdav => $webdav, # SL::Webdav instance
105 filename => 'technical_drawing_AB28375.pdf',
108 # get existing versioned files
109 my @webdav_objects = $webdav_file->versions;
112 my $data = SL::Helper::CreatePDF->create_pdf(...);
113 my $webdav_object = $webdav_file->store(data => \$data);
115 # use file instead of data
116 my $webdav_object = $webdav_file->store(file => $path_to_file);
119 my $webdav_object = $webdav_file->store(data => \$data, new_version => 1);
123 A file in this context is the collection of all versions of a single file saved
124 into the webdav. This module provides methods to access and manipulate these
133 Will return all L<SL::Webdav::Object>s found in this file, sorted by version
134 according to the version scheme used.
136 =item C<latest_version>
138 Returns only the latest version object.
142 Loads objects from disk.
144 =item C<store PARAMS>
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.
149 If C<file> is a valid filename then it will be copied.
151 C<file> and C<data> are exclusive.
153 If param C<new_version> is set, force a new version, even if the versioning
154 scheme would keep the old one.
160 L<SL::Webdav>, L<SL::Webdav::Object>
168 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>