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
60 if ( !isset( $subPage ) ||
$subPage === '' ) {
61 // the user probably visited the page just to see what would happen, so explain it a bit.
63 $message = "Missing key\n\n"
64 . 'This page provides access to temporarily stashed files for the user that '
65 . 'uploaded those files. See the upload API documentation. To access a stashed file, '
66 . 'use the URL of this page, with a slash and the key of the stashed file appended.';
69 $file = $this->getStashFile( $subPage );
70 $size = $file->getSize();
73 $message = 'File is zero length';
74 } else if ( $size > self
::MAX_SERVE_BYTES
) {
76 $message = 'Cannot serve a file larger than ' . self
::MAX_SERVE_BYTES
. ' bytes';
78 $this->outputFile( $file );
81 } catch( UploadStashFileNotFoundException
$e ) {
83 $message = $e->getMessage();
84 } catch( UploadStashBadPathException
$e ) {
86 $message = $e->getMessage();
87 } catch( Exception
$e ) {
89 $message = $e->getMessage();
93 wfHttpError( $code, OutputPage
::getStatusMessage( $code ), $message );
99 * Convert the incoming url portion (subpage of Special page) into a stashed file,
102 * @param $subPage String
103 * @return File object
104 * @throws MWException, UploadStashFileNotFoundException, UploadStashBadPathException
106 private function getStashFile( $subPage ) {
107 // due to an implementation quirk (and trying to be compatible with older method)
108 // the stash key doesn't have an extension
110 $n = strrpos( $subPage, '.' );
111 if ( $n !== false ) {
112 $key = $n ?
substr( $subPage, 0, $n ) : $subPage;
116 $file = $this->stash
->getFile( $key );
117 } catch ( UploadStashFileNotFoundException
$e ) {
118 // if we couldn't find it, and it looks like a thumbnail,
119 // and it looks like we have the original, go ahead and generate it
121 if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
122 // that doesn't look like a thumbnail. re-raise exception
126 list( , $width, $origKey ) = $matches;
128 // do not trap exceptions, if key is in bad format, or file not found,
129 // let exceptions propagate to caller.
130 $origFile = $this->stash
->getFile( $origKey );
132 // ok we're here so the original must exist. Generate the thumbnail.
133 // because the file is a UploadStashFile, this thumbnail will also be stashed,
134 // and a thumbnailFile will be created in the thumbnailImage composite object
135 $thumbnailImage = $origFile->transform( array( 'width' => $width ) );
136 if ( !$thumbnailImage ) {
137 throw new MWException( 'Could not obtain thumbnail' );
139 $file = $thumbnailImage->thumbnailFile
;
146 * Output HTTP response for file
147 * Side effects, obviously, of echoing lots of stuff to stdout.
149 * @param $file File object
151 private function outputFile( $file ) {
152 header( 'Content-Type: ' . $file->getMimeType(), true );
153 header( 'Content-Transfer-Encoding: binary', true );
154 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
155 header( 'Content-Length: ' . $file->getSize(), true );
156 readfile( $file->getPath() );