This commit is contained in:
2024-10-14 00:08:40 +02:00
parent dbfba56f66
commit 1462d52e13
4572 changed files with 2658864 additions and 0 deletions

View File

@@ -0,0 +1,244 @@
# --
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::VirtualFS::DB;
use strict;
use warnings;
use MIME::Base64;
our @ObjectDependencies = (
'Kernel::System::DB',
'Kernel::System::Encode',
'Kernel::System::Log',
);
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
# config (not used right now)
$Self->{Compress} = 0;
$Self->{Crypt} = 0;
return $Self;
}
sub Read {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(BackendKey Mode)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!"
);
return;
}
}
my $Attributes = $Self->_BackendKeyParse(%Param);
my $Encode = 1;
if ( lc $Param{Mode} eq 'binary' ) {
$Encode = 0;
}
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
return if !$DBObject->Prepare(
SQL => 'SELECT content FROM virtual_fs_db WHERE id = ?',
Bind => [ \$Attributes->{FileID} ],
Encode => [$Encode],
);
# get encode object
my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
my $Content;
while ( my @Row = $DBObject->FetchrowArray() ) {
# decode attachment if it's e. g. a postgresql backend!!!
if ( !$DBObject->GetDatabaseFunction('DirectBlob') ) {
$Content = decode_base64( $Row[0] );
if ($Encode) {
$EncodeObject->EncodeInput( \$Content );
}
}
else {
$Content = $Row[0];
}
}
return if !$Content;
# uncompress (in case)
if ( $Attributes->{Compress} ) {
# $Content = ...
}
# decrypt (in case)
if ( $Attributes->{Crypt} ) {
# $Content = ...
}
return \$Content;
}
sub Write {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(Content Filename Mode)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!"
);
return;
}
}
# check if already exists
my $Exists = $Self->_FileLookup( $Param{Filename} );
return if $Exists;
# compress (in case)
if ( $Self->{Compress} ) {
# $Param{Content} = ...
}
# crypt (in case)
if ( $Self->{Crypt} ) {
# $Param{Content} = ...
}
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# encode attachment if it's a postgresql backend!!!
if ( !$DBObject->GetDatabaseFunction('DirectBlob') ) {
$Kernel::OM->Get('Kernel::System::Encode')->EncodeOutput( $Param{Content} );
my $Content = encode_base64( ${ $Param{Content} } );
$Param{Content} = \$Content;
}
my $Encode = 1;
if ( lc $Param{Mode} eq 'binary' ) {
$Encode = 0;
}
return if !$DBObject->Do(
SQL => 'INSERT INTO virtual_fs_db (filename, content, create_time) '
. 'VALUES ( ?, ?, current_timestamp )',
Bind => [ \$Param{Filename}, $Param{Content} ],
);
my $FileID = $Self->_FileLookup( $Param{Filename} );
return if !$FileID;
my $BackendKey = $Self->_BackendKeyGenerate(
FileID => $FileID,
Compress => $Self->{Compress},
Crypt => $Self->{Crypt},
Mode => $Param{Mode},
);
return $BackendKey;
}
sub Delete {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(BackendKey)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!"
);
return;
}
}
my $Attributes = $Self->_BackendKeyParse(%Param);
return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => 'DELETE FROM virtual_fs_db WHERE id = ?',
Bind => [ \$Attributes->{FileID} ],
);
return 1;
}
sub _FileLookup {
my ( $Self, $Filename ) = @_;
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# lookup
return if !$DBObject->Prepare(
SQL => 'SELECT id FROM virtual_fs_db WHERE filename = ?',
Bind => [ \$Filename ],
);
my $FileID;
while ( my @Row = $DBObject->FetchrowArray() ) {
$FileID = $Row[0];
}
return $FileID;
}
sub _BackendKeyGenerate {
my ( $Self, %Param ) = @_;
my $BackendKey = '';
for my $Key ( sort keys %Param ) {
$BackendKey .= "$Key=$Param{$Key};";
}
return $BackendKey;
}
sub _BackendKeyParse {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(BackendKey)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!"
);
return;
}
}
my %Attributes;
my @Pairs = split /;/, $Param{BackendKey};
for my $Pair (@Pairs) {
my ( $Key, $Value ) = split /=/, $Pair;
$Attributes{$Key} = $Value;
}
return \%Attributes;
}
1;

View File

@@ -0,0 +1,239 @@
# --
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::VirtualFS::FS;
use strict;
use warnings;
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Log',
'Kernel::System::Main',
);
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
# get data dir
$Self->{DataDir} = $Kernel::OM->Get('Kernel::Config')->Get('Home') . '/var/virtualfs';
$Self->{Permission} = '660';
# create data dir
if ( !-d $Self->{DataDir} ) {
mkdir $Self->{DataDir} || die $!;
}
# check write permissions
if ( !-w $Self->{DataDir} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message => "Can't write $Self->{DataDir}! try: \$OTRS_HOME/bin/otrs.SetPermissions.pl!",
);
die "Can't write $Self->{DataDir}! try: \$OTRS_HOME/bin/otrs.SetPermissions.pl!";
}
# config (not used right now)
$Self->{Compress} = 0;
$Self->{Crypt} = 0;
return $Self;
}
sub Read {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(BackendKey Mode)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!",
);
return;
}
}
my $Attributes = $Self->_BackendKeyParse(%Param);
my $Content = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Directory => $Self->{DataDir} . $Attributes->{DataDir},
Filename => $Attributes->{Filename},
Mode => $Param{Mode},
);
# uncompress (in case)
if ( $Attributes->{Compress} ) {
# $Content = ...
}
# decrypt (in case)
if ( $Attributes->{Crypt} ) {
# $Content = ...
}
return if !$Content;
return $Content;
}
sub Write {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(Content Filename Mode)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!",
);
return;
}
}
# compress (in case)
if ( $Self->{Compress} ) {
# $Param{Content} = ...
}
# crypt (in case)
if ( $Self->{Crypt} ) {
# $Param{Content} = ...
}
# get main object
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
my $MD5 = $MainObject->FilenameCleanUp(
Filename => $Param{Filename},
Type => 'MD5',
);
my $DataDir = '';
my @Dirs = $Self->_SplitDir( Filename => $MD5 );
DIRECTORY:
for my $Dir (@Dirs) {
$DataDir .= '/' . $Dir;
next DIRECTORY if -e $Self->{DataDir} . $DataDir;
next DIRECTORY if mkdir $Self->{DataDir} . $DataDir;
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Can't create $Self->{DataDir}$DataDir: $!",
);
return;
}
# write article to fs
my $Filename = $MainObject->FileWrite(
Directory => $Self->{DataDir} . $DataDir,
Filename => $MD5,
Mode => $Param{Mode},
Content => $Param{Content},
Permission => $Self->{Permission},
);
return if !$Filename;
my $BackendKey = $Self->_BackendKeyGenerate(
Filename => $Filename,
DataDir => $DataDir,
Compress => $Self->{Compress},
Crypt => $Self->{Crypt},
Mode => $Param{Mode},
);
return $BackendKey;
}
sub Delete {
my ( $Self, %Param ) = @_;
# check needed stuff
if ( !$Param{BackendKey} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!",
);
return;
}
my $Attributes = $Self->_BackendKeyParse(%Param);
return $Kernel::OM->Get('Kernel::System::Main')->FileDelete(
Directory => $Self->{DataDir} . $Attributes->{DataDir},
Filename => $Attributes->{Filename},
);
}
sub _BackendKeyGenerate {
my ( $Self, %Param ) = @_;
my $BackendKey = '';
for my $Key ( sort keys %Param ) {
$BackendKey .= "$Key=$Param{$Key};";
}
return $BackendKey;
}
sub _BackendKeyParse {
my ( $Self, %Param ) = @_;
# check needed stuff
if ( !$Param{BackendKey} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!",
);
return;
}
my @Pairs = split /;/, $Param{BackendKey};
my %Attributes;
for my $Pair (@Pairs) {
my ( $Key, $Value ) = split /=/, $Pair;
$Attributes{$Key} = $Value;
}
return \%Attributes;
}
sub _SplitDir {
my ( $Self, %Param ) = @_;
# check needed stuff
if ( !$Param{Filename} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!",
);
return;
}
my @Dir;
$Dir[0] = substr $Param{Filename}, 0, 2;
$Dir[1] = substr $Param{Filename}, 2, 2;
return ( $Dir[0], $Dir[1] );
}
1;