/** * Handle image upload * * Returns array with uploaded files details or error details */ public function uploadImage($uploadFieldName = self::DEFAULT_FILE_FIELD_NAME, $destFileName = null, $forceOverwrite = false) { global $IP, $wgRequest, $wgUser; wfProfileIn(__METHOD__); $ret = false; // check whether upload is enabled (RT #53714) if (!WikiaPhotoGalleryHelper::isUploadAllowed()) { $ret = array('error' => true, 'message' => wfMsg('uploaddisabled')); wfProfileOut(__METHOD__); return $ret; } $imageName = stripslashes(!empty($destFileName) ? $destFileName : $wgRequest->getFileName($uploadFieldName)); // validate name and content of uploaded photo $nameValidation = $this->checkImageName($imageName, $uploadFieldName); if ($nameValidation == UploadBase::SUCCESS) { // get path to uploaded image $imagePath = $wgRequest->getFileTempName($uploadFieldName); // check if image with this name is already uploaded if ($this->imageExists($imageName) && !$forceOverwrite) { // upload as temporary file $this->log(__METHOD__, "image '{$imageName}' already exists!"); $tempName = $this->tempFileName($wgUser); $title = Title::makeTitle(NS_FILE, $tempName); $localRepo = RepoGroup::singleton()->getLocalRepo(); $file = new FakeLocalFile($title, $localRepo); $file->upload($wgRequest->getFileTempName($uploadFieldName), '', ''); // store uploaded image in GarbageCollector (image will be removed if not used) $tempId = $this->tempFileStoreInfo($tempName); // generate thumbnail (to fit 200x200 box) of temporary file $width = min(WikiaPhotoGalleryHelper::thumbnailMaxWidth, $file->width); $height = min(WikiaPhotoGalleryHelper::thumbnailMaxHeight, $file->height); $thumbnail = $file->transform(array('height' => $height, 'width' => $width)); // split uploaded file name into name + extension (foo-bar.png => foo-bar + png) list($fileName, $extensionsName) = UploadBase::splitExtensions($imageName); $extensionName = !empty($extensionsName) ? end($extensionsName) : ''; $this->log(__METHOD__, 'upload successful'); $ret = array('conflict' => true, 'name' => $imageName, 'nameParts' => array($fileName, $extensionName), 'tempId' => $tempId, 'size' => array('height' => $file->height, 'width' => $file->width), 'thumbnail' => array('height' => $thumbnail->height, 'url' => $thumbnail->url, 'width' => $thumbnail->width)); } else { // use regular MW upload $this->log(__METHOD__, "image '{$imageName}' is new one - uploading as MW file"); $this->log(__METHOD__, "uploading '{$imagePath}' as File:{$imageName}"); // create title and file objects for MW image to create $imageTitle = Title::newFromText($imageName, NS_FILE); $imageFile = new LocalFile($imageTitle, RepoGroup::singleton()->getLocalRepo()); // perform upload $result = $imageFile->upload($imagePath, '', ''); $this->log(__METHOD__, !empty($result->ok) ? 'upload successful' : 'upload failed'); $ret = array('success' => !empty($result->ok), 'name' => $imageName, 'size' => array('height' => !empty($result->ok) ? $imageFile->getHeight() : 0, 'width' => !empty($result->ok) ? $imageFile->getWidth() : 0)); } } else { $reason = $nameValidation; $this->log(__METHOD__, "upload failed - file name is not valid (error #{$reason})"); $ret = array('error' => true, 'reason' => $reason, 'message' => $this->translateError($reason)); } wfProfileOut(__METHOD__); return $ret; }
<h2><?php echo wfMsg('wmu-existing'); ?> </h2> <div style="margin: 5px 0;"> <input type="button" value="<?php echo wfMsg('wmu-insert'); ?> " onclick="WMU_insertImage(event, 'existing');" /> </div> </td> </tr> <tr id="ImageUploadCompare"> <td style="border-right: 1px solid #CCC;"> <?php echo $file_mwname->transform(array('width' => 265, 'height' => 205))->toHtml(); ?> </td> <td> <input type="hidden" id="ImageUploadExistingName" value="<?php echo $file_name->getName(); ?> " /> <?php echo $file_name->transform(array('width' => 265, 'height' => 205))->toHtml(); ?> </td> </tr> </table> <div style="text-align: center;"><a onclick="WMU_insertImage(event, 'overwrite');" href="#"><?php echo wfMsg('wmu-overwrite');
/** * @param $fileName String * @param $fileuploader WikiaTempFilesUpload * @return bool|int|MediaTransformOutput */ protected function storeInTempImage($fileName, $fileuploader) { wfProfileIn(__METHOD__); if (filesize($fileName) > self::AVATAR_MAX_SIZE) { wfProfileOut(__METHOD__); return UPLOAD_ERR_FORM_SIZE; } $tempName = $fileuploader->tempFileName($this->wg->User); $title = Title::makeTitle(NS_FILE, $tempName); $localRepo = RepoGroup::singleton()->getLocalRepo(); /** * @var $ioh ImageOperationsHelper */ $ioh = new ImageOperationsHelper(); $out = $ioh->postProcessFile($fileName); if ($out !== true) { wfProfileOut(__METHOD__); return $out; } $file = new FakeLocalFile($title, $localRepo); $status = $file->upload($fileName, '', ''); if ($status->ok) { $width = min(self::AVATAR_DEFAULT_SIZE, $file->width); $height = min(self::AVATAR_DEFAULT_SIZE, $file->height); $thumbnail = $file->transform(['height' => $height, 'width' => $width]); } else { $errors = $status->getErrorsArray(); $errMsg = 'Unable to upload temp file fo avatar. Error(s): '; foreach ($errors as $error) { $errMsg .= $error[0] . ', '; } $errMsg = rtrim($errMsg, ', '); wfDebugLog(__METHOD__, $errMsg); $thumbnail = false; } wfProfileOut(__METHOD__); return $thumbnail; }
/** * @param $fileName String * @param $fileuploader WikiaTempFilesUpload * @return bool|int|MediaTransformOutput */ protected function storeInTempImage($fileName, $fileuploader) { $this->app->wf->ProfileIn(__METHOD__); if (filesize($fileName) > self::AVATAR_MAX_SIZE) { return UPLOAD_ERR_FORM_SIZE; } $tempName = $fileuploader->tempFileName($this->wg->User); $title = Title::makeTitle(NS_FILE, $tempName); $localRepo = RepoGroup::singleton()->getLocalRepo(); /** * @var $ioh ImageOperationsHelper */ $ioh = F::build('ImageOperationsHelper'); $out = $ioh->postProcessFile($fileName); if ($out !== true) { return $out; } $file = new FakeLocalFile($title, $localRepo); $file->upload($fileName, '', ''); $width = min(self::AVATAR_DEFAULT_SIZE, $file->width); $height = min(self::AVATAR_DEFAULT_SIZE, $file->height); $thumbnail = $file->transform(array('height' => $height, 'width' => $width)); $this->app->wf->ProfileOut(__METHOD__); return $thumbnail; }