Fixed saving post original file name to DB

This commit is contained in:
Marcin Kurczewski 2014-02-17 23:11:00 +01:00
parent eee6421775
commit 1352aba438
3 changed files with 7 additions and 9 deletions

View file

@ -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)]);

View file

@ -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;

View file

@ -19,6 +19,6 @@
<?php elseif ($post->type == PostType::Youtube): ?>
<iframe style="width: 800px; height: 600px; border: 0;" src="//www.youtube.com/embed/<?php echo $post->origName ?>" allowfullscreen></iframe>
<iframe style="width: 800px; height: 600px; border: 0;" src="//www.youtube.com/embed/<?php echo $post->fileHash ?>" allowfullscreen></iframe>
<?php endif ?>