5 * Web access for files temporarily stored by UploadStash.
7 * For example -- files that were uploaded with the UploadWizard extension are stored temporarily
8 * before committing them to the db. But we want to see their thumbnails and get other information
11 * Since this is based on the user's session, in effect this creates a private temporary file area.
12 * However, the URLs for the files cannot be shared.
15 * @ingroup SpecialPage
19 class SpecialUploadStash
extends SpecialPage
{
21 static $HttpErrors = array( // FIXME: Use OutputPage::getStatusMessage() --RK
23 403 => 'Access Denied',
24 404 => 'File not found',
25 500 => 'Internal Server Error',
31 // we should not be reading in really big files and serving them out
32 private $maxServeFileSize = 262144; // 256K
34 // $request is the request (usually wgRequest)
35 // $subpage is everything in the URL after Special:UploadStash
36 // FIXME: These parameters don't match SpecialPage::__construct()'s params at all, and are unused --RK
37 public function __construct( $request = null, $subpage = null ) {
38 parent
::__construct( 'UploadStash', 'upload' );
39 $this->stash
= new UploadStash();
43 * If file available in stash, cats it out to the client as a simple HTTP response.
44 * n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
46 * @param {String} $subPage: subpage, e.g. in http://example.com/wiki/Special:UploadStash/foo.jpg, the "foo.jpg" part
47 * @return {Boolean} success
49 public function execute( $subPage ) {
50 global $wgOut, $wgUser;
52 if ( !$this->userCanExecute( $wgUser ) ) {
53 $this->displayRestrictionError();
57 // prevent callers from doing standard HTML output -- we'll take it from here
61 $file = $this->getStashFile( $subPage );
62 if ( $file->getSize() > $this->maxServeFileSize
) {
63 throw new MWException( 'file size too large' );
65 $this->outputFile( $file );
68 } catch( UploadStashFileNotFoundException
$e ) {
70 } catch( UploadStashBadPathException
$e ) {
72 } catch( Exception
$e ) {
76 wfHttpError( $code, self
::$HttpErrors[$code], $e->getCode(), $e->getMessage() );
82 * Convert the incoming url portion (subpage of Special page) into a stashed file, if available.
83 * @param {String} $subPage
84 * @return {File} file object
85 * @throws MWException, UploadStashFileNotFoundException, UploadStashBadPathException
87 private function getStashFile( $subPage ) {
88 // due to an implementation quirk (and trying to be compatible with older method)
89 // the stash key doesn't have an extension
91 $n = strrpos( $subPage, '.' );
93 $key = $n ?
substr( $subPage, 0, $n ) : $subPage;
97 $file = $this->stash
->getFile( $key );
98 } catch ( UploadStashFileNotFoundException
$e ) {
99 // if we couldn't find it, and it looks like a thumbnail,
100 // and it looks like we have the original, go ahead and generate it
102 if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
103 // that doesn't look like a thumbnail. re-raise exception
107 list( $dummy, $width, $origKey ) = $matches;
109 // do not trap exceptions, if key is in bad format, or file not found,
110 // let exceptions propagate to caller.
111 $origFile = $this->stash
->getFile( $origKey );
113 // ok we're here so the original must exist. Generate the thumbnail.
114 // because the file is a UploadStashFile, this thumbnail will also be stashed,
115 // and a thumbnailFile will be created in the thumbnailImage composite object
116 $thumbnailImage = $origFile->transform( array( 'width' => $width ) );
117 if ( !$thumbnailImage ) {
118 throw new MWException( 'Could not obtain thumbnail' );
120 $file = $thumbnailImage->thumbnailFile
;
127 * Output HTTP response for file
128 * Side effects, obviously, of echoing lots of stuff to stdout.
131 private function outputFile( $file ) {
132 header( 'Content-Type: ' . $file->getMimeType(), true );
133 header( 'Content-Transfer-Encoding: binary', true );
134 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
135 header( 'Pragma: public', true );
136 header( 'Content-Length: ' . $file->getSize(), true ); // FIXME: PHP can handle Content-Length for you just fine --RK
137 readfile( $file->getPath() );