Changeset 1221 for trunk/core/lib/Foswiki/UI/Upload.pm
- Timestamp:
- 12/09/08 18:16:48 (3 years ago)
- File:
-
- 1 edited
-
trunk/core/lib/Foswiki/UI/Upload.pm (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/core/lib/Foswiki/UI/Upload.pm
r1127 r1221 119 119 CGI parameters, passed in $query: 120 120 121 | =hidefile= | if defined, will not show file in attachment table | 122 | =filepath= | | 123 | =filename= | | 124 | =filecomment= | comment to associate with file in attachment table | 125 | =createlink= | if defined, will create a link to file at end of topic | 126 | =changeproperties= | | 127 | =redirectto= | URL to redirect to after upload. ={AllowRedirectUrl}= must be enabled in =configure=. The parameter value can be a =TopicName=, a =Web.TopicName=, or a URL. Redirect to a URL only works if it is enabled in =configure=. | 128 129 Does the work of uploading a file to a topic. Designed to be useable for 130 a crude RPC (it will redirect to the 'view' script unless the 131 'noredirect' parameter is specified, in which case it will print a message to 132 STDOUT, starting with 'OK' on success and 'ERROR' on failure. 121 Does the work of uploading an attachment to a topic. 122 123 * =hidefile= - if defined, will not show file in attachment table 124 * =filepath= - 125 * =filename= - 126 * =filecomment= - comment to associate with file in attachment table 127 * =createlink= - if defined, will create a link to file at end of topic 128 * =changeproperties= - 129 * =redirectto= - URL to redirect to after upload. ={AllowRedirectUrl}= 130 must be enabled in =configure=. The parameter value can be a 131 =TopicName=, a =Web.TopicName=, or a URL. Redirect to a URL only works 132 if it is enabled in =configure=, and is ignored if =noredirect= is 133 specified. 134 * =noredirect= - Normally it will redirect to 'view' when the upload is 135 complete, but also designed to be useable for REST-style calling using 136 the 'noredirect' parameter. If this parameter is set it will return an 137 appropriate HTTP status code and print a message to STDOUT, starting 138 with 'OK' on success and 'ERROR' on failure. 133 139 134 140 =cut 135 141 136 142 sub upload { 143 my $session = shift; 144 145 my $query = $session->{request}; 146 if ($query->param('noredirect')) { 147 my $message; 148 my $status = 200; 149 try { 150 $message = _upload($session); 151 } catch Foswiki::OopsException with { 152 my $e = shift; 153 $status = $e->{status}; 154 if ($status >= 400) { 155 $message = 'ERROR: '.$e->stringify(); 156 } 157 } catch Foswiki::AccessControlException with { 158 my $e = shift; 159 $status = 403; 160 $message = 'ERROR: '.$e->stringify(); 161 }; 162 if ($status < 400) { 163 $message = 'OK '.$message; 164 }; 165 $session->{response}->header( 166 -status => $status, 167 -type => 'text/plain'); 168 $session->{response}->print($message); 169 } else { 170 # allow exceptions to propagate 171 _upload($session); 172 173 my $nurl = $session->getScriptUrl( 174 1, 'view', $session->{webName}, $session->{topicName} ); 175 $session->redirect( $session->redirectto( $nurl )); 176 }; 177 } 178 179 # Real work of upload 180 sub _upload { 137 181 my $session = shift; 138 182 … … 159 203 $filePath =~ s/\s*$//o; 160 204 161 Foswiki::UI::checkWebExists( $session, $webName, $topic, 'attach files to' ); 205 Foswiki::UI::checkWebExists( 206 $session, $webName, $topic, 'attach files to' ); 162 207 Foswiki::UI::checkTopicExists( $session, $webName, $topic, 163 'attach files to' );208 'attach files to' ); 164 209 Foswiki::UI::checkMirror( $session, $webName, $topic ); 165 Foswiki::UI::checkAccess( $session, $webName, $topic, 'CHANGE', $user ); 210 Foswiki::UI::checkAccess( 211 $session, $webName, $topic, 'CHANGE', $user ); 166 212 167 213 my $origName = $fileName; … … 174 220 try { 175 221 $tmpFilePath = $query->tmpFileName($fh); 176 } 177 catch Error::Simple with { 222 } catch Error::Simple with { 178 223 179 224 # Item5130, Item5133 - Illegal file name, bad path, … … 185 230 topic => $topic, 186 231 params => [ ( $filePath || '""' ) ] 187 );232 ); 188 233 }; 189 234 … … 205 250 topic => $topic, 206 251 params => [ ( $filePath || '""' ) ] 207 );252 ); 208 253 } 209 254 … … 219 264 topic => $topic, 220 265 params => [ $fileName, $maxSize ] 221 );266 ); 222 267 } 223 268 } … … 238 283 tmpFilename => $tmpFilePath, 239 284 } 240 ); 241 } 242 catch Error::Simple with { 285 ); 286 } catch Error::Simple with { 243 287 throw Foswiki::OopsException( 244 288 'attention', … … 247 291 topic => $topic, 248 292 params => [ shift->{-text} ] 249 );293 ); 250 294 }; 251 295 close($stream) if $stream; 252 296 253 if ( $fileName eq $origName ) { 254 $session->redirect( 255 $session->getScriptUrl( 1, 'view', $webName, $topic ), 256 undef, 1 ); 257 } 258 else { 297 if ( $fileName ne $origName ) { 259 298 throw Foswiki::OopsException( 260 299 'attention', … … 264 303 topic => $topic, 265 304 params => [ $origName, $fileName ] 266 );267 } 268 269 # generate a message useful for those calling this script from the command line270 my $message = ($doPropsOnly) ? 'properties changed' : "$fileName uploaded";271 272 print 'OK ', $message, "\n" if $session->inContext('command_line');305 ); 306 } 307 308 # generate a message useful for those calling this script 309 # from the command line 310 return ($doPropsOnly) ? 'properties changed' : 311 "$fileName uploaded"; 273 312 } 274 313
Note: See TracChangeset
for help on using the changeset viewer.
