/** * @param \Mirasvit\Blog\Model\Post $post * @return $this * @throws \Exception */ protected function saveImage($post) { if (!isset($_FILES['featured_image']) || !$_FILES['featured_image']['name']) { return $this; } $image = $_FILES['featured_image']; $ext = pathinfo($image['name'], PATHINFO_EXTENSION); $name = pathinfo($image['name'], PATHINFO_FILENAME); $oldFileName = $post->getFeaturedImage(); $newFileName = $name . '-' . $post->getId() . '.' . $ext; $allowedFileExtensions = ['png', 'jpeg', 'jpg', 'gif']; $ext = pathinfo($image['name'], PATHINFO_EXTENSION); if (!in_array($ext, $allowedFileExtensions)) { throw new \Exception(__('File type not allowed (only JPG, JPEG, PNG & GIF files are allowed)')); } $uploader = new FileUploader($_FILES['featured_image']); $uploader->setAllowedExtensions($allowedFileExtensions)->setAllowRenameFiles(false)->setFilesDispersion(false)->setAllowCreateFolders(true)->setAllowRenameFiles(false)->setFilesDispersion(false); $uploader->save($this->config->getMediaPath(), $newFileName); $post->setFeaturedImage($newFileName); if ($newFileName != $oldFileName) { $this->deleteImage($oldFileName); } return $this; }
/** * @param Post $post * @return string */ public function getPostUrl($post) { return $this->getUrl('/' . $post->getUrlKey(), 'post'); }