1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| ---++ StaticMethod attach( $session )
# Programme en PERL 5 car certaine chose du genre attach s'ecrive &attach en Perl4.
=upload= command handler.
This method is designed to be
invoked via the =TWiki::UI::run= method.
Adds the meta-data for an attachment to a toic. Does *not* upload
the attachment itself, just modifies the meta-data.
=cut
sub attach { # declaration (appel attach) avec parametres
# L'instruction « my » réalise une affectation dans des variables locales à la
# procédure avec les éléments du tableau. Ce type de passage de paramètre est très efficace car le
# nombre de paramètres n'est pas forcément fixe.
my $session = shift;
my $query = $session->{cgiQuery};
my $webName = $session->{webName};
my $topic = $session->{topicName};
my $fileName = $query->param( 'filename' ) || '';
my $skin = $session->getSkin(); # Le skin courant est remplacé par le skin de la session en cours. ( getSkin accesseur )
TWiki::UI::checkWebExists( $session, $webName, $topic, 'attach' );
my $tmpl = ''; #initialisation des variables a ' '
my $text = ''; #initialisation des variables a ' '
my $meta = ''; #initialisation des variables a ' '
my $atext = ''; #initialisation des variables a ' '
my $fileUser = ''; #initialisation des variables a ' '
my $isHideChecked = ''; #initialisation des variables a ' '
TWiki::UI::checkMirror( $session, $webName, $topic );
TWiki::UI::checkAccess( $session, $webName, $topic,
'change', $session->{user} );
TWiki::UI::checkTopicExists( $session, $webName, $topic,
'upload files to' );
( $meta, $text ) = $session->{store}->readTopic( $session->{user}, $webName, $topic, undef );
my $args = $meta->get( 'FILEATTACHMENT', $fileName );
$args = {
name => $fileName,
attr => '',
path => '',
comment => ''
} unless( $args );
if ( $args->{attr} =~ /h/o ) {
$isHideChecked = 'checked';
}
# SMELL: why log attach before post is called?
# FIXME: Move down, log only if successful (or with error msg?)
# Attach is a read function, only has potential for a change
if( $TWiki::cfg{Log}{attach} ) {
# write log entry
$session->writeLog( 'attach', $webName.'.'.$topic, $fileName );
}
my $fileWikiUser = '';
if( $fileName ) # SI il le fichier comporte un nom. ( Définie par l'utilisateur ) ALORS ...
{
$tmpl = $session->{templates}->readTemplate( 'attachagain', $skin );
my $u = $session->{users}->findUser( $args->{user} );
$fileWikiUser = $u->webDotWikiName() if $u;
} else {
$tmpl = $session->{templates}->readTemplate( 'attachnew', $skin );
}
if ( $fileName ) {
# Must come after templates have been read
$atext .= $session->{attach}->formatVersions( $webName, $topic, %$args )
;
}
$tmpl =~ s/%ATTACHTABLE%/$atext/go;
$tmpl =~ s/%FILEUSER%/$fileWikiUser/go;
$session->enterContext( 'can_render_meta', $meta );
$tmpl = $session->handleCommonTags( $tmpl, $webName, $topic );
$tmpl = $session->{renderer}->getRenderedVersion( $tmpl, $webName, $topic );
$tmpl =~ s/%HIDEFILE%/$isHideChecked/go;
$tmpl =~ s/%FILENAME%/$fileName/go;
$tmpl =~ s/%FILEPATH%/$args->{path}/go;
$args->{comment} = TWiki::entityEncode( $args->{comment} );
$tmpl =~ s/%FILECOMMENT%/$args->{comment}/go;
$session->writeCompletePage( $tmpl );
}
=pod
---++ StaticMethod upload( $session )
=upload= command handler.
This method is designed to be
invoked via the =TWiki::UI::run= method.
CGI parameters, passed in $query:
| =hidefile= | if efined, will not show file in attachment table |
| =filepath= | |
| =filename= | |
| =filecomment= | Comment to associate with file in attachment table |
| =createlink= | if defined, will create a link to file at end of topic |
| =changeproperties= | |
Does the work of uploading a file to a topic. Designed to be useable as
a REST method (it will redirect to the 'view' script unless the 'noredirect'
parameter is specified, in which case it will print a message to
STDOUT, starting with 'OK' on success and 'ERROR' on failure.
=cut
sub upload {
my $session = shift;
my $query = $session->{cgiQuery};
my $webName = $session->{webName};
my $topic = $session->{topicName};
my $user = $session->{user};
my $hideFile = $query->param( 'hidefile' ) || '';
my $fileComment = $query->param( 'filecomment' ) || '';
my $createLink = $query->param( 'createlink' ) || '';
my $doPropsOnly = $query->param( 'changeproperties' );
my $filePath = $query->param( 'filepath' ) || '';
my $fileName = $query->param( 'filename' ) || '';
if ( $filePath && ! $fileName ) {
$filePath =~ m|([^/\\]*$)|;
$fileName = $1;
}
$fileComment =~ s/\s+/ /go;
$fileComment =~ s/^\s*//o;
$fileComment =~ s/\s*$//o;
$fileName =~ s/\s*$//o;
$filePath =~ s/\s*$//o;
TWiki::UI::checkWebExists( $session, $webName, $topic, 'attach files to' );
TWiki::UI::checkTopicExists( $session, $webName, $topic, 'attach files to' )
;
TWiki::UI::checkMirror( $session, $webName, $topic );
TWiki::UI::checkAccess( $session, $webName, $topic,
'change', $user );
my ( $fileSize, $fileDate, $tmpFileName );
my $stream;
# SMELL: Does $stream get closed in all throws?
$stream = $query->upload( 'filepath' ) unless ( $doPropsOnly );
my $origName = $fileName;
unless( $doPropsOnly ) {
( $fileName, $origName ) =
TWiki::Sandbox::sanitizeAttachmentName( $fileName );
# check if upload has non zero size
if( $stream ) {
my @stats = stat $stream;
$fileSize = $stats[7];
$fileDate = $stats[9];
}
unless( $fileSize && $fileName ) {
#OopsExecption ( script d'erreur géré par le TWiki )
throw TWiki::OopsException( 'attention',
def => 'zero_size_upload',
web => $webName,
topic => $topic,
params => ($filePath || '""') );
}
my $maxSize = $session->{prefs}->getPreferencesValue( 'ATTACHFILESIZELIM
IT' );
$maxSize = 0 unless ( $maxSize =~ /([0-9]+)/o );
if( $maxSize && $fileSize > $maxSize * 1024 ) { # SI taille maximum et la taille du fichier sont superieur a la taille max fois 1024 ALORS ....
# OopsExecption ( script d'erreur géré par le TWiki )
throw TWiki::OopsException( 'attention',
def => 'oversized_upload',
web => $webName,
topic => $topic,
params => [ $fileName, $maxSize ] );
}
}
try {
$session->{store}->saveAttachment(
$webName, $topic, $fileName, $user,
{ dontlog => !$TWiki::cfg{Log}{upload},
comment => $fileComment,
hide => $hideFile,
createlink => $createLink,
stream => $stream,
filepath => $filePath,
filesize => $fileSize,
filedate => $fileDate,
} );
} catch Error::Simple with {
#OopsExecption ( script d'erreur géré par le TWiki )
throw TWiki::OopsException( 'attention',
def => 'save_error',
web => $webName,
topic => $topic,
params => shift->{-text} );
};
close( $stream ) if $stream;
if( $fileName eq $origName ) { # SI le nom du fichier est equivalent a sont nom d'origine ALORS ....
$session->redirect( $session->getScriptUrl( 1, 'view', $webName, $topic
));
} else {
#OopsExecption ( script d'erreur géré par le TWiki )
throw TWiki::OopsException( 'attention',
def => 'upload_name_changed',
web => $webName,
topic => $topic,
params => [ $origName, $fileName ] );
}
# generate a message useful for those calling this script from the command l
ine
my $message = ( $doPropsOnly ) ?
'properties changed' : "$fileName uploaded";
print 'OK ',$message,"\n";
}
1; |
Partager