From 2279e5605b82c6fa95a800159a84b6142fafd17a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 25 Oct 2013 13:18:03 +0200 Subject: [PATCH] Closed #37 --- public_html/media/css/post-small.css | 18 +++- public_html/media/img/thumb-overlay-yt.png | Bin 0 -> 1597 bytes public_html/media/js/upload.js | 27 +++-- src/Controllers/PostController.php | 117 ++++++++++++++------- src/Models/PostType.php | 1 + src/Views/post-view.phtml | 44 ++++---- 6 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 public_html/media/img/thumb-overlay-yt.png diff --git a/public_html/media/css/post-small.css b/public_html/media/css/post-small.css index abfa714a..d588ae25 100644 --- a/public_html/media/css/post-small.css +++ b/public_html/media/css/post-small.css @@ -5,9 +5,7 @@ position: relative; display: inline-block; } -.post-type-flash { - border-color: red; -} +.post-type-youtube:after, .post-type-flash:after { position: absolute; right: 0; @@ -15,9 +13,21 @@ width: 150px; height: 150px; content: ' '; - background: url('../img/thumb-overlay-swf.png'); pointer-events: none; } +.post-type-flash { + border-color: red; +} +.post-type-youtube { + border-color: red; +} +.post-type-flash:after { + background: url('../img/thumb-overlay-swf.png'); +} +.post-type-youtube:after { + background: url('../img/thumb-overlay-yt.png'); +} + .post:focus, .post:hover { diff --git a/public_html/media/img/thumb-overlay-yt.png b/public_html/media/img/thumb-overlay-yt.png new file mode 100644 index 0000000000000000000000000000000000000000..c29d0d0049e3ca75a7973c41a3bc758aefa1029a GIT binary patch literal 1597 zcmc(fdop31vir;w8v|bTyNs&h(uL%*YmE<>~)OxJJ z)?`Rc%w$YB5n@yxdFTDgBk!3p?63WA|Jc9xo^$W#-0!{j-22zfaC5Pjl2DWY06+?I zup=PM`|96`Bk@vBWeEb&ARA{J0C<`$xqC(o$%P^b_O<}6OL-niemLtua0Y;QbpS}d z0RTKiN}dCNXng=!2mk={djOyi@sR0mffU4lb+orbh#2?S%OnH`A{@LU5jy()xwNt*kunktevJ=)})_-5%U?M_1A2mV{x7Hl~`ocU@4D zd351OQ}J)b%6{LtclZ7UVuT^kUKNi%vXD~zrpdOn_vmYvEuNnPyDO1BU|jp`*~iJt zm!)STy0-Z! zbQM~+EElCEme5>uJ*31OY#Pc)^Mp z6z$5W$R&u&NFBzMaHpfO2`^aa8D5b8J4Yur%w|b8f)Yqk zgSwPV!+(Hu{gUQ+pf~HxW;!@9M6cC?wk%mI^B^OlBkfd+dDw>m=O9J0Z=k(Qc4GuHZsOJj=JMP5_%c2G=Go`G^0ZrHewkFy@+y2JIOg7_FX ziXKDPXv;KV#S-C5D+8lsaOg$YkPn}goqw%f8}dQb}Y+=-b?@>EN&3C#s= z9ZmSm0=bpzwOvn}HX`|)B?$Zc!UcEOqJDPV5~?A!uA$GO_tTaYm}CmGj#;;5YW-Z$ zod>e~HxhF|o!S?}7%%;bEESrP$4E5>N?h1~l=S51rG%g{9JDqcS~QvmQhA=68j!uc zed}0S^=v_52uxwuvQ41dq?oCfAh%SIy#;aww6PUD+)>E*e8|zZxMgQN{?E;fKG3Xw zezhF*PMYA!k*99U$2@U>i7S?yqZ!(2RsOMpCUD?NOP(Q2Vb(CQ&{owj8w***@yWI1 zsg7xXT~|1mNY{i0#Rs(yT=$v088T29m~et+?VGM4`RCwxD zj)*-xHQkWUhbb35KhVIqg(nqVcsLr>k9LB^iu86Ypmy#wsZluCX>5ixJ>1TK*5**z ziFd$W$H)0#TUFH_FBskkTO7#)ol~7tOB?nRzNbc)Q_h_y{W+^P{q0cy3Yj?Gd7krzf3S0J1IiJJz8!LDPzYdRqZ2WJ}_p z8wx3MG%YK@wl~*Qx7FGPRr>etft@;?Iw@j3jXllusOI-l5SAUD=ntPX*OIF%^{Y;v zG?sd>{zsrh$=W*h6{T~RGk8ng>lMWa%vZ^zFCEGpV%#uh$^yU z-70}l*tC5AYnRaX{d4>} $maxBytes) - throw new SimpleException('File is too big (maximum allowed size: ' . TextHelper::useBytesUnits($maxBytes) . ')'); + fclose($urlFP); + throw new SimpleException('Cannot open file for writing'); + } + try + { + while (!feof($urlFP)) + { + $buffer = fread($urlFP, 4 * 1024); + if (fwrite($sourceFP, $buffer) === false) + throw new SimpleException('Cannot write into file'); + fflush($sourceFP); + if (ftell($sourceFP) > $maxBytes) + throw new SimpleException('File is too big (maximum allowed size: ' . TextHelper::useBytesUnits($maxBytes) . ')'); + } + } + finally + { + fclose($urlFP); + fclose($sourceFP); } - } - finally - { - fclose($urlFP); - fclose($sourceFP); } } /* file details */ - $mimeType = mime_content_type($sourcePath); + $mimeType = $sourcePath ? mime_content_type($sourcePath) : null; $imageWidth = null; $imageHeight = null; switch ($mimeType) @@ -272,13 +282,29 @@ class PostController list ($imageWidth, $imageHeight) = getimagesize($sourcePath); break; default: - throw new SimpleException('Invalid file type "' . $mimeType . '"'); + if (!isset($postType)) + throw new SimpleException('Invalid file type "' . $mimeType . '"'); } - $fileHash = md5_file($sourcePath); - $duplicatedPost = R::findOne('post', 'file_hash = ?', [$fileHash]); - if ($duplicatedPost !== null) - throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id); + if ($sourcePath) + { + $fileSize = filesize($sourcePath); + $fileHash = md5_file($sourcePath); + $duplicatedPost = R::findOne('post', 'file_hash = ?', [$fileHash]); + if ($duplicatedPost !== null) + throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id); + } + else + { + $fileSize = 0; + $fileHash = null; + if ($postType == PostType::Youtube) + { + $duplicatedPost = R::findOne('post', 'orig_name = ?', [$origName]); + if ($duplicatedPost !== null) + throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id); + } + } do { @@ -307,7 +333,7 @@ class PostController $dbPost->name = $name; $dbPost->orig_name = $origName; $dbPost->file_hash = $fileHash; - $dbPost->file_size = filesize($sourcePath); + $dbPost->file_size = $fileSize; $dbPost->mime_type = $mimeType; $dbPost->safety = $suppliedSafety; $dbPost->source = $suppliedSource; @@ -319,10 +345,13 @@ class PostController $dbPost->ownFavoritee = []; $dbPost->sharedTag = $dbTags; - if (is_uploaded_file($sourcePath)) - move_uploaded_file($sourcePath, $path); - else - rename($sourcePath, $path); + if ($sourcePath) + { + if (is_uploaded_file($sourcePath)) + move_uploaded_file($sourcePath, $path); + else + rename($sourcePath, $path); + } R::store($dbPost); $this->context->transport->success = true; @@ -623,7 +652,15 @@ class PostController $dstWidth = $this->config->browsing->thumbWidth; $dstHeight = $this->config->browsing->thumbHeight; - switch ($post->mime_type) + if ($post->type == PostType::Youtube) + { + $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png'; + $contents = file_get_contents('http://img.youtube.com/vi/' . $post->orig_name . '/mqdefault.jpg'); + file_put_contents($tmpPath, $contents); + if (file_exists($tmpPath)) + $srcImage = imagecreatefromjpeg($tmpPath); + } + else switch ($post->mime_type) { case 'image/jpeg': $srcImage = imagecreatefromjpeg($srcPath); @@ -784,6 +821,10 @@ class PostController case 'img': $type = PostType::Image; break; + case 'yt': + case 'youtube': + $type = PostType::Youtube; + break; default: throw new SimpleException('Unknown type "' . $val . '"'); } diff --git a/src/Models/PostType.php b/src/Models/PostType.php index 98a7f9c5..2a0a3407 100644 --- a/src/Models/PostType.php +++ b/src/Models/PostType.php @@ -3,4 +3,5 @@ class PostType extends Enum { const Image = 1; const Flash = 2; + const Youtube = 3; } diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index c39e04cb..c05f8e30 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -92,19 +92,21 @@ - + context->transport->post->type != PostType::Youtube): ?> + +
@@ -211,11 +213,17 @@
- context->transport->post->type == PostType::Image): ?> - <?php echo $this->context->transport->post->name ?> - context->transport->post->type == PostType::Flash): ?> - - + context->transport->post->type): + case PostType::Image: ?> + <?php echo $this->context->transport->post->name ?> + + + + + + + +