diff --git a/README.md b/README.md index b894e92..39e691c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ PictShare is a selfhostable, open source image, video and text hosting as well a - [ ] Upload of links to shorten ### MP4 hosting -- [ ] Resizing +- [x] Resizing - [x] Preview image generation - [x] Upload of videos - [x] Automatic conversion if not mobile friendly or wrong encoder used diff --git a/api/upload.php b/api/upload.php index ec86dcb..35456bb 100644 --- a/api/upload.php +++ b/api/upload.php @@ -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. 'video'. DS . 'video.controller.php'); +// check write permissions first if(!isFolderWritable(ROOT.DS.'data')) exit(json_encode(array('status'=>'err','reason'=>'Data directory not writable'))); else if(!isFolderWritable(ROOT.DS.'tmp')) 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 if ($_FILES['file']["error"] == UPLOAD_ERR_OK) { @@ -37,17 +40,17 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK) //image? 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 else if($type=='text') { - $answer = (new TextController())->handleUpload($_FILES['file']['tmp_name']); + $answer = (new TextController())->handleUpload($_FILES['file']['tmp_name'],$hash); } //or, a video 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) diff --git a/controllers/image/image.controller.php b/controllers/image/image.controller.php index 40b22b3..c58968f 100644 --- a/controllers/image/image.controller.php +++ b/controllers/image/image.controller.php @@ -40,6 +40,12 @@ class ImageController { $hash = getNewHash($ext,6); } + else + { + $hash.='.'.$ext; + if(isExistingHash($hash)) + return array('status'=>'err','reason'=>'Custom hash already exists'); + } if($newsha1) addSha1($hash,$newsha1); diff --git a/controllers/text/text.controller.php b/controllers/text/text.controller.php index ec99945..2ea77bf 100644 --- a/controllers/text/text.controller.php +++ b/controllers/text/text.controller.php @@ -38,6 +38,12 @@ class TextController { $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); $file = ROOT.DS.'data'.DS.$hash.DS.$hash; diff --git a/controllers/video/video.controller.php b/controllers/video/video.controller.php index 8451d96..6a331a8 100644 --- a/controllers/video/video.controller.php +++ b/controllers/video/video.controller.php @@ -10,13 +10,26 @@ class VideoController $path = ROOT.DS.'data'.DS.$hash.DS.$hash; //@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)) $this->serveMP4($path,$hash); else if(in_array('preview',$url)) { - $preview = ROOT.DS.'data'.DS.$hash.DS.'preview.jpg'; + $preview = $path.'_preview.jpg'; if(!file_exists($preview)) { $this->saveFirstFrameOfMP4($path,$preview); @@ -51,6 +64,12 @@ class VideoController { if($hash===false) $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); $file = ROOT.DS.'data'.DS.$hash.DS.$hash; @@ -194,5 +213,20 @@ class VideoController $cmd = "$bin -y -i $file -vframes 1 -f image2 $target"; 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); } } \ No newline at end of file diff --git a/inc/core.php b/inc/core.php index c4511c3..0380996 100644 --- a/inc/core.php +++ b/inc/core.php @@ -113,7 +113,7 @@ function getExtensionOfFilename($file) function sizeStringToWidthHeight($size) { - if(!$size || !$this->isSize($size)) return false; + if(!$size || !isSize($size)) return false; if(!is_numeric($size)) $size = explode('x',$size); @@ -324,4 +324,13 @@ function addSha1($hash,$sha1) fwrite($fp,"$sha1;$hash\n"); fclose($fp); 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; } \ No newline at end of file diff --git a/templates/video.html b/templates/video.html index b17aa68..20a44fd 100644 --- a/templates/video.html +++ b/templates/video.html @@ -38,8 +38,8 @@