# HG changeset patch # User Matti Hamalainen # Date 1417445565 -7200 # Node ID fbc2e6514dfac69c6fcd5e3dda6d9e7ad999f616 # Parent 5d04bd1a7a10767a31a3c03ac3b0a1dfd0f30958 Cleanups and improved comments. diff -r 5d04bd1a7a10 -r fbc2e6514dfa faptool.php --- a/faptool.php Sun Nov 30 03:15:25 2014 +0200 +++ b/faptool.php Mon Dec 01 16:52:45 2014 +0200 @@ -190,10 +190,13 @@ return FALSE; } - // Create temporary directory + // Delete any pre-existing directory wtPurgeDir($path); + + // Create temporary directory for unpacking wtMakeDir($path, 0700); + // Save current working directory and chdir to target $cwd = getcwd(); if (!is_dir($path) || chdir($path) === false) { @@ -204,22 +207,12 @@ // Unpack archive $ret = wtExec($exe, $args); + // Return to stored cwd chdir($cwd); return $ret; } -function wtNiceName($compo, $entry, $efile = FALSE) -{ - return sprintf( - "%d: %s by %s%s", - $entry["id"], - $entry["name"], - $entry["author"], - ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); -} - - function wtHandleEntryPreview($compo, $entry, $mode) { global $fileTypeData, $setEntryPath; @@ -350,10 +343,15 @@ return TRUE; } - // Either we now have a sourcefile or generated file + // + // Either we now have a "user specified" source file or + // a generated source file - in any case, now convert it + // to the final form(s). + // switch ($compo["preview_type"]) { case EFILE_AUDIO: + // Convert to each specified sample type (MP3, Ogg, etc.) foreach (stGetSetting("sampleTypes") as $stype => $sopts) { wtConvertSample( @@ -365,6 +363,7 @@ break; case EFILE_IMAGE: + // Images we convert to bigger image and thumbnail wtConvertImage( $inFilename, $pdata["files"]["image"]["file"], @@ -392,16 +391,22 @@ } +// +// Crop a filename to specified length +// function wtCropFilename($filename, $len) { if ($len !== FALSE && strlen($filename) > $len) { + // Check if the filename has a path and attempt + // to get the filename extension $fext = ""; $dpos = strrpos($filename, "/"); if (($spos = strrpos($filename, ".")) !== FALSE && ($dpos === FALSE || $dpos < $spos)) $fext = substr($filename, $spos); + // Is the filename extension too long, if yes, just dump it if (strlen($fext) >= $len) $fext = ""; @@ -412,12 +417,12 @@ } +// +// Recursively goes through directory/files and subdirectories, +// cropping file/dir names to specified length. +// function wtCropFilenamesRec($path, $len) { - echo "X: ".$path.", $len\n"; - if ($len === false && $len <= 4) - return FALSE; - $dir = opendir($path); while (($dentry = readdir($dir)) !== false) { @@ -450,19 +455,24 @@ { global $setEntryPath; + // Get latest file for the entry if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false) { echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; return FALSE; } + // Create source file full path $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); + + // Check if the file exists if (!file_exists($filename)) { echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; return FALSE; } + // Get file data $edata = stProbeFileInfo($filename, TRUE); if ($edata === false) { @@ -471,29 +481,36 @@ return FALSE; } + // Create the destination directory if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false) return FALSE; + // Form the destination path and/or filename if ($copyOnly) $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"]; else $dstFileBase = wtCropFilename($entry["show_id"]."-".($useOrig ? $efile["origname"] : $efile["filename"]), $cropNames); + + // Handle based on class/type and whether we are just copying or not if (!$copyOnly && $edata["class"] == EFILE_ARCHIVE) { - // Entry is an archive file .. + // Entry is an archive file, so unpack it $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, ""))); if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false) return FALSE; - wtCropFilenamesRec($dstPath, $cropNames); + // Crop the filenames from the unpacked archive, if we need to + if ($cropNames !== false) + wtCropFilenamesRec($dstPath, $cropNames); } else { + // We have a single file (or copyOnly mode) $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); if (copy($filename, $dstFilename) === false) { - echo "Failed to copy '".$filename."' to '".$dstFilename."'\n"; + echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; return FALSE; } } @@ -505,6 +522,18 @@ // // Misc helper functions // + +function wtNiceName($compo, $entry, $efile = FALSE) +{ + return sprintf( + "%d: %s by %s%s", + $entry["id"], + $entry["name"], + $entry["author"], + ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); +} + + function wtExec($exe, $args, $expect = 0) { echo "EXEC: ".$exe." @ ".$args."\n";