From 1352aba43845f7baa8f36bdca0af6d325a307371 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 17 Feb 2014 23:11:00 +0100 Subject: [PATCH] Fixed saving post original file name to DB --- src/Controllers/PostController.php | 2 ++ src/Models/Entities/PostEntity.php | 12 ++++-------- src/Views/post-file-render.phtml | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index f7979696..d2b59409 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -536,6 +536,7 @@ class PostController $srcPath = $suppliedFile['tmp_name']; $post->setContentFromPath($srcPath); + $post->origName = $suppliedFile['name']; if (!$isNew) LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]); @@ -547,6 +548,7 @@ class PostController $url = InputHelper::get('url'); $post->setContentFromUrl($url); + $post->origName = $url; if (!$isNew) LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]); diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 55eb51a3..9db2d78f 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -234,7 +234,7 @@ class PostEntity extends AbstractEntity if ($this->type == PostType::Youtube) { $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.jpg'; - $contents = file_get_contents('http://img.youtube.com/vi/' . $this->origName . '/mqdefault.jpg'); + $contents = file_get_contents('http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg'); file_put_contents($tmpPath, $contents); if (file_exists($tmpPath)) $srcImage = imagecreatefromjpeg($tmpPath); @@ -333,7 +333,6 @@ class PostEntity extends AbstractEntity throw new SimpleException('Invalid file type "' . $this->mimeType . '"'); } - $this->origName = basename($srcPath); $duplicatedPost = PostModel::findByHash($this->fileHash, false); if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id)) throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id); @@ -352,19 +351,16 @@ class PostEntity extends AbstractEntity public function setContentFromUrl($srcUrl) { - $this->origName = $srcUrl; - if (!preg_match('/^https?:\/\//', $srcUrl)) throw new SimpleException('Invalid URL "' . $srcUrl . '"'); if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches)) { - $origName = $matches[1]; - $this->origName = $origName; + $youtubeId = $matches[1]; $this->type = PostType::Youtube; $this->mimeType = null; $this->fileSize = null; - $this->fileHash = $origName; + $this->fileHash = $youtubeId; $this->imageWidth = null; $this->imageHeight = null; @@ -372,7 +368,7 @@ class PostEntity extends AbstractEntity if (file_exists($thumbPath)) unlink($thumbPath); - $duplicatedPost = PostModel::findByHash($origName, false); + $duplicatedPost = PostModel::findByHash($youtubeId, false); if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id)) throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id); return; diff --git a/src/Views/post-file-render.phtml b/src/Views/post-file-render.phtml index 150981e0..8350f1a5 100644 --- a/src/Views/post-file-render.phtml +++ b/src/Views/post-file-render.phtml @@ -19,6 +19,6 @@ type == PostType::Youtube): ?> - +