Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload cache fixes #439

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions Kernel/Modules/AgentTicketPhoneCommon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -431,42 +431,6 @@ sub Run {
);
}

# If is an action about attachments
my $IsUpload = 0;

# attachment delete
my @AttachmentIDs = map {
my ($ID) = $_ =~ m{ \A AttachmentDelete (\d+) \z }xms;
$ID ? $ID : ();
} $ParamObject->GetParamNames();

COUNT:
for my $Count ( reverse sort @AttachmentIDs ) {
my $Delete = $ParamObject->GetParam( Param => "AttachmentDelete$Count" );
next COUNT if !$Delete;
$Error{AttachmentDelete} = 1;
$UploadCacheObject->FormIDRemoveFile(
FormID => $Self->{FormID},
FileID => $Count,
);
$IsUpload = 1;
}

# attachment upload
if ( $ParamObject->GetParam( Param => 'AttachmentUpload' ) ) {
$IsUpload = 1;
%Error = ();
$Error{AttachmentUpload} = 1;
my %UploadStuff = $ParamObject->GetUploadAll(
Param => 'FileUpload',
);
$UploadCacheObject->FormIDAddFile(
FormID => $Self->{FormID},
Disposition => 'attachment',
%UploadStuff,
);
}

# Get and validate draft action.
my $FormDraftAction = $ParamObject->GetParam( Param => 'FormDraftAction' );
if ( $FormDraftAction && !$Config->{FormDraft} ) {
Expand Down Expand Up @@ -525,7 +489,6 @@ sub Run {
ErrorMessage => $Kernel::OM->Get('Kernel::Language')
->Translate( "FormDraft name %s is already in use!", $Title ),
);
$IsUpload = 1;
last DRAFT;
}
}
Expand Down
15 changes: 8 additions & 7 deletions Kernel/Modules/AjaxAttachment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ sub Run {
elsif ( $Self->{Subaction} eq 'Delete' ) {

my $Return;
my $AttachmentFileID = $ParamObject->GetParam( Param => 'FileID' ) || '';
my $AttachmentFilename = $ParamObject->GetParam( Param => 'Filename' ) || '';

if ( !$AttachmentFileID ) {
if ( !$AttachmentFilename ) {
$Return->{Message} = $LayoutObject->{LanguageObject}->Translate(
'Error: the file could not be deleted properly. Please contact your administrator (missing FileID).'
'Error: the file could not be deleted properly. Please contact your administrator (missing Filename).'
);
}
else {

my $DeleteAttachment = $UploadCacheObject->FormIDRemoveFile(
FormID => $Self->{FormID},
FileID => $AttachmentFileID,
FormID => $Self->{FormID},
Filename => $AttachmentFilename,
);

if ($DeleteAttachment) {
Expand All @@ -127,9 +127,10 @@ sub Run {
}

$Return = {
Message => 'Success',
Data => \@AttachmentData,
Message => 'Success',
NumberOfAttachmentsLeft => scalar @AttachmentData,
};

}
}

Expand Down
3 changes: 0 additions & 3 deletions Kernel/System/FormDraft.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Returns (without GetContent or GetContent = 1):
'ContentID' => undef, # optional
'Filename' => 'thankyou.txt',
'Filesize' => 25,
'FileID' => 1,
'Disposition' => 'attachment',
},
...
Expand Down Expand Up @@ -261,7 +260,6 @@ add a new draft
'ContentID' => undef, # optional
'Filename' => 'thankyou.txt',
'Filesize' => 25,
'FileID' => 1,
'Disposition' => 'attachment',
},
...
Expand Down Expand Up @@ -348,7 +346,6 @@ update an existing draft
'ContentID' => undef, # optional
'Filename' => 'thankyou.txt',
'Filesize' => 25,
'FileID' => 1,
'Disposition' => 'attachment',
},
...
Expand Down
6 changes: 3 additions & 3 deletions Kernel/System/Web/UploadCache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ removes a file from a form id

$UploadCacheObject->FormIDRemoveFile(
FormID => 12345,
FileID => 1,
FileName => 'document.txt',
);

=cut
Expand All @@ -136,7 +136,7 @@ returns an array with a hash ref of all files for a Form ID
FormID => 12345,
);

Return data of on hash is Content, ContentType, ContentID, Filename, Filesize, FileID;
Return data of on hash is Content, ContentType, ContentID, Filename, Filesize;

=cut

Expand All @@ -156,7 +156,7 @@ Note: returns no content, only meta data.
FormID => 12345,
);

Return data of hash is ContentType, ContentID, Filename, Filesize, FileID;
Return data of hash is ContentType, ContentID, Filename, Filesize;

=cut

Expand Down
33 changes: 17 additions & 16 deletions Kernel/System/Web/UploadCache/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ sub FormIDAddFile {
sub FormIDRemoveFile {
my ( $Self, %Param ) = @_;

for my $Needed (qw(FormID FileID)) {
for my $Needed (qw(FormID Filename)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Expand All @@ -138,24 +138,30 @@ sub FormIDRemoveFile {
# finish if files have been already removed by other process
return if !@Index;

my $ID = $Param{FileID} - 1;
$Param{Filename} = $Index[$ID]->{Filename};
# Find and remove file with given filename; return success if
# file not found (to avoid error if user double clicks delete icon).
FILE:
for my $File (@Index) {
if ( $File->{Filename} eq $Param{Filename} ) {

return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => '
DELETE FROM web_upload_cache
WHERE form_id = ?
AND filename = ?',
Bind => [ \$Param{FormID}, \$Param{Filename} ],
);
return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => '
DELETE FROM web_upload_cache
WHERE form_id = ?
AND filename = ?',
Bind => [ \$Param{FormID}, \$Param{Filename} ],
);

last FILE;
}
}

return 1;
}

sub FormIDGetAllFilesData {
my ( $Self, %Param ) = @_;

my $Counter = 0;
my @Data;
for my $Needed (qw(FormID)) {
if ( !$Param{$Needed} ) {
Expand All @@ -181,7 +187,6 @@ sub FormIDGetAllFilesData {
);

while ( my @Row = $DBObject->FetchrowArray() ) {
$Counter++;

# encode attachment if it's a postgresql backend!!!
if ( !$DBObject->GetDatabaseFunction('DirectBlob') ) {
Expand All @@ -198,7 +203,6 @@ sub FormIDGetAllFilesData {
Filename => $Row[0],
Filesize => $Row[2],
Disposition => $Row[5],
FileID => $Counter,
}
);
}
Expand All @@ -209,7 +213,6 @@ sub FormIDGetAllFilesData {
sub FormIDGetAllFilesMeta {
my ( $Self, %Param ) = @_;

my $Counter = 0;
my @Data;
for my $Needed (qw(FormID)) {
if ( !$Param{$Needed} ) {
Expand All @@ -234,7 +237,6 @@ sub FormIDGetAllFilesMeta {
);

while ( my @Row = $DBObject->FetchrowArray() ) {
$Counter++;

# add the info
push(
Expand All @@ -245,7 +247,6 @@ sub FormIDGetAllFilesMeta {
Filename => $Row[0],
Filesize => $Row[2],
Disposition => $Row[4],
FileID => $Counter,
}
);
}
Expand Down
72 changes: 35 additions & 37 deletions Kernel/System/Web/UploadCache/FS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ sub FormIDAddFile {
sub FormIDRemoveFile {
my ( $Self, %Param ) = @_;

for my $Needed (qw(FormID FileID)) {
for my $Needed (qw(FormID Filename)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Expand All @@ -193,38 +193,44 @@ sub FormIDRemoveFile {
# finish if files have been already removed by other process
return if !@Index;

my $ID = $Param{FileID} - 1;
my %File = %{ $Index[$ID] };
# Find and remove file with given filename; return success if
# file not found (to avoid error if user double clicks delete icon).
FILE:
for my $File (@Index) {
if ( $File->{Filename} eq $Param{Filename} ) {
my $Directory = $Self->{TempDir} . '/' . $Param{FormID};

my $Directory = $Self->{TempDir} . '/' . $Param{FormID};
if ( !-d $Directory ) {
return 1;
}

if ( !-d $Directory ) {
return 1;
}
# Get main object.
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

# get main object
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
$MainObject->FileDelete(
Directory => $Directory,
Filename => $File->{Filename},
NoFilenameClean => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => $File->{Filename} . '.ContentType',
NoFilenameClean => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => $File->{Filename} . '.ContentID',
NoFilenameClean => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => $File->{Filename} . '.Disposition',
NoFilenameClean => 1,
);

$MainObject->FileDelete(
Directory => $Directory,
Filename => "$File{Filename}",
NoReplace => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => "$File{Filename}.ContentType",
NoReplace => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => "$File{Filename}.ContentID",
NoReplace => 1,
);
$MainObject->FileDelete(
Directory => $Directory,
Filename => "$File{Filename}.Disposition",
NoReplace => 1,
);
last FILE;
}
}

return 1;
}
Expand Down Expand Up @@ -258,8 +264,6 @@ sub FormIDGetAllFilesData {
Filter => "*",
);

my $Counter = 0;

FILEPATH:
for my $FilePath (@List) {

Expand All @@ -268,7 +272,6 @@ sub FormIDGetAllFilesData {
next FILEPATH if $FilePath =~ /\.ContentID$/;
next FILEPATH if $FilePath =~ /\.Disposition$/;

$Counter++;
my $FileSize = -s $FilePath;

my $Filename = basename($FilePath);
Expand Down Expand Up @@ -318,7 +321,6 @@ sub FormIDGetAllFilesData {
ContentType => ${$ContentType},
Filename => $Filename,
Filesize => $FileSize,
FileID => $Counter,
Disposition => ${$Disposition},
},
);
Expand Down Expand Up @@ -356,8 +358,6 @@ sub FormIDGetAllFilesMeta {
Filter => "*",
);

my $Counter = 0;

FILEPATH:
for my $FilePath (@List) {

Expand All @@ -366,7 +366,6 @@ sub FormIDGetAllFilesMeta {
next FILEPATH if $FilePath =~ /\.ContentID$/;
next FILEPATH if $FilePath =~ /\.Disposition$/;

$Counter++;
my $FileSize = -s $FilePath;

my $Filename = basename($FilePath);
Expand Down Expand Up @@ -410,7 +409,6 @@ sub FormIDGetAllFilesMeta {
ContentType => ${$ContentType},
Filename => $Filename,
Filesize => $FileSize,
FileID => $Counter,
Disposition => ${$Disposition},
},
);
Expand Down
12 changes: 6 additions & 6 deletions scripts/test/WebUploadCache.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ for my $Module (qw(DB FS)) {

if ( $Module eq 'FS' ) {
my $Delete = $UploadCacheObject->FormIDRemoveFile(
FormID => $InvalidFormID,
FileID => 1,
FormID => $InvalidFormID,
Filename => $ExpectedFilename,
);

$Self->False(
Expand All @@ -170,8 +170,8 @@ for my $Module (qw(DB FS)) {
}

my $Delete = $UploadCacheObject->FormIDRemoveFile(
FormID => $FormID,
FileID => 1,
FormID => $FormID,
Filename => $ExpectedFilename,
);
$Self->True(
$Delete || '',
Expand Down Expand Up @@ -276,8 +276,8 @@ for my $Module (qw(DB FS)) {
);
}
my $Delete = $UploadCacheObject->FormIDRemoveFile(
FormID => $FormID,
FileID => 1,
FormID => $FormID,
Filename => $ExpectedFilename,
);
$Self->True(
$Delete || '',
Expand Down
Loading