Revision 10095677
Von Tamino Steinert vor 8 Monaten hinzugefügt
- ID 100956770fecfaba2bfd040d9f3ddf3e904c8e60
- Vorgänger 11f62590
SL/File/Backend/Filesystem.pm | ||
---|---|---|
6 | 6 |
use SL::DB::File; |
7 | 7 |
use SL::DB::FileVersion; |
8 | 8 |
|
9 |
use Carp; |
|
10 |
use List::Util qw(first); |
|
11 |
|
|
9 | 12 |
use File::Copy; |
10 | 13 |
use File::Slurp; |
11 | 14 |
use File::stat; |
... | ... | |
19 | 22 |
sub delete { |
20 | 23 |
my ($self, %params) = @_; |
21 | 24 |
die "no dbfile in backend delete" unless $params{dbfile}; |
22 |
my $last_version = $params{dbfile}->backend_data; |
|
23 |
my $first_version = 1; |
|
24 |
$last_version = 0 if $params{last}; |
|
25 |
$last_version = $params{dbfile}->backend_data-1 if $params{all_but_notlast}; |
|
26 |
$last_version = $params{version} if $params{version}; |
|
27 |
$first_version = $params{version} if $params{version}; |
|
28 |
|
|
29 |
if ($last_version > 0 ) { |
|
30 |
for my $version ( $first_version..$last_version) { |
|
31 |
my $file_path = $self->_filesystem_path($params{dbfile},$version); |
|
32 |
unlink($file_path); |
|
33 |
} |
|
34 |
if ($params{version}) { |
|
35 |
for my $version ( $last_version+1 .. $params{dbfile}->backend_data) { |
|
36 |
my $from = $self->_filesystem_path($params{dbfile},$version); |
|
37 |
my $to = $self->_filesystem_path($params{dbfile},$version - 1); |
|
38 |
die "file not exists in backend delete" unless -f $from; |
|
39 |
rename($from,$to); |
|
40 |
} |
|
41 |
$params{dbfile}->backend_data($params{dbfile}->backend_data-1); |
|
42 |
} |
|
43 |
elsif ($params{all_but_notlast}) { |
|
44 |
my $from = $self->_filesystem_path($params{dbfile},$params{dbfile}->backend_data); |
|
45 |
my $to = $self->_filesystem_path($params{dbfile},1); |
|
46 |
die "file not exists in backend delete" unless -f $from; |
|
47 |
rename($from,$to); |
|
48 |
$params{dbfile}->backend_data(1); |
|
49 |
} else { |
|
50 |
$params{dbfile}->backend_data(0); |
|
51 |
} |
|
52 |
unless ($params{dbfile}->backend_data) { |
|
53 |
my $dir_path = $self->_filesystem_path($params{dbfile}); |
|
54 |
rmdir($dir_path); |
|
55 |
} |
|
25 |
my @versions = @{$params{dbfile}->file_versions_sorted}; |
|
26 |
|
|
27 |
my @versions_to_delete; |
|
28 |
if ($params{last}) { |
|
29 |
my $last = pop @versions; |
|
30 |
@versions_to_delete = ($last); |
|
31 |
} elsif ($params{all_but_notlast}) { |
|
32 |
pop @versions; # remove last |
|
33 |
@versions_to_delete = @versions; |
|
34 |
} elsif ($params{version}) { |
|
35 |
my $version = first {$_->version == $params{version}} @versions |
|
36 |
or confess "Version not found."; |
|
37 |
@versions_to_delete = ($version); |
|
56 | 38 |
} else { |
57 |
my $file_path = $self->_filesystem_path($params{dbfile},$params{dbfile}->backend_data); |
|
58 |
die "file not exists in backend delete" unless -f $file_path; |
|
59 |
unlink($file_path); |
|
60 |
$params{dbfile}->backend_data($params{dbfile}->backend_data-1); |
|
39 |
@versions_to_delete = @versions; |
|
61 | 40 |
} |
62 |
return 1; |
|
63 |
} |
|
64 | 41 |
|
65 |
sub rename { |
|
42 |
foreach my $version (@versions_to_delete) { |
|
43 |
unlink($version->get_system_location()); |
|
44 |
$version->delete; |
|
45 |
} |
|
46 |
|
|
47 |
return 1; |
|
66 | 48 |
} |
67 | 49 |
|
68 | 50 |
sub save { |
... | ... | |
81 | 63 |
return 1 if $last_file_size == $new_file_size; |
82 | 64 |
} |
83 | 65 |
|
84 |
$dbfile->backend_data(0) unless $dbfile->backend_data; |
|
85 |
$dbfile->backend_data($dbfile->backend_data*1+1); |
|
86 |
$dbfile->save->load; |
|
66 |
my @versions = @{$dbfile->file_versions_sorted}; |
|
67 |
my $new_version_number = scalar @versions ? $versions[-1]->version + 1 : 1; |
|
87 | 68 |
|
88 |
my $tofile = $self->_filesystem_path($dbfile); |
|
69 |
my $tofile = $self->_filesystem_path($dbfile, $new_version_number);
|
|
89 | 70 |
if ($params{file_path} && -f $params{file_path}) { |
90 | 71 |
File::Copy::copy($params{file_path}, $tofile); |
91 | 72 |
} elsif ($params{file_contents}) { |
... | ... | |
100 | 81 |
$rel_file =~ s/$doc_path//; |
101 | 82 |
my $fv = SL::DB::FileVersion->new( |
102 | 83 |
file_id => $dbfile->id, |
103 |
version => $dbfile->backend_data,
|
|
84 |
version => $new_version_number,
|
|
104 | 85 |
file_location => $rel_file, |
105 | 86 |
doc_path => $doc_path, |
106 | 87 |
backend => 'Filesystem', |
... | ... | |
116 | 97 |
sub get_version_count { |
117 | 98 |
my ($self, %params) = @_; |
118 | 99 |
die "no dbfile" unless $params{dbfile}; |
119 |
return $params{dbfile}->backend_data//0 * 1; |
|
100 |
my $file_id = $params{dbfile}->id; |
|
101 |
return 0 unless defined $file_id; |
|
102 |
return SL::DB::Manager::FileVersion->get_all_count(where => [file_id => $file_id]); |
|
120 | 103 |
} |
121 | 104 |
|
122 | 105 |
sub get_mtime { |
123 | 106 |
my ($self, %params) = @_; |
124 |
die "no dbfile" unless $params{dbfile}; |
|
125 |
die "unknown version" if $params{version} && |
|
126 |
($params{version} < 0 || $params{version} > $params{dbfile}->backend_data); |
|
127 |
my $path = $self->_filesystem_path($params{dbfile}, $params{version}); |
|
128 |
|
|
129 |
die "No file found at $path. Expected: $params{dbfile}{file_name}, file.id: $params{dbfile}{id}" if !-f $path; |
|
107 |
my $path = $self->get_filepath(%params); |
|
130 | 108 |
|
131 | 109 |
my $dt = DateTime->from_epoch(epoch => stat($path)->mtime, time_zone => $::locale->get_local_time_zone()->name, locale => $::lx_office_conf{system}->{language})->clone(); |
132 | 110 |
return $dt; |
... | ... | |
158 | 136 |
} |
159 | 137 |
|
160 | 138 |
sub sync_from_backend { |
161 |
my ($self, %params) = @_; |
|
162 |
my @query = (file_type => $params{file_type}); |
|
163 |
push @query, (file_name => $params{file_name}) if $params{file_name}; |
|
164 |
push @query, (mime_type => $params{mime_type}) if $params{mime_type}; |
|
165 |
push @query, (source => $params{source}) if $params{source}; |
|
166 |
|
|
167 |
my $sortby = $params{sort_by} || 'itime DESC,file_name ASC'; |
|
168 |
|
|
169 |
my @files = @{ SL::DB::Manager::File->get_all(query => [@query], sort_by => $sortby) }; |
|
170 |
for (@files) { |
|
171 |
$main::lxdebug->message(LXDebug->DEBUG2(), "file id=" . $_->id." version=".$_->backend_data); |
|
172 |
my $newversion = $_->backend_data; |
|
173 |
for my $version ( reverse 1 .. $_->backend_data ) { |
|
174 |
my $path = $self->_filesystem_path($_, $version); |
|
175 |
$main::lxdebug->message(LXDebug->DEBUG2(), "path=".$path." exists=".( -f $path?1:0)); |
|
176 |
last if -f $path; |
|
177 |
$newversion = $version - 1; |
|
178 |
} |
|
179 |
$main::lxdebug->message(LXDebug->DEBUG2(), "newversion=".$newversion." version=".$_->backend_data); |
|
180 |
if ( $newversion < $_->backend_data ) { |
|
181 |
$_->backend_data($newversion); |
|
182 |
$_->save if $newversion > 0; |
|
183 |
$_->delete if $newversion <= 0; |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
139 |
die "Not implemented"; |
|
187 | 140 |
} |
188 | 141 |
|
189 | 142 |
# |
... | ... | |
196 | 149 |
die "No files backend enabled" unless $::instance_conf->get_doc_files || $::lx_office_conf{paths}->{document_path}; |
197 | 150 |
|
198 | 151 |
# use filesystem with depth 3 |
199 |
$version = $dbfile->backend_data if !$version || $version < 1 || $version > $dbfile->backend_data; |
|
152 |
$version ||= $dbfile->file_versions_sorted->[-1]->version; |
|
153 |
confess "Version is required." unless $version; |
|
200 | 154 |
my $iddir = sprintf("%04d", $dbfile->id % 1000); |
201 | 155 |
my $path = File::Spec->catdir($::lx_office_conf{paths}->{document_path}, $::auth->client->{id}, $iddir, $dbfile->id); |
202 | 156 |
if (!-d $path) { |
203 | 157 |
File::Path::make_path($path, { chmod => 0770 }); |
204 | 158 |
} |
205 |
return $path if !$version; |
|
206 | 159 |
return File::Spec->catdir($path, $dbfile->id . '_' . $version); |
207 | 160 |
} |
208 | 161 |
|
Auch abrufbar als: Unified diff
SL::File: Versionsnummber über file_version bestimmen