/** * 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; }
private function createTempFile($filepath) { $tempFile = new FakeLocalFile(Title::newFromText(uniqid('Temp_', true), 6), RepoGroup::singleton()->getLocalRepo()); $tempFile->upload($filepath, '', ''); // TODO: Add to the garbage collector return $tempFile; }
/** * @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; }
function uploadImage() { global $wgRequest, $wgUser; $check_result = $this->checkImage(); if (UploadBase::SUCCESS == $check_result) { $tempname = $this->tempFileName($wgUser); $file = new FakeLocalFile(Title::newFromText($tempname, 6), RepoGroup::singleton()->getLocalRepo()); $file->upload($wgRequest->getFileTempName('wpUploadFile'), '', ''); $tempid = $this->tempFileStoreInfo($tempname); $props = array(); $props['file'] = $file; $props['name'] = stripslashes($wgRequest->getFileName('wpUploadFile')); $props['mwname'] = $tempname; $props['tempid'] = $tempid; $props['upload'] = true; $props['default_caption'] = Wikia::getProps($file->getTitle()->getArticleID(), 'default_caption'); return $this->detailsPage($props); } else { return $this->loadMain($this->translateError($check_result)); } }
/** * @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; }