This commit is contained in:
2017-05-22 23:37:36 -07:00
parent 07bd8e897e
commit 2ad83fcb34

268
Yuba.php
View File

@@ -3,22 +3,27 @@
// Yuba // Yuba
////////////////////////////////////////// //////////////////////////////////////////
$version = "0.2.1";
$thumb_size = "256";
$thumb_factor = $thumb_size/128;
// Path arguments
$zpath = realpath($argv[1]);
if (!is_dir($zpath)) { echo "Usage: walk <path> <dbfile>"; die; }
if (isset($argv[2]) && is_dir($argv[2])) {
$dbprefix = realpath($argv[2]);
} else {
$dbprefix = ".";
}
// File checks // File checks
$version = "0.1.1";
date_default_timezone_set("America/Los_Angeles"); date_default_timezone_set("America/Los_Angeles");
$time_start = microtime(true); $time_start = microtime(true);
$stamp = date("Y-m-d_H-i-s", time()); $stamp = date("Y-m-d_H-i-s", time());
$tmpdir = "/tmp/WalkWalk_".$stamp."/"; $tmpdir = "/tmp/WalkWalk_".$stamp."/";
if (!is_dir($tmpdir)) { mkdir($tmpdir); } if (!is_dir($tmpdir)) { mkdir($tmpdir); }
$zpath = realpath($argv[1]);
if (!$zpath) { echo "Usage: walk <path> <dbfile>"; die; }
if (isset($argv[2]) && is_dir($argv[2])) {
$dbprefix = chop($argv[2],"/");
} else {
$dbprefix = ".";
}
$base = preg_replace("/[^A-Za-z0-9\.]/", "_", basename($zpath)); $base = preg_replace("/[^A-Za-z0-9\.]/", "_", basename($zpath));
$dbfile = $dbprefix."/".$stamp."_".$base.".sqlite3"; $dbfile = $dbprefix."/".$stamp."_".$base.".sqlite3";
if (file_exists($dbfile)) { echo "File \"".$dbfile."\" already exists!"; die; } if (file_exists($dbfile)) { echo "File \"".$dbfile."\" already exists!"; die; }
@@ -31,11 +36,56 @@ $banner = $zpath." -> ".$dbfile;
echo $banner."\n"; echo $banner."\n";
echo str_repeat("-", strlen($banner))."\n"; echo str_repeat("-", strlen($banner))."\n";
// Database // Disk info
$host = gethostname();
$disks = shell_exec("diskutil list 2>&1");
if (substr($zpath, 0, 9) != "/Volumes/") {
$zbase = "/";
} else {
$zparts = explode("/", $zpath);
$zbase = "/Volumes/".$zparts[2];
}
$diskutil = shell_exec("diskutil info ".$zbase." 2>&1");
$getstats = array( "Volume Name",
"Protocol",
"Volume UUID",
"Device Location",
"Volume Total Space",
"Volume Available Space",
"Level Type"
);
foreach ($getstats as $stat) {
preg_match("/(".$stat.":)(.*)(\n)/",$diskutil,$matches);
if (isset($matches[2])) {
if (substr($stat, -5, 5) == "Space") {
$pieces = explode(" ", trim($matches[2]));
$summary = $pieces[0]." ".$pieces[1];
$stats[$stat] = $summary;
} else {
$stats[$stat] = trim($matches[2]);
}
}
}
$dstats = serialize($stats);
if ($zpath == "/") {
$type = "Startup disk";
} elseif (strtolower($zpath) == strtolower("/Volumes/".$stats["Volume Name"])) {
if ($stats["Protocol"] == "Disk Image") {
$type = "Disk image";
} else {
$type = "External disk";
}
} else {
$type = "Folder";
}
$profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1"); $profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1");
$disks = shell_exec("diskutil list 2>&1");
$host = gethostname(); // Database
$dbo = new PDO("sqlite:".$dbfile); $dbo = new PDO("sqlite:".$dbfile);
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@@ -57,21 +107,33 @@ $dbo->exec("CREATE TABLE IF NOT EXISTS \"".$base."\" (
mdls TEXT, mdls TEXT,
tags TEXT, tags TEXT,
mediainfo TEXT, mediainfo TEXT,
exif TEXT, exiftool TEXT,
tinfo TEXT)");
$dbo->exec("CREATE TABLE IF NOT EXISTS thumbs (
id INTEGER PRIMARY KEY,
thumb BLOB)"); thumb BLOB)");
$dbo->exec("CREATE TABLE IF NOT EXISTS _walkwalk ( $dbo->exec("CREATE TABLE IF NOT EXISTS _walkwalk (
version TEXT, version TEXT,
host TEXT, host TEXT,
profile TEXT, zpath TEXT,
disks TEXT)"); type TEXT,
stats TEXT,
diskutil TEXT,
disks TEXT,
profile TEXT)");
$insert = "INSERT INTO _walkwalk VALUES (:version, :host, :profile, :disks)"; $insert = "INSERT INTO _walkwalk VALUES (:version, :host, :zpath, :type, :stats, :diskutil, :disks, :profile)";
$stmt = $dbo->prepare($insert); $stmt = $dbo->prepare($insert);
$stmt->BindValue(":version",$version); $stmt->BindValue(":version",$version." (".posix_getuid().")");
$stmt->BindValue(":host",$host); $stmt->BindValue(":host",$host);
$stmt->BindValue(":profile",$profile); $stmt->BindValue(":zpath",$zpath);
$stmt->BindValue(":type",$type);
$stmt->BindValue(":stats",$dstats);
$stmt->BindValue(":diskutil",$diskutil);
$stmt->BindValue(":disks",$disks); $stmt->BindValue(":disks",$disks);
$stmt->BindValue(":profile",$profile);
$stmt->execute(); $stmt->execute();
// Iterate // Iterate
@@ -83,12 +145,137 @@ $iter = new RecursiveIteratorIterator($dir_iter,
RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD RecursiveIteratorIterator::CATCH_GET_CHILD
); );
// Check perms
if (posix_getuid()) {
echo "You are not root. Checking file readability: ";
$oops = 0;
foreach ($iter as $splFileInfo) {
$path = $splFileInfo->getRealPath();
if (!is_readable($path)) {
$oops = 1;
echo "x";
} else {
echo ".";
}
}
echo "\n\n";
if ($oops) {
echo "Some files could not be read. Continue? (Y/n)";
$line = trim(fgets(fopen("php://stdin","r")));
$line = $line ?: "y";
if($line != "y"){
echo "Exiting!\n"; die;
}
}
}
// Filetypes
$m_files = array( "mkv",
"ogg",
"avi",
"wav",
"mpeg",
"mpg",
"vob",
"mp4",
"m2v",
"mp3",
"asf",
"wma",
"wmv",
"qt",
"mov",
"rm",
"ifo",
"ac3",
"dts",
"aac",
"ape",
"flac",
"aiff",
"m2ts" );
$e_files = array( "ai",
"aiff",
"ape",
"asf",
"avi",
"bmp",
"divx",
"dng",
"doc",
"docx",
"eps",
"epub",
"exe",
"exif",
"fla",
"flac",
"flv",
"gif",
"icc",
"iso",
"jpg",
"jpeg",
"m2ts",
"m4a",
"m4b",
"m4v",
"mkv",
"mobi",
"azw",
"azw3",
"mov",
"qt",
"mp3",
"mp4",
"mpeg",
"mpg",
"m2v",
"nef",
"numbers",
"ogg",
"pages",
"pdf",
"pict",
"png",
"ppm",
"ppt",
"psd",
"psb",
"qif",
"raw",
"rtf",
"sr2",
"srf",
"svg",
"swf",
"tiff",
"tif",
"torrent",
"vcf",
"vob",
"wav",
"webm",
"wma",
"wmv",
"xls",
"xlsx",
"xmp",
"zip" );
// Insert // Insert
foreach ($iter as $splFileInfo) { foreach ($iter as $splFileInfo) {
$path = $splFileInfo->getRealPath(); $path = $splFileInfo->getRealPath();
echo $path."\n"; echo $path.": ";
$line = array(); $line = array();
$info = pathinfo($path); $info = pathinfo($path);
$select = "SELECT id from \"".$base."\" WHERE (path=\"".$info['dirname']."\")"; $select = "SELECT id from \"".$base."\" WHERE (path=\"".$info['dirname']."\")";
@@ -107,13 +294,16 @@ foreach ($iter as $splFileInfo) {
} else { } else {
$line['size'] = filesize($path); $line['size'] = filesize($path);
} }
echo floor($line['size']/1024)."k; ";
$line['hash'] = md5_file($path); $line['hash'] = md5_file($path);
echo "hashed; ";
$line['perms'] = fileperms($path); $line['perms'] = fileperms($path);
$line['owner'] = fileowner($path).":".filegroup($path); $line['owner'] = fileowner($path).":".filegroup($path);
$line['inode'] = fileinode($path); $line['inode'] = fileinode($path);
$line['mtime'] = filemtime($path); $line['mtime'] = filemtime($path);
$line['atime'] = fileatime($path); $line['atime'] = fileatime($path);
$line['ctime'] = filectime($path); $line['ctime'] = filectime($path);
echo "perms; ";
$spotlight = shell_exec("mdls \"".$path."\" 2>&1"); $spotlight = shell_exec("mdls \"".$path."\" 2>&1");
if ($spotlight != $path.": could not find ".$path.".\n") { if ($spotlight != $path.": could not find ".$path.".\n") {
$line['mdls'] = $spotlight; $line['mdls'] = $spotlight;
@@ -126,16 +316,27 @@ foreach ($iter as $splFileInfo) {
$line['tags'] = serialize($colors); $line['tags'] = serialize($colors);
} }
} }
$line['mediainfo'] = ""; echo "mdls; ";
$line['exif'] = "";
@exec("qlmanage -t -o ".$tmpdir." \"".$path."\" 2>&1");
$thumb = $tmpdir.basename($path).".png"; $thumb = $tmpdir.basename($path).".png";
if (file_exists($thumb)) { @exec("qlmanage -t -f ".$thumb_factor." -o ".$tmpdir." \"".$path."\" 2>&1");
$line['thumb'] = file_get_contents($thumb); if (!file_exists($thumb) && (in_array($line['ext'], $m_files) | in_array($line['ext'], $e_files))) {
@exec("ffmpegthumbnailer -i \"".$path."\" -o \"".$thumb."\" -s ".$thumb_size." -c png 2>&1");
echo "ffmpeg-";
} }
if (file_exists($thumb) && filesize($thumb)) {
$insert = "INSERT INTO \"".$base."\" VALUES (:id, :parent, :path, :ext, :type, :size, :hash, :perms, :owner, :inode, :mtime, :atime, :ctime, :mdls, :tags, :mediainfo, :exif, :thumb)"; $line['thumb'] = file_get_contents($thumb);
$stmt = null; $line['tinfo'] = serialize(getimagesize($thumb));
}
echo "thumb; ";
if ($line['ext'] && in_array($line['ext'], $m_files)) {
$line['mediainfo'] = shell_exec("mediainfo \"".$path."\" 2>&1");
}
echo "media; ";
if ($line['ext'] && in_array($line['ext'], $e_files)) {
$line['exiftool'] = shell_exec("exiftool \"".$path."\" 2>&1");
}
echo "exif ";
$insert = "INSERT INTO \"".$base."\" VALUES (:id, :parent, :path, :ext, :type, :size, :hash, :perms, :owner, :inode, :mtime, :atime, :ctime, :mdls, :tags, :mediainfo, :exiftool, :tinfo)";
$stmt = $dbo->prepare($insert); $stmt = $dbo->prepare($insert);
$stmt->BindValue(":parent",$line['parent']); $stmt->BindValue(":parent",$line['parent']);
$stmt->BindValue(":path",$line['path']); $stmt->BindValue(":path",$line['path']);
@@ -151,10 +352,19 @@ foreach ($iter as $splFileInfo) {
$stmt->BindValue(":ctime",$line['ctime']); $stmt->BindValue(":ctime",$line['ctime']);
$stmt->BindValue(":mdls",$line['mdls']); $stmt->BindValue(":mdls",$line['mdls']);
$stmt->BindValue(":tags",@$line['tags']); $stmt->BindValue(":tags",@$line['tags']);
$stmt->BindValue(":mediainfo",$line['mediainfo']); $stmt->BindValue(":mediainfo",@$line['mediainfo']);
$stmt->BindValue(":exif",$line['exif']); $stmt->BindValue(":exiftool",@$line['exiftool']);
$stmt->BindValue(":tinfo",@$line['tinfo']);
$stmt->execute();
echo "-> sql";
$insert = "INSERT INTO thumbs VALUES (:id, :thumb)";
$stmt = $dbo->prepare($insert);
$stmt->BindValue(":thumb",@$line['thumb']); $stmt->BindValue(":thumb",@$line['thumb']);
$stmt->execute(); $stmt->execute();
if (@$line['thumb']) {
echo " (thumb)";
}
echo "\n";
} }