mirror of
https://github.com/HaschekSolutions/pictshare.git
synced 2025-11-11 18:56:21 +00:00
implemented alt_folder and reorganized interface class code
This commit is contained in:
98
content-controllers/image/image.controller.php
Normal file
98
content-controllers/image/image.controller.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Todo:
|
||||
* - Resizing
|
||||
* - Filters
|
||||
* - Conversion gif to mp4
|
||||
* - Conversion jpg,png to webp
|
||||
*/
|
||||
|
||||
class ImageController implements ContentController
|
||||
{
|
||||
//returns all extensions registered by this type of content
|
||||
public function getRegisteredExtensions(){return array('png','bmp','gif','jpg','jpeg','x-png','ico','webp');}
|
||||
|
||||
public function handleUpload($tmpfile,$hash=false)
|
||||
{
|
||||
$type = exif_imagetype($tmpfile); //http://www.php.net/manual/en/function.exif-imagetype.php
|
||||
switch($type)
|
||||
{
|
||||
case 1: $ext = 'gif';break; //gif
|
||||
case 3: $ext = 'png';break; // png
|
||||
case 6: $ext = 'bmp';break; // bmp
|
||||
case 17: $ext = 'ico';break; // ico
|
||||
case 18: $ext = 'webp';break; // webp
|
||||
|
||||
case 2: //we clean up exif data of JPGs so GPS and other data is removed
|
||||
$res = imagecreatefromjpeg($tmpfile);
|
||||
imagejpeg($res, $tmpfile, (defined('JPEG_COMPRESSION')?JPEG_COMPRESSION:90));
|
||||
$ext = 'jpg';
|
||||
|
||||
$newsha1 = sha1_file($tmpfile);
|
||||
break;
|
||||
|
||||
default:
|
||||
return array('status'=>'err','reason'=>'Not a valid image');
|
||||
}
|
||||
|
||||
if($hash===false)
|
||||
{
|
||||
$hash = getNewHash($ext,6);
|
||||
}
|
||||
else
|
||||
{
|
||||
$hash.='.'.$ext;
|
||||
if(isExistingHash($hash))
|
||||
return array('status'=>'err','reason'=>'Custom hash already exists');
|
||||
}
|
||||
|
||||
if($newsha1)
|
||||
addSha1($hash,$newsha1);
|
||||
|
||||
mkdir(ROOT.DS.'data'.DS.$hash);
|
||||
$file = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
||||
|
||||
copy($tmpfile, $file);
|
||||
unlink($tmpfile);
|
||||
|
||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
||||
{
|
||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||
}
|
||||
|
||||
public function handleHash($hash,$url)
|
||||
{
|
||||
$path = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
||||
|
||||
$type = getExtensionOfFilename($hash);
|
||||
switch($type)
|
||||
{
|
||||
case 'jpeg':
|
||||
case 'jpg':
|
||||
header ("Content-type: image/jpeg");
|
||||
readfile($path);
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
header ("Content-type: image/png");
|
||||
readfile($path);
|
||||
break;
|
||||
|
||||
case 'gif':
|
||||
header ("Content-type: image/gif");
|
||||
readfile($path);
|
||||
break;
|
||||
|
||||
case 'webp':
|
||||
header ("Content-type: image/webp");
|
||||
readfile($path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
69
content-controllers/text/text.controller.php
Normal file
69
content-controllers/text/text.controller.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
class TextController implements ContentController
|
||||
{
|
||||
//returns all extensions registered by this type of content
|
||||
public function getRegisteredExtensions(){return array('txt');}
|
||||
|
||||
public function handleHash($hash,$url)
|
||||
{
|
||||
$path = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
||||
|
||||
if(in_array('raw',$url))
|
||||
{
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
echo file_get_contents($path);
|
||||
}
|
||||
else if(in_array('download',$url))
|
||||
{
|
||||
if (file_exists($path)) {
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Disposition: attachment; filename="'.basename($path).'"');
|
||||
header('Expires: 0');
|
||||
header('Cache-Control: must-revalidate');
|
||||
header('Pragma: public');
|
||||
header('Content-Length: ' . filesize($path));
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
renderTemplate('text',array('hash'=>$hash,'content'=>htmlentities(file_get_contents($path))));
|
||||
}
|
||||
|
||||
public function handleUpload($tmpfile,$hash=false)
|
||||
{
|
||||
if($hash===false)
|
||||
{
|
||||
$hash = getNewHash('txt',6);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!endswith($hash,'.txt'))
|
||||
$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;
|
||||
|
||||
copy($tmpfile, $file);
|
||||
unlink($tmpfile);
|
||||
|
||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
||||
{
|
||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||
}
|
||||
|
||||
function getTypeOfText($hash)
|
||||
{
|
||||
return file_get_contents(ROOT.DS.'data'.DS.$hash.DS.'type');
|
||||
}
|
||||
}
|
||||
9
content-controllers/url/url.controller.php
Normal file
9
content-controllers/url/url.controller.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class UrlController implements ContentController
|
||||
{
|
||||
//returns all extensions registered by this type of content
|
||||
public function getRegisteredExtensions(){return array('url');}
|
||||
public function handleHash($hash,$url){}
|
||||
public function handleUpload($tmpfile,$hash=false){}
|
||||
}
|
||||
225
content-controllers/video/video.controller.php
Normal file
225
content-controllers/video/video.controller.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
class VideoController implements ContentController
|
||||
{
|
||||
//returns all extensions registered by this type of content
|
||||
public function getRegisteredExtensions(){return array('mp4','ogg','webm');}
|
||||
|
||||
public function handleHash($hash,$url)
|
||||
{
|
||||
$path = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
||||
|
||||
//@todo: - resize by changing $path
|
||||
|
||||
//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;
|
||||
if(!file_exists($newpath))
|
||||
$this->resize($path,$newpath,$width);
|
||||
$path = $newpath;
|
||||
}
|
||||
|
||||
|
||||
if(in_array('raw',$url))
|
||||
$this->serveMP4($path,$hash);
|
||||
else if(in_array('preview',$url))
|
||||
{
|
||||
$preview = $path.'_preview.jpg';
|
||||
if(!file_exists($preview))
|
||||
{
|
||||
$this->saveFirstFrameOfMP4($path,$preview);
|
||||
}
|
||||
|
||||
header ("Content-type: image/jpeg");
|
||||
readfile($preview);
|
||||
|
||||
}
|
||||
else if(in_array('download',$url))
|
||||
{
|
||||
if (file_exists($path)) {
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Disposition: attachment; filename="'.basename($path).'"');
|
||||
header('Expires: 0');
|
||||
header('Cache-Control: must-revalidate');
|
||||
header('Pragma: public');
|
||||
header('Content-Length: ' . filesize($path));
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = array('url'=>implode('/',$url),'hash'=>$hash,'filesize'=>renderSize(filesize($path)));
|
||||
renderTemplate('video',$data);
|
||||
}
|
||||
}
|
||||
|
||||
public function handleUpload($tmpfile,$hash=false)
|
||||
{
|
||||
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;
|
||||
|
||||
copy($tmpfile, $file);
|
||||
unlink($tmpfile);
|
||||
|
||||
if(!$this->rightEncodedMP4($file))
|
||||
system("nohup php ".ROOT.DS.'tools'.DS.'re-encode_mp4.php force '.$hash." > /dev/null 2> /dev/null &");
|
||||
|
||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
||||
{
|
||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||
}
|
||||
|
||||
|
||||
//via gist: https://gist.github.com/codler/3906826
|
||||
function serveMP4($path,$hash)
|
||||
{
|
||||
if ($fp = fopen($path, "rb"))
|
||||
{
|
||||
$size = filesize($path);
|
||||
$length = $size;
|
||||
$start = 0;
|
||||
$end = $size - 1;
|
||||
header('Content-type: video/mp4');
|
||||
header("Accept-Ranges: 0-$length");
|
||||
if (isset($_SERVER['HTTP_RANGE'])) {
|
||||
$c_start = $start;
|
||||
$c_end = $end;
|
||||
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
|
||||
if (strpos($range, ',') !== false) {
|
||||
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
||||
header("Content-Range: bytes $start-$end/$size");
|
||||
exit;
|
||||
}
|
||||
if ($range == '-') {
|
||||
$c_start = $size - substr($range, 1);
|
||||
} else {
|
||||
$range = explode('-', $range);
|
||||
$c_start = $range[0];
|
||||
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
|
||||
}
|
||||
$c_end = ($c_end > $end) ? $end : $c_end;
|
||||
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
|
||||
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
||||
header("Content-Range: bytes $start-$end/$size");
|
||||
exit;
|
||||
}
|
||||
$start = $c_start;
|
||||
$end = $c_end;
|
||||
$length = $end - $start + 1;
|
||||
fseek($fp, $start);
|
||||
header('HTTP/1.1 206 Partial Content');
|
||||
}
|
||||
header("Content-Range: bytes $start-$end/$size");
|
||||
header("Content-Length: ".$length);
|
||||
$buffer = 1024 * 8;
|
||||
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
|
||||
if ($p + $buffer > $end) {
|
||||
$buffer = $end - $p + 1;
|
||||
}
|
||||
set_time_limit(0);
|
||||
echo fread($fp, $buffer);
|
||||
flush();
|
||||
}
|
||||
fclose($fp);
|
||||
exit();
|
||||
} else die('file not found');
|
||||
}
|
||||
|
||||
function isProperMP4($filename)
|
||||
{
|
||||
$file = escapeshellarg($filename);
|
||||
$tmp = ROOT.DS.'tmp'.DS.md5(time()+rand(1,10000)).'.'.rand(1,10000).'.log';
|
||||
$bin = escapeshellcmd(FFMPEG_BINARY);
|
||||
|
||||
|
||||
|
||||
$cmd = "$bin -i $file > $tmp 2>> $tmp";
|
||||
|
||||
system($cmd);
|
||||
|
||||
//var_dump(system( "$bin -i $file "));
|
||||
|
||||
$answer = file($tmp);
|
||||
unlink($tmp);
|
||||
$ismp4 = false;
|
||||
if(is_array($answer))
|
||||
foreach($answer as $line)
|
||||
{
|
||||
$line = trim($line);
|
||||
if(strpos($line,'Duration: 00:00:00')) return false;
|
||||
if(strpos($line, 'Video: h264'))
|
||||
$ismp4 = true;
|
||||
}
|
||||
|
||||
return $ismp4;
|
||||
}
|
||||
|
||||
function rightEncodedMP4($file)
|
||||
{
|
||||
$hash = md5($file);
|
||||
$cmd = FFMPEG_BINARY." -i $file -hide_banner 2> ".ROOT.DS.'tmp'.DS.$hash.'.txt';
|
||||
system($cmd);
|
||||
$results = file(ROOT.DS.'tmp'.DS.$hash.'.txt');
|
||||
foreach($results as $l)
|
||||
{
|
||||
$elements = explode(':',trim($l));
|
||||
$key=trim(array_shift($elements));
|
||||
$value = trim(implode(':',$elements));
|
||||
if($key=='encoder')
|
||||
{
|
||||
if(startsWith(strtolower($value),'lav'))
|
||||
{
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
}
|
||||
unlink(ROOT.DS.'tmp'.DS.$hash.'.txt');
|
||||
return false;
|
||||
}
|
||||
|
||||
function saveFirstFrameOfMP4($path,$target)
|
||||
{
|
||||
$bin = escapeshellcmd(FFMPEG_BINARY);
|
||||
$file = escapeshellarg($path);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user