This commit is contained in:
Marcin Kurczewski 2013-10-25 13:18:03 +02:00
parent 4ecb3f3b81
commit 2279e5605b
6 changed files with 141 additions and 66 deletions

View file

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -188,12 +188,27 @@ $(function()
handleInputs(urls, function(postDom, url)
{
postDom.data('url', url);
$('.file-name strong', postDom).text(url);
$('[name=source]', postDom).val(url);
var img = postDom.find('img');
img.css('background-image', 'none');
img.attr('src', url);
postDom.find('[name=source]').val(url);
if (matches = url.match(/watch.*?=([a-zA-Z0-9_-]+)/))
{
postDom.find('.file-name strong').text(url);
$.getJSON('http://gdata.youtube.com/feeds/api/videos/' + matches[1] + '?v=2&alt=jsonc', function(data)
{
postDom.find('.file-name strong')
.text(data.data.title);
postDom.find('img')
.css('background-image', 'none')
.attr('src', data.data.thumbnail.hqDefault);
});
}
else
{
postDom.find('.file-name strong')
.text(url);
postDom.find('img')
.css('background-image', 'none')
.attr('src', url);
}
});
}

View file

@ -217,46 +217,56 @@ class PostController
elseif (InputHelper::get('url'))
{
$url = InputHelper::get('url');
$origName = $url;
if (!preg_match('/^https?:\/\//', $url))
throw new SimpleException('Invalid URL "' . $url . '"');
$origName = $url;
$sourcePath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
//warning: low level sh*t ahead
//download the URL $url into $sourcePath
$maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize'));
set_time_limit(0);
$urlFP = fopen($url, 'rb');
if (!$urlFP)
throw new SimpleException('Cannot open URL for reading');
$sourceFP = fopen($sourcePath, 'w+b');
if (!$sourceFP)
if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $url, $matches))
{
fclose($urlFP);
throw new SimpleException('Cannot open file for writing');
$origName = $matches[1];
$postType = PostType::Youtube;
$sourcePath = null;
}
try
else
{
while (!feof($urlFP))
$sourcePath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
//warning: low level sh*t ahead
//download the URL $url into $sourcePath
$maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize'));
set_time_limit(0);
$urlFP = fopen($url, 'rb');
if (!$urlFP)
throw new SimpleException('Cannot open URL for reading');
$sourceFP = fopen($sourcePath, 'w+b');
if (!$sourceFP)
{
$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) . ')');
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 . '"');
}

View file

@ -3,4 +3,5 @@ class PostType extends Enum
{
const Image = 1;
const Flash = 2;
const Youtube = 3;
}

View file

@ -92,19 +92,21 @@
</span>
</div>
<div class="permalink">
<a href="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>" title="Download">
<i class="icon-dl"></i>
<span class="ext">
<?php $mimes = ['image/jpeg' => 'JPG', 'image/gif' => 'GIF', 'image/png' => 'PNG', 'application/x-shockwave-flash' => 'SWF'] ?>
<?php $mime = $this->context->transport->post->mimeType ?>
<?php echo isset($mimes[$mime]) ? $mimes[$mime] : 'unknown' ?>
</span>
<span class="size">
<?php echo TextHelper::useBytesUnits($this->context->transport->post->file_size) ?>
</span>
</a>
</div>
<?php if ($this->context->transport->post->type != PostType::Youtube): ?>
<div class="permalink">
<a href="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>" title="Download">
<i class="icon-dl"></i>
<span class="ext">
<?php $mimes = ['image/jpeg' => 'JPG', 'image/gif' => 'GIF', 'image/png' => 'PNG', 'application/x-shockwave-flash' => 'SWF'] ?>
<?php $mime = $this->context->transport->post->mimeType ?>
<?php echo isset($mimes[$mime]) ? $mimes[$mime] : 'unknown' ?>
</span>
<span class="size">
<?php echo TextHelper::useBytesUnits($this->context->transport->post->file_size) ?>
</span>
</a>
</div>
<?php endif ?>
</div>
<div class="unit favorites">
@ -211,11 +213,17 @@
<div id="inner-content">
<div class="post-wrapper post-type-<?php echo strtolower(PostType::toString($this->context->transport->post->type)) ?>">
<?php if ($this->context->transport->post->type == PostType::Image): ?>
<img src="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>" alt="<?php echo $this->context->transport->post->name ?>"/>
<?php elseif ($this->context->transport->post->type == PostType::Flash): ?>
<embed width="<?php echo $this->context->transport->post->image_width ?>" height="<?php echo $this->context->transport->post->image_height ?>" type="application/x-shockwave-flash" src="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>"/>
<?php endif ?>
<?php switch ($this->context->transport->post->type):
case PostType::Image: ?>
<img src="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>" alt="<?php echo $this->context->transport->post->name ?>"/>
<?php break ?>
<?php case PostType::Flash: ?>
<embed width="<?php echo $this->context->transport->post->image_width ?>" height="<?php echo $this->context->transport->post->image_height ?>" type="application/x-shockwave-flash" src="<?php echo \Chibi\UrlHelper::route('post', 'retrieve', ['name' => $this->context->transport->post->name]) ?>"/>
<?php break ?>
<?php case PostType::Youtube: ?>
<iframe width="800" height="600" src="//www.youtube.com/embed/<?php echo $this->context->transport->post->orig_name ?>" frameborder="0" allowfullscreen></iframe>
<?php break ?>
<?php endswitch ?>
</div>
<?php if ($canEditAnything): ?>