1# -- 2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/ 3# -- 4# This software comes with ABSOLUTELY NO WARRANTY. For details, see 5# the enclosed file COPYING for license information (GPL). If you 6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt. 7# -- 8 9package Kernel::Modules::AjaxAttachment; 10 11use strict; 12use warnings; 13 14use Kernel::System::VariableCheck qw(:all); 15use Kernel::Language qw(Translatable); 16 17our $ObjectManagerDisabled = 1; 18 19sub new { 20 my ( $Type, %Param ) = @_; 21 22 # allocate new hash for object 23 my $Self = {%Param}; 24 bless( $Self, $Type ); 25 26 return $Self; 27} 28 29sub Run { 30 my ( $Self, %Param ) = @_; 31 32 # get params 33 my %GetParam; 34 35 # get param object 36 my $ParamObject = $Kernel::OM->Get('Kernel::System::Web::Request'); 37 my $UploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache'); 38 my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout'); 39 40 # get form id 41 $Self->{FormID} = $Kernel::OM->Get('Kernel::System::Web::Request')->GetParam( Param => 'FormID' ); 42 43 if ( !$Self->{FormID} ) { 44 return $LayoutObject->FatalError( 45 Message => Translatable('Got no FormID.'), 46 ); 47 } 48 49 # challenge token check for write action 50 $LayoutObject->ChallengeTokenCheck(); 51 52 if ( $Self->{Subaction} eq 'Upload' ) { 53 54 my %UploadStuff = $ParamObject->GetUploadAll( 55 Param => 'Files', 56 ); 57 58 $UploadCacheObject->FormIDAddFile( 59 FormID => $Self->{FormID}, 60 Disposition => 'attachment', 61 %UploadStuff, 62 ); 63 64 # get all attachments meta data 65 my @Attachments = $UploadCacheObject->FormIDGetAllFilesMeta( 66 FormID => $Self->{FormID}, 67 ); 68 69 my @AttachmentData; 70 71 ATTACHMENT: 72 for my $Attachment (@Attachments) { 73 74 # Hide inline attachments from the display. Please see bug#13498 for more information. 75 next ATTACHMENT if $Attachment->{Disposition} eq 'inline'; 76 77 # Add human readable data size. 78 $Attachment->{HumanReadableDataSize} = $LayoutObject->HumanReadableDataSize( 79 Size => $Attachment->{Filesize}, 80 ); 81 82 push @AttachmentData, $Attachment; 83 } 84 85 return $LayoutObject->Attachment( 86 ContentType => 'application/json; charset=' . $LayoutObject->{Charset}, 87 Content => $Kernel::OM->Get('Kernel::System::JSON')->Encode( 88 Data => \@AttachmentData, 89 ), 90 Type => 'inline', 91 NoCache => 1, 92 ); 93 } 94 elsif ( $Self->{Subaction} eq 'Delete' ) { 95 96 my $Return; 97 my $AttachmentFileID = $ParamObject->GetParam( Param => 'FileID' ) || ''; 98 99 if ( !$AttachmentFileID ) { 100 $Return->{Message} = $LayoutObject->{LanguageObject}->Translate( 101 'Error: the file could not be deleted properly. Please contact your administrator (missing FileID).' 102 ); 103 } 104 else { 105 106 my $DeleteAttachment = $UploadCacheObject->FormIDRemoveFile( 107 FormID => $Self->{FormID}, 108 FileID => $AttachmentFileID, 109 ); 110 111 if ($DeleteAttachment) { 112 113 my @Attachments = $UploadCacheObject->FormIDGetAllFilesMeta( 114 FormID => $Self->{FormID}, 115 ); 116 117 my @AttachmentData; 118 119 ATTACHMENT: 120 for my $Attachment (@Attachments) { 121 122 # Hide inline attachments from the display. Please see bug#13498 for more information. 123 next ATTACHMENT if $Attachment->{Disposition} eq 'inline'; 124 125 push @AttachmentData, $Attachment; 126 } 127 128 $Return = { 129 Message => 'Success', 130 Data => \@AttachmentData, 131 }; 132 } 133 } 134 135 return $LayoutObject->Attachment( 136 ContentType => 'application/json; charset=' . $LayoutObject->{Charset}, 137 Content => $Kernel::OM->Get('Kernel::System::JSON')->Encode( 138 Data => $Return, 139 ), 140 Type => 'inline', 141 NoCache => 1, 142 ); 143 } 144} 145 1461; 147