Ignore:
Timestamp:
12/09/08 18:16:48 (3 years ago)
Author:
CrawfordCurrie
Message:

Item253: remove TWikiDrawPlugin hack; analyse, rationalise and document usage of redirectto; Item5926: added encodings that were proposed to make chinese work (they don't break anything AFAICT). Deprecate Foswiki::Func::getRegularExpression (the regex array is published)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/lib/Foswiki/UI/Upload.pm

    r1127 r1221  
    119119CGI parameters, passed in $query: 
    120120 
    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. 
     121Does 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. 
    133139 
    134140=cut 
    135141 
    136142sub 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 
     180sub _upload { 
    137181    my $session = shift; 
    138182 
     
    159203    $filePath    =~ s/\s*$//o; 
    160204 
    161     Foswiki::UI::checkWebExists( $session, $webName, $topic, 'attach files to' ); 
     205    Foswiki::UI::checkWebExists( 
     206        $session, $webName, $topic, 'attach files to' ); 
    162207    Foswiki::UI::checkTopicExists( $session, $webName, $topic, 
    163         'attach files to' ); 
     208                                   'attach files to' ); 
    164209    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 ); 
    166212 
    167213    my $origName = $fileName; 
     
    174220        try { 
    175221            $tmpFilePath = $query->tmpFileName($fh); 
    176         } 
    177         catch Error::Simple with { 
     222        } catch Error::Simple with { 
    178223 
    179224            # Item5130, Item5133 - Illegal file name, bad path, 
     
    185230                topic  => $topic, 
    186231                params => [ ( $filePath || '""' ) ] 
    187             ); 
     232               ); 
    188233        }; 
    189234 
     
    205250                topic  => $topic, 
    206251                params => [ ( $filePath || '""' ) ] 
    207             ); 
     252               ); 
    208253        } 
    209254 
     
    219264                topic  => $topic, 
    220265                params => [ $fileName, $maxSize ] 
    221             ); 
     266               ); 
    222267        } 
    223268    } 
     
    238283                tmpFilename => $tmpFilePath, 
    239284            } 
    240         ); 
    241     } 
    242     catch Error::Simple with { 
     285           ); 
     286    } catch Error::Simple with { 
    243287        throw Foswiki::OopsException( 
    244288            'attention', 
     
    247291            topic  => $topic, 
    248292            params => [ shift->{-text} ] 
    249         ); 
     293           ); 
    250294    }; 
    251295    close($stream) if $stream; 
    252296 
    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 ) { 
    259298        throw Foswiki::OopsException( 
    260299            'attention', 
     
    264303            topic  => $topic, 
    265304            params => [ $origName, $fileName ] 
    266         ); 
    267     } 
    268  
    269  # generate a message useful for those calling this script from the command line 
    270     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"; 
    273312} 
    274313 
Note: See TracChangeset for help on using the changeset viewer.