1 package SL::File::Backend::Filesystem;
 
   5 use parent qw(SL::File::Backend);
 
   9 use File::Path qw(make_path);
 
  16   my ($self, %params) = @_;
 
  17   die "no dbfile in backend delete" unless $params{dbfile};
 
  18   my $backend_data = $params{dbfile}->backend_data;
 
  19   $backend_data    = 0                               if $params{last};
 
  20   $backend_data    = $params{dbfile}->backend_data-1 if $params{all_but_notlast};
 
  22   if ($backend_data > 0 ) {
 
  23     for my $version ( 1..$backend_data) {
 
  24       my $file_path = $self->_filesystem_path($params{dbfile},$version);
 
  27     if ($params{all_but_notlast}) {
 
  28       my $from = $self->_filesystem_path($params{dbfile},$params{dbfile}->backend_data);
 
  29       my $to   = $self->_filesystem_path($params{dbfile},1);
 
  30       die "file not exists in backend delete" unless -f $from;
 
  32       $params{dbfile}->backend_data(1);
 
  34       $params{dbfile}->backend_data(0);
 
  35       my $dir_path = $self->_filesystem_path($params{dbfile});
 
  39     my $file_path = $self->_filesystem_path($params{dbfile},$params{dbfile}->backend_data);
 
  40     die "file not exists in backend delete" unless -f $file_path;
 
  42     $params{dbfile}->backend_data($params{dbfile}->backend_data-1);
 
  51   my ($self, %params) = @_;
 
  52   die 'dbfile not exists' unless $params{dbfile};
 
  53   my $dbfile = $params{dbfile};
 
  54   die 'no file contents' unless $params{file_path} || $params{file_contents};
 
  55   $dbfile->backend_data(0) unless $dbfile->backend_data;
 
  56   $dbfile->backend_data($dbfile->backend_data*1+1);
 
  59   my $tofile = $self->_filesystem_path($dbfile);
 
  60   if ($params{file_path} && -f $params{file_path}) {
 
  61     File::Copy::copy($params{file_path}, $tofile);
 
  63   elsif ($params{file_contents}) {
 
  64     open(OUT, "> " . $tofile);
 
  65     print OUT $params{file_contents};
 
  71 sub get_version_count {
 
  72   my ($self, %params) = @_;
 
  73   die "no dbfile" unless $params{dbfile};
 
  74   return $params{dbfile}->backend_data * 1;
 
  78   my ($self, %params) = @_;
 
  79   die "no dbfile" unless $params{dbfile};
 
  80   die "unknown version" if $params{version} &&
 
  81                           ($params{version} < 0 || $params{version} > $params{dbfile}->backend_data) ;
 
  82   my $path = $self->_filesystem_path($params{dbfile},$params{version});
 
  83   die "no file found in backend get_mtime" if !-f $path;
 
  85   my $dt = DateTime->from_epoch(epoch => $st[9])->clone();
 
  90   my ($self, %params) = @_;
 
  91   die "no dbfile" unless $params{dbfile};
 
  92   my $path = $self->_filesystem_path($params{dbfile},$params{version});
 
  93   die "no file in backend get_filepath" if !-f $path;
 
  98   my ($self, %params) = @_;
 
  99   my $path = $self->get_filepath(%params);
 
 100   return "" unless $path;
 
 101   my $contents = File::Slurp::read_file($path);
 
 106   return 0 unless $::instance_conf->get_doc_files;
 
 107   return 0 unless $::lx_office_conf{paths}->{document_path};
 
 108   return 0 unless -d $::lx_office_conf{paths}->{document_path};
 
 117 sub _filesystem_path {
 
 118   my ($self, $dbfile, $version) = @_;
 
 120   die "No files backend enabled" unless $::instance_conf->get_doc_files || $::lx_office_conf{paths}->{document_path};
 
 122   # use filesystem with depth 3
 
 123   $version    = $dbfile->backend_data if !$version || $version < 1 || $version > $dbfile->backend_data;
 
 124   my $iddir   = sprintf("%04d", $dbfile->id % 1000);
 
 125   my $path    = File::Spec->catdir($::lx_office_conf{paths}->{document_path}, $::auth->client->{id}, $iddir, $dbfile->id);
 
 127     File::Path::make_path($path, { chmod => 0770 });
 
 129   return $path if !$version;
 
 130   return File::Spec->catdir($path, $dbfile->id . '_' . $version);
 
 143 SL::File::Backend::Filesystem  - Filesystem class for file storage backend
 
 147 See the synopsis of L<SL::File::Backend>.
 
 151 This specific storage backend use a Filesystem which is only accessed by this interface.
 
 152 This is the big difference to the Webdav backend where the files can be accessed without the control of that backend.
 
 153 This backend use the database id of the SL::DB::File object as filename. The filesystem has up to 1000 subdirectories
 
 154 to store the files not to flat in the filesystem. In this Subdirectories for each file an additional subdirectory exists
 
 155 for the versions of this file.
 
 157 The Versioning is done via a Versionnumber which is incremented by one for each version.
 
 158 So the Version 2 of the file with the database id 4 is stored as path {root}/0004/4/4_2.
 
 163 See methods of L<SL::File::Backend>.
 
 171 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>