3 * Implements Special:UploadStash
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 UnlistedSpecialPage
{
23 // Since we are directly writing the file to STDOUT,
24 // we should not be reading in really big files and serving them out.
26 // We also don't want people using this as a file drop, even if they
29 // This service is really for thumbnails and other such previews while
31 const MAX_SERVE_BYTES
= 262144; // 256K
33 public function __construct( ) {
34 parent
::__construct( 'UploadStash', 'upload' );
36 $this->stash
= new UploadStash( );
37 } catch (UploadStashNotAvailableException
$e) {
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 $subPage String: 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 $message = 'Unknown error';
63 if ( !isset( $subPage ) ||
$subPage === '' ) {
64 // the user probably visited the page just to see what would happen, so explain it a bit.
66 $message = "Missing key\n\n"
67 . 'This page provides access to temporarily stashed files for the user that '
68 . 'uploaded those files. See the upload API documentation. To access a stashed file, '
69 . 'use the URL of this page, with a slash and the key of the stashed file appended.';
72 $file = $this->getStashFile( $subPage );
73 $size = $file->getSize();
76 $message = 'File is zero length';
77 } else if ( $size > self
::MAX_SERVE_BYTES
) {
79 $message = 'Cannot serve a file larger than ' . self
::MAX_SERVE_BYTES
. ' bytes';
81 $this->outputFile( $file );
84 } catch( UploadStashFileNotFoundException
$e ) {
86 $message = $e->getMessage();
87 } catch( UploadStashBadPathException
$e ) {
89 $message = $e->getMessage();
90 } catch( Exception
$e ) {
92 $message = $e->getMessage();
96 wfHttpError( $code, OutputPage
::getStatusMessage( $code ), $message );
102 * Convert the incoming url portion (subpage of Special page) into a stashed file,
105 * @param $subPage String
106 * @return File object
107 * @throws MWException, UploadStashFileNotFoundException, UploadStashBadPathException
109 private function getStashFile( $subPage ) {
110 // due to an implementation quirk (and trying to be compatible with older method)
111 // the stash key doesn't have an extension
113 $n = strrpos( $subPage, '.' );
114 if ( $n !== false ) {
115 $key = $n ?
substr( $subPage, 0, $n ) : $subPage;
119 $file = $this->stash
->getFile( $key );
120 } catch ( UploadStashFileNotFoundException
$e ) {
121 // if we couldn't find it, and it looks like a thumbnail,
122 // and it looks like we have the original, go ahead and generate it
124 if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
125 // that doesn't look like a thumbnail. re-raise exception
129 list( $dummy, $width, $origKey ) = $matches;
131 // do not trap exceptions, if key is in bad format, or file not found,
132 // let exceptions propagate to caller.
133 $origFile = $this->stash
->getFile( $origKey );
135 // ok we're here so the original must exist. Generate the thumbnail.
136 // because the file is a UploadStashFile, this thumbnail will also be stashed,
137 // and a thumbnailFile will be created in the thumbnailImage composite object
138 $thumbnailImage = $origFile->transform( array( 'width' => $width ) );
139 if ( !$thumbnailImage ) {
140 throw new MWException( 'Could not obtain thumbnail' );
142 $file = $thumbnailImage->thumbnailFile
;
149 * Output HTTP response for file
150 * Side effects, obviously, of echoing lots of stuff to stdout.
152 * @param $file File object
154 private function outputFile( $file ) {
155 header( 'Content-Type: ' . $file->getMimeType(), true );
156 header( 'Content-Transfer-Encoding: binary', true );
157 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
158 header( 'Content-Length: ' . $file->getSize(), true );
159 readfile( $file->getPath() );