implemented mp4 resizing and added support for custom hashes

This commit is contained in:
Chris
2018-12-22 18:37:53 +01:00
parent 0fe80b29b8
commit 47545e5702
7 changed files with 68 additions and 10 deletions

View File

@@ -35,7 +35,7 @@ PictShare is a selfhostable, open source image, video and text hosting as well a
- [ ] Upload of links to shorten - [ ] Upload of links to shorten
### MP4 hosting ### MP4 hosting
- [ ] Resizing - [x] Resizing
- [x] Preview image generation - [x] Preview image generation
- [x] Upload of videos - [x] Upload of videos
- [x] Automatic conversion if not mobile friendly or wrong encoder used - [x] Automatic conversion if not mobile friendly or wrong encoder used

View File

@@ -15,11 +15,14 @@ require_once(ROOT . DS . 'controllers' . DS. 'text'. DS . 'text.controller.php')
require_once(ROOT . DS . 'controllers' . DS. 'url'. DS . 'url.controller.php'); require_once(ROOT . DS . 'controllers' . DS. 'url'. DS . 'url.controller.php');
require_once(ROOT . DS . 'controllers' . DS. 'video'. DS . 'video.controller.php'); require_once(ROOT . DS . 'controllers' . DS. 'video'. DS . 'video.controller.php');
// check write permissions first
if(!isFolderWritable(ROOT.DS.'data')) if(!isFolderWritable(ROOT.DS.'data'))
exit(json_encode(array('status'=>'err','reason'=>'Data directory not writable'))); exit(json_encode(array('status'=>'err','reason'=>'Data directory not writable')));
else if(!isFolderWritable(ROOT.DS.'tmp')) else if(!isFolderWritable(ROOT.DS.'tmp'))
exit(json_encode(array('status'=>'err','reason'=>'Temp directory not writable'))); exit(json_encode(array('status'=>'err','reason'=>'Temp directory not writable')));
$hash = sanatizeString(trim($_REQUEST['hash']))?sanatizeString(trim($_REQUEST['hash'])):false;
// check for POST upload // check for POST upload
if ($_FILES['file']["error"] == UPLOAD_ERR_OK) if ($_FILES['file']["error"] == UPLOAD_ERR_OK)
{ {
@@ -37,17 +40,17 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK)
//image? //image?
if(in_array($type,(new ImageController)->getRegisteredExtensions())) if(in_array($type,(new ImageController)->getRegisteredExtensions()))
{ {
$answer = (new ImageController())->handleUpload($_FILES['file']['tmp_name']); $answer = (new ImageController())->handleUpload($_FILES['file']['tmp_name'],$hash);
} }
//or, a text //or, a text
else if($type=='text') else if($type=='text')
{ {
$answer = (new TextController())->handleUpload($_FILES['file']['tmp_name']); $answer = (new TextController())->handleUpload($_FILES['file']['tmp_name'],$hash);
} }
//or, a video //or, a video
else if(in_array($type,(new VideoController)->getRegisteredExtensions())) else if(in_array($type,(new VideoController)->getRegisteredExtensions()))
{ {
$answer = (new VideoController())->handleUpload($_FILES['file']['tmp_name']); $answer = (new VideoController())->handleUpload($_FILES['file']['tmp_name'],$hash);
} }
if(!$answer) if(!$answer)

View File

@@ -40,6 +40,12 @@ class ImageController
{ {
$hash = getNewHash($ext,6); $hash = getNewHash($ext,6);
} }
else
{
$hash.='.'.$ext;
if(isExistingHash($hash))
return array('status'=>'err','reason'=>'Custom hash already exists');
}
if($newsha1) if($newsha1)
addSha1($hash,$newsha1); addSha1($hash,$newsha1);

View File

@@ -38,6 +38,12 @@ class TextController
{ {
$hash = getNewHash('txt',6); $hash = getNewHash('txt',6);
} }
else
{
$hash.='.txt';
if(isExistingHash($hash))
return array('status'=>'err','reason'=>'Custom hash already exists');
}
mkdir(ROOT.DS.'data'.DS.$hash); mkdir(ROOT.DS.'data'.DS.$hash);
$file = ROOT.DS.'data'.DS.$hash.DS.$hash; $file = ROOT.DS.'data'.DS.$hash.DS.$hash;

View File

@@ -10,13 +10,26 @@ class VideoController
$path = ROOT.DS.'data'.DS.$hash.DS.$hash; $path = ROOT.DS.'data'.DS.$hash.DS.$hash;
//@todo: - resize by changing $path //@todo: - resize by changing $path
// - preview images
//check if video should be resized
foreach($url as $u)
if(isSize($u)==true)
$size = $u;
if($size)
{
$s = sizeStringToWidthHeight($size);
$width = $s['width'];
$newpath = ROOT.DS.'data'.DS.$hash.DS.$width.'_'.$hash;
$this->resize($path,$newpath,$width);
$path = $newpath;
}
if(in_array('raw',$url)) if(in_array('raw',$url))
$this->serveMP4($path,$hash); $this->serveMP4($path,$hash);
else if(in_array('preview',$url)) else if(in_array('preview',$url))
{ {
$preview = ROOT.DS.'data'.DS.$hash.DS.'preview.jpg'; $preview = $path.'_preview.jpg';
if(!file_exists($preview)) if(!file_exists($preview))
{ {
$this->saveFirstFrameOfMP4($path,$preview); $this->saveFirstFrameOfMP4($path,$preview);
@@ -51,6 +64,12 @@ class VideoController
{ {
if($hash===false) if($hash===false)
$hash = getNewHash('mp4',6); $hash = getNewHash('mp4',6);
else
{
$hash.='.mp4';
if(isExistingHash($hash))
return array('status'=>'err','reason'=>'Custom hash already exists');
}
mkdir(ROOT.DS.'data'.DS.$hash); mkdir(ROOT.DS.'data'.DS.$hash);
$file = ROOT.DS.'data'.DS.$hash.DS.$hash; $file = ROOT.DS.'data'.DS.$hash.DS.$hash;
@@ -195,4 +214,19 @@ class VideoController
system($cmd); system($cmd);
} }
function resize($in,$out,$width)
{
$file = escapeshellarg($in);
$tmp = '/dev/null';
$bin = escapeshellcmd(FFMPEG_BINARY);
$addition = '-c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p';
$height = 'trunc(ow/a/2)*2';
$cmd = "$bin -i $file -y -vf scale=\"$width:$height\" $addition $out";
system($cmd);
return (file_exists($out) && filesize($out)>0);
}
} }

View File

@@ -113,7 +113,7 @@ function getExtensionOfFilename($file)
function sizeStringToWidthHeight($size) function sizeStringToWidthHeight($size)
{ {
if(!$size || !$this->isSize($size)) return false; if(!$size || !isSize($size)) return false;
if(!is_numeric($size)) if(!is_numeric($size))
$size = explode('x',$size); $size = explode('x',$size);
@@ -325,3 +325,12 @@ function addSha1($hash,$sha1)
fclose($fp); fclose($fp);
return true; return true;
} }
function isSize($var)
{
if(is_numeric($var)) return true;
$a = explode('x',$var);
if(count($a)!=2 || !is_numeric($a[0]) || !is_numeric($a[1])) return false;
return true;
}

View File

@@ -38,8 +38,8 @@
<body id="body"> <body id="body">
<div id="container"> <div id="container">
<video id="video" poster="<?php echo URL.'preview/'.$hash; ?>" preload="auto" autoplay="autoplay" controls muted="muted" loop="loop" webkit-playsinline> <video id="video" poster="<?php echo URL.$url.'/preview/'.$hash; ?>" preload="auto" autoplay="autoplay" controls muted="muted" loop="loop" webkit-playsinline>
<source src="<?php echo '/raw/mp4/'.$hash; ?>" type="video/mp4"> <source src="<?php echo URL.$url.'/raw' ?>" type="video/mp4">
<?php <?php
if(file_exists(ROOT.DS.'data'.DS.$hash.DS.'webm_1.'.$hash)) if(file_exists(ROOT.DS.'data'.DS.$hash.DS.'webm_1.'.$hash))
echo '<source src="'.URL.'raw/webm/'.$hash.'" type="video/webm">'."\n"; echo '<source src="'.URL.'raw/webm/'.$hash.'" type="video/webm">'."\n";
@@ -48,7 +48,7 @@
?> ?>
</video> </video>
</div> </div>
<small><?php echo $filesize; ?> <a href="/<?php echo $url.'/raw' ?>">Raw</a> <a href="/<?php echo $url.'/download' ?>">Download</a></a></small> <small><?php echo $filesize; ?> <a href="/<?php echo URL.$url.'/raw' ?>">Raw</a> <a href="/<?php echo URL.$url.'/download' ?>">Download</a></a></small>
<script> <script>
var hadToResizeW = false; var hadToResizeW = false;