Files
Yuba/helper.php
2019-10-09 04:07:46 -07:00

429 lines
13 KiB
PHP
Executable File

<?
// Yuba batch helper
// //
//////////////////////////////////////////
$prefs_file = "/Users/".get_current_user()."/Library/Preferences/org.profiteroles.Yuba.php";
$p = unserialize(file_get_contents($prefs_file));
require("functions.php");
require("filetypes.php");
$dm = "";
function dfm ($string, $flag = 0) {
// dumb workaround for debug messages appearing out of order
global $dm;
if ($flag) {
$dm .= str_repeat("-",33)."\n".$string."\n".str_repeat("-",33)."\n";
} else {
$dm .= "[".date('Y-m-d h:i:s')."] ".$string."\n";
}
}
$fid = $argv[1];
$pathname = $argv[2];
$shellpath = escapeshellarg($pathname);
$ext = pathinfo($pathname,PATHINFO_EXTENSION);
$bpath = $argv[3];
$mytime = $argv[4];
$stamp = date("Y-m-d_H-i-s", $mytime);
$messages_log_file = $bpath."/".$stamp."_messages.log";
$tmpdir = "/tmp/yuba/".$mytime;
if (!is_dir($tmpdir)) { mkdir($tmpdir,0777,true); dfm("mkdir ",$tmpdir); }
$file = pathinfo($pathname, PATHINFO_BASENAME);
$ext = pathinfo($pathname, PATHINFO_EXTENSION);
$dbp = new PDO("sqlite:".$bpath."/pool.sqlite3");
$dbp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbp->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
//$dbp->query("PRAGMA page_size = 4096");
//$dbp->query("PRAGMA cache_size = 10000");
//$dbp->query("PRAGMA locking_mode = EXCLUSIVE");
//$dbp->query("PRAGMA synchronous = NORMAL");
$dbp->query("PRAGMA journal_mode = WAL");
$sbatch = array();
$estring = "\nBATCH: ".basename($pathname);
dfm("BATCH INIT ".$file.":".print_r($argv,true));
// Thumbs //////////////////////////////////////////
thumbs:
if ($p['thumbs']) {
dfm("THUMBS",1);
$estring .= " THUMBS";
// "bad" filesizes
$discard = array( 3953,
4977,
5019,
6059,
6616,
17393 );
$tfile = $tmpdir."/".substr($fid,0,2)."/".$fid.".jpg";
$tpfile = $tmpdir."/".substr($fid,0,2)."/".$file.".png"; // workaround for qlmanage naming convention
$dfile = $bpath."/thumbs/".substr($fid, 0, 2)."/".$fid.".jpg";;
if (!is_dir(dirname($tfile))) { @mkdir(dirname($tfile)); dfm("mkdir ".dirname($tfile)); }
if (in_array($ext, $p['t_skip'])) {
// file extension is in the skip list
$estring .= " ->t_skip";
dfm("file extension is in the skip list");
goto icons;
}
if ($p['thumbs'] == 1 && file_exists($dfile)) {
// if this is not a rebuild, first check for an existing thumb file (faster)
dfm("this is not a rebuild, and an existing thumb file was found");
$estring .= " ->skip_fast";
goto icons;
}
$rowid = @$dbp->query("SELECT rowid FROM thumbs WHERE fid='".$fid."'")->fetch()['rowid'];
// check for existing thumb generation attempt
if (!$rowid) {
// no prior attempt
dfm("no prior attempt at thumb file generation was made for ".$fid);
$stmt = $dbp->prepare("INSERT INTO thumbs VALUES (:fid, :created, :relative_path, :width, :height, :tool)");
dfm("mysql insert for thumb generation attempt");
$estring .= " ->generate";
} elseif ($rowid && $p['thumbs'] == 2) {
// prior attempt but rebuild mode
$stmt = $dbp->prepare("UPDATE thumbs SET fid = :fid, created = :created, relative_path = :relative_path, width = :width, height = :height, tool = :tool WHERE rowid = ".$rowid);
dfm("mysql update for rebuild");
$estring .= " ->rebuild";
} else {
// prior attempt
dfm("a prior attempt at thumb generation was found for ".$fid);
$estring .= " ->skip";
goto icons;
}
$cmd['sips'] = $bin_sips." -s format jpeg -s formatOptions 80 --resampleHeightWidthMax ".$p['thumb_size']." ".$shellpath." --out ".$tfile; // add ?? "-d profile --deleteColorManagementProperties"
$cmd['vips'] = $bin_vips." ".$shellpath." -o ".$tfile."[Q=90,optimize_coding] --size=".$p['thumb_size'];
//$cmd['sox'] = $bin_sox." ".$shellpath." -n trim 0 $(".$bin_exiftool." -s -s -s -duration# ".$shellpath." | awk '{print $1/10}') spectrogram -o - | ".$bin_convert." - -crop 800x515+58+30 -scale 515x515! +dither -colors 16 ".$tfile;
$cmd['sox'] = $bin_sox." ".$shellpath." -n trim 0 $(".$bin_exiftool." -s -s -s -duration# ".$shellpath." | awk '{print $1/10}') spectrogram -o ".$tpfile."; ".$bin_sips." -s format jpeg -s formatOptions 80 ".escapeshellarg($tpfile)." --out ".$tfile;
$cmd['ffmpeg'] = $bin_ffmpeg." -ss $(( $(".$bin_mediainfo." --Inform='Video;%Duration%' ".$shellpath." | cut -d'.' -f1) / 10000 )) -i ".$shellpath." -vframes 1 -filter:v scale='400:-1' -q:v 3 ".$tfile;
$cmd['ql-thumbnail'] = $bin_qlthumb." ".$shellpath." ".$tfile." public.jpeg ".$p['thumb_size']." ".$p['thumb_size']." .8";
$cmd['qlmanage'] = $bin_qlmanage." -t -s ".$p['thumb_size']." -o ".dirname($tpfile)." ".$shellpath."; ".$bin_sips." -s format jpeg -s formatOptions 80 ".escapeshellarg($tpfile)." --out ".$tfile;
if (in_array($ext, $p['t_files']['sips'])) {
$external_tool = "sips";
} elseif (in_array($ext, $p['t_files']['ffmpeg'])) {
$external_tool = "ffmpeg";
} elseif (in_array($ext, $p['t_files']['sox'])) {
$external_tool = "sox";
} else {
$external_tool = null;
}
dfm("external tool for ".$ext." = ".$external_tool);
switch ($p['thumb_priority']) {
// external tool priority
case 0: $priority = array($external_tool,"ql-thumbnail","qlmanage"); break;
// ql-thumbnail priority
case 1: $priority = array("ql-thumbnail","qlmanage",$external_tool); break;
// qlmanage priority
case 2: $priority = array("qlmanage","ql-thumbnail",$external_tool); break;
}
dfm("priority for ".$ext." is: ".print_r($priority,true));
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":created",time());
if (!is_dir(dirname($dfile))) { @mkdir(dirname($dfile)); dfm("mkdir ".dirname($dfile)); }
foreach ($priority as $tool) {
if (empty($cmd[$tool])) { break; }
shell_exec($cmd[$tool]." 2>&1");
dfm($cmd[$tool]);
$estring .= " ->".$tool;
$checksize = @filesize($tfile);
if ($checksize && !in_array($checksize,$discard)) {
$estring .= " ->use";
$stmt->BindValue(":tool",$tool);
$stmt->BindValue(":relative_path",substr($dfile, strlen($bpath)));
list($width, $height) = getimagesize($tfile);
$stmt->BindValue(":width",$width);
$stmt->BindValue(":height",$height);
rename($tfile,$dfile);
dfm("moving ".$tfile." to ".$dfile);
break;
} else {
$estring .= " ->discard";
dfm("filesize ".$checksize." is in the banned size array (or is zero)");
}
}
$sbatch[] = $stmt;
$estring .= " ->db";
}
// Icons //////////////////////////////////////////
icons:
if ($p['icons']) {
dfm("ICONS",1);
$estring .= " ICONS";
if ($p['icon_tool'] == 0) {
$tool = "qltool";
} elseif ($p['icon_tool'] == 1) {
$tool = "qlmanage";
}
if (in_array($ext, $p['i_skip'])) {
// file extension is in the skip list
$estring .= " ->i_skip";
goto meta;
}
$tfile = $tmpdir."/".substr($fid,0,2)."/".$file.".png";
if (!is_dir(dirname($tfile))) { @mkdir(dirname($tfile)); }
// workaround for qlmanage naming convention
$rowid = @$dbp->query("SELECT rowid FROM icons WHERE fid='".$fid."'")->fetch()['rowid'];
// check for existing icon generation attempt
if (!$rowid) {
// no prior attempt
$stmt = $dbp->prepare("INSERT INTO icons VALUES (:fid, :hash, :created, :relative_path, :tool)");
$estring .= " ->generate";
} elseif ($rowid && $p['icons'] == 2) {
// prior attempt but rebuild mode
$stmt = $dbp->prepare("UPDATE icons SET fid = :fid, hash = :hash, created = :created, relative_path = :relative_path, tool = :tool WHERE rowid = ".$rowid);
$estring .= " ->rebuild";
} else {
// prior attempt
$estring .= " ->skip";
goto meta;
}
$stmt->BindValue(":fid",$fid);
if ($tool == "qltool") {
$cmd = $bin_qltool." di ".$shellpath." ".$p['icon_size']." ".$p['icon_size']." | base64 --decode > ".escapeshellarg($tfile);
} elseif ($tool == "qlmanage") {
// > code for custom qlgenerator bindings goes here <
$cmd = $bin_qlmanage." -ti -s ".$p['icon_size']." -o ".dirname($tfile)." ".$shellpath;
//$cmd = $bin_qlmanage." -ti -f ".floor($p['icon_size']/128)." -o ".dirname($tfile)." ".$shellpath;
}
shell_exec($cmd);
if (@filesize($tfile)) {
$hash = md5_file($tfile);
$stmt->BindValue(":hash",$hash);
if ($row = @$dbp->query("SELECT * FROM icons WHERE hash='".$hash."'")->fetchAll()[0]) {
// check for another icon with the same hash
$stmt->BindValue(":created",$row['created']);
$stmt->BindValue(":relative_path",$row['relative_path']);
$stmt->BindValue(":tool",$row['tool']);
$estring .= " ->recycle";
} else {
$dfile = $bpath."/icons/".substr($hash, 0, 2)."/".$hash.".png";
if (!is_dir(dirname($dfile))) { @mkdir(dirname($dfile)); }
$estring .= " ->use";
if ($tool == "qltool") {
// qltool makes 2x icons for some reason
//shell_exec($bin_sips." -z ".$p['icon_size']." ".$p['icon_size']." ".$tfile." --out ".$dfile);
shell_exec($bin_convert." ".escapeshellarg($tfile)." -scale 50% -strip -define png:compression-level=9 ".$dfile);
} else {
//rename($tfile,$dfile);
shell_exec($bin_convert." ".escapeshellarg($tfile)." -strip -define png:compression-level=9 ".$dfile);
}
$stmt->BindValue(":created",time());
$stmt->BindValue(":relative_path",substr($dfile,strlen($bpath)));
$stmt->BindValue(":tool",$tool);
}
} else {
$estring .= " ->discard";
}
$sbatch[] = $stmt;
$estring .= " ->db";
}
// Metadata //////////////////////////////////////////
meta:
if ($p['meta']) {
dfm("META",1);
$estring .= " META";
if (!in_array($ext, $p['e_files']) && !in_array($ext, $p['m_files'])) {
$estring .= " ->notmedia";
goto hashy;
}
if (in_array($ext, $p['e_files'])) {
$check = @$dbp->query("SELECT EXISTS(SELECT 1 FROM exiftool WHERE fid='".$fid."')")->fetch();
if (!reset($check)) { // hacky but needed because we set FETCH_ASSOC
$arrstring = shell_exec($bin_exiftool." -php ".$shellpath);
// $rawexif = eval("return ".`$bin_exiftool -php $shellpath`);
// do an addtl check below to prevent "PHP Parse error: syntax error, unexpected end of file, expecting ';'"
if (substr($arrstring,0,5) == "Array") {
$rawexif = eval("return ".$arrstring);
$stmt = $dbp->prepare("INSERT INTO exiftool VALUES (:fid, :tags)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":tags",serialize($rawexif[0]));
$sbatch[] = $stmt;
$estring .= " ->etool";
}
} else {
$estring .= " ->e_found";
}
}
if (in_array($ext, $p['m_files'])) {
$check = @$dbp->query("SELECT EXISTS(SELECT 1 FROM mediainfo WHERE fid='".$fid."')")->fetch();
if (!reset($check)) {
$stmt = $dbp->prepare("INSERT INTO mediainfo VALUES (:fid, :info)");
$stmt->BindValue(":fid",$fid);
//$stmt->BindValue(":info",serialize(parseMediaInfo(shell_exec($bin_mediainfo." --Output=OLDXML ".$shellpath." 2>&1"))));
//$stmt->BindValue(":info",shell_exec($bin_mediainfo." --Output=OLDXML ".$shellpath." 2>&1"));
$stmt->BindValue(":info",shell_exec($bin_mediainfo." --Output=JSON ".$shellpath." 2>&1"));
$sbatch[] = $stmt;
$estring .= " ->minfo";
} else {
$estring .= " ->m_found";
}
}
}
// Hash //////////////////////////////////////////
hashy:
if ($p['hash']) {
dfm("HASH",1);
$estring .= " HASH";
$size = filesize($pathname);
$limit = $p['hash_limit']*1000000000;
$check = @$dbp->query("SELECT EXISTS(SELECT 1 FROM md5 WHERE fid='".$fid."')")->fetch();
if (reset($check)) {
$estring .= " ->exists";
} elseif ($p['hash_limit'] && ($size > $limit)) {
$estring .= " ->too big";
} else {
$estring .= " ->generating";
$stmt = $dbp->prepare("INSERT INTO md5 VALUES (:fid, :hash)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":hash",md5_file($pathname));
$sbatch[] = $stmt;
}
$estring .= " ->done";
}
// Contents //////////////////////////////////////////
contents:
if ($p['contents']) {
dfm("CONTENTS",1);
$estring .= " CONTENTS";
$cpath = $bpath."/contents/".substr($fid, 0, 2);
$cfile = $cpath."/".$fid.".zip";
if (in_array($ext, $p['c_files'])) {
if (!is_dir($cpath)) { mkdir($cpath); }
$max_size = $p['contents_limit'] * 1000;
if ($ext == ".DS_Store") {
// store all DS_Store files no matter how big
$my_size = 1;
} else {
$my_size = filesize($pathname);
}
if (!file_exists($cfile) && $my_size < $max_size) {
$estring .= " ->zip";
$zip = new ZipArchive();
$zip->open($cfile, ZipArchive::CREATE);
$zip->addfile($pathname,basename($pathname));
$zip->close();
$stmt = $dbp->prepare("INSERT INTO contents VALUES (:fid, :created, :relative_path)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":created",time());
$stmt->BindValue(":relative_path",substr($cfile, strlen($bpath)));
$sbatch[] = $stmt;
}
}
}
// Inserts //////////////////////////////////////////
foreach ($sbatch as $stmt) { $stmt->execute(); }
dm($dm);
echo msg($estring);
?>