changeset 963:003af1c4ae1f

Implement 'entry unpack'.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 29 Nov 2014 03:08:55 +0200
parents b61c0e7eb52e
children 280dc9b01297
files faptool.php
diffstat 1 files changed, 125 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/faptool.php	Sat Nov 29 03:08:46 2014 +0200
+++ b/faptool.php	Sat Nov 29 03:08:55 2014 +0200
@@ -15,13 +15,6 @@
   global $setPreviewPath;
   $isTemp = FALSE;
 
-  if (($outDim = stGetSetting($setDim)) === FALSE ||
-      ($outFormat = stGetSetting($osetFormat)) === FALSE ||
-      ($outQuality = stGetSetting($setQuality)) === FALSE)
-  {
-    die("Missing one of res/format/quality settings for '".$outFilename."'\n");
-  }
-
   if ($inFileType == "gfx")
   {
     // Oh great .. we need gfxconv here because Imagick handles ILBM like shit
@@ -37,7 +30,6 @@
       echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n";
       return FALSE;
     }
-    
   }
   else
     $tmpFilename = $inFilename;
@@ -53,26 +45,36 @@
     return FALSE;
   }
 
-  // Get dimensions, setup background  
-  $dim = $img->getImageGeometry();
-  $img->setBackgroundColor(imagick::COLOR_BLACK);
-  $img->setGravity(imagick::GRAVITY_CENTER);
-
-  // Act based on image size vs. desired size and $thumb mode
-  if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1])
+  if ($setDim !== FALSE)
   {
-    // Image is larger
-    $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
-    $img->setExtent($outDim[0], $outDim[1]);
-    $img->normalizeImage();
-    $img->unsharpMaskImage(0, 0.5, 1, 0.05);
-  }
-  else
-  if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1])
-  {
-    // Image is smaller than requested dimension(s)?
-    $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1);
-    $img->setExtent($outDim[0], $outDim[1]);
+    if (($outDim = stGetSetting($setDim)) === FALSE ||
+        ($outFormat = stGetSetting($setFormat)) === FALSE ||
+        ($outQuality = stGetSetting($setQuality)) === FALSE)
+    {
+      die("Missing one of res/format/quality settings for '".$outFilename."'\n");
+    }
+
+    // Get dimensions, setup background  
+    $dim = $img->getImageGeometry();
+    $img->setBackgroundColor(imagick::COLOR_BLACK);
+    $img->setGravity(imagick::GRAVITY_CENTER);
+
+    // Act based on image size vs. desired size and $thumb mode
+    if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1])
+    {
+      // Image is larger
+      $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
+      $img->setExtent($outDim[0], $outDim[1]);
+      $img->normalizeImage();
+      $img->unsharpMaskImage(0, 0.5, 1, 0.05);
+    }
+    else
+    if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1])
+    {
+      // Image is smaller than requested dimension(s)?
+      $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1);
+      $img->setExtent($outDim[0], $outDim[1]);
+    }
   }
 
   $img->setFormat($outFormat);
@@ -98,7 +100,7 @@
     "--channels ".$schannels." ".
     "--playtime ".$sduration." ".
     escapeshellarg($inFilename)." -o ".escapeshellarg($outFilename),
-    1);
+    0);
 }
 
 
@@ -121,25 +123,39 @@
 }
 
 
-function wtRenderConvertEntryToSource($filename, $edata, &$pdata)
+function wtRenderConvertEntryToSource($inFilename, $edata, $entryFilename, &$outData, &$outFilename)
 {
-  echo "CHECKING: ".$filename." / ".$edata["id"]."\n";
+  // Does compo preview type match this file class?
+  if ($edata["class"] != $compo["preview_type"])
+    return FALSE;
 
+  $outFilename = stReplaceFileExt($entryFilename, "_preview.".$edata["fext"]);
+  if (file_exists($outFilename) && filemtime($outFilename) >= filemtime($entryFilename))
+    return FALSE;
+
+  echo "CHECKING: ".$inFilename." / ".$edata["id"]."\n";
   switch ($edata["class"])
   {
     case EFILE_AUDIO:
-      // Construct output filename
-      $outFilename = stMakePath(FALSE, TRUE, array("output.wav"));
       if ($edata["mime"] == "audio/x-mod")
-        return wtRenderSample($filename, $outFilename);
+        $res = wtRenderSample($inFilename, $outFilename);
       else
-        return wtConvertSample($filename, $outFilename, array());
+        $res = wtConvertSample($inFilename, $outFilename, array());
       break;
+    
     case EFILE_IMAGE:
-      // Construct output filename
-      //return wtConvertImage();
+      $res = wtConvertImage(
+        $inFilename,
+        $edata["mime"],
+        $outFilename,
+        FALSE, "PNG", 9, FALSE);
+        break;
+    
+    default:
+      return FALSE;
   }
-  return FALSE;
+
+  return $res;
 }
 
 
@@ -204,10 +220,7 @@
     return FALSE;
 
   // Get entry file data
-  if ($entry["file_id"] != 0)
-    $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]);
-  else
-    $efile = FALSE;
+  $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]);
 
   // Check preview file(s) status
   if ($mode == "sta" || $mode == "lis")
@@ -253,7 +266,8 @@
     }
 
     // Preview source file does not exist, let's see ..
-    if (!isset($fileTypeData[$efile["filetype"]]))
+    $edata = stProbeFileInfo($filename, TRUE);
+    if ($edata === false)
     {
       echo
         "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n";
@@ -261,13 +275,10 @@
     }
 
     $found = FALSE;
-    $edata = $fileTypeData[$efile["filetype"]];
-    $edata["id"] = $efile["filetype"];
-
     if ($edata["class"] == EFILE_ARCHIVE)
     {
       // Entry is an archive file ..
-      $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $filename));
+      $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"]));
 
       echo "INFO: Entry file is an ".$efile["filetype"]." archive, scanning '".$filename."' ...\n";
 
@@ -283,8 +294,7 @@
         // if match, render / convert / cp to as source and exit loop.
         if (is_file($fname) &&
           ($mdata = stProbeFileInfo($fname, TRUE)) !== false &&
-          $mdata["class"] == $compo["preview_type"] &&
-          ($found = wtRenderConvertEntryToSource($fname, $mdata, $pdata)) === true)
+          ($found = wtRenderConvertEntryToSource($fname, $mdata, $filename, $pdata, $outFilename)) === true)
             break;
       }
 
@@ -296,7 +306,7 @@
     if ($edata["class"] == $compo["preview_type"])
     {
       // Single file
-      $found = wtRenderConvertEntryToSource($filename, $edata, $pdata);
+      $found = wtRenderConvertEntryToSource($filename, $edata, $filename, $pdata, $outFilename);
     }
 
     if (!$found)
@@ -307,10 +317,9 @@
   }
 
   // Either we now have a sourcefile or generated file
-/*
-  switch (source)
+  switch ($outData["class"])
   {
-    case audio:
+    case EFILE_AUDIO:
       foreach (stGetSetting("sampleTypes") as $stype => $sopts)
       {
         if (!isset($fileTypeData[$stype]))
@@ -326,7 +335,7 @@
       }
       break;
     
-    case image:
+    case EFILE_IMAGE:
       wtConvertImage($inFilename,
         stMakePath(FALSE, TRUE, array($setPreviewPath, $compo["cpath"], $filename)),
         "previewImageSize", "previewImageType",
@@ -338,7 +347,7 @@
         "previewThumbQuality", TRUE);
       break;
   }
-*/
+
   return TRUE;
 }
 
@@ -348,6 +357,57 @@
 }
 
 
+function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig = TRUE)
+{
+  global $setEntryPath;
+
+  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;
+  }
+
+  $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"]));
+  if (!file_exists($filename))
+  {
+    echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n";
+    return FALSE;
+  }
+
+  $edata = stProbeFileInfo($filename, TRUE);
+  if ($edata === false)
+  {
+    echo
+      "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n";
+    return FALSE;
+  }
+
+  if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false)
+    return FALSE;
+
+  $dstFileBase = $useOrig ? $efile["id"]."--".$efile["origname"] : $efile["filename"];
+
+  if ($edata["class"] == EFILE_ARCHIVE)
+  {
+    // Entry is an archive file ..
+    $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, "")));
+    if (wtUnpackArchiveTo($efile["filetype"], $filename, $dstPath) === false)
+      return FALSE;
+  }
+  else
+  {
+    $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase));
+    if (copy($filename, $dstFilename) === false)
+    {
+      echo "Failed to copy '".$filename."' to '".$dstFilename."'\n";
+      return FALSE;
+    }
+  }
+
+  return TRUE;
+}
+
+
 //
 // Misc helper functions
 //
@@ -599,7 +659,6 @@
     $mode = substr(stCArgLC(2), 0, 3);
     switch ($mode)
     {
-      case "unp":
       case "sta":
       case "lis":
         $sql = (stCArg(3) != "") ? " AND id=".intval(stCArg(3)) : "";
@@ -619,6 +678,17 @@
         }
         break;
 
+      case "unp":
+        if (($setPrefix = stCArg(3)) == "")
+          die("Unpack needs a destination path.\n");
+
+        foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
+        foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
+        {
+          wtUnpackEntry($compo, $entry, $setPrefix);
+        }
+        break;
+
       case "pac":
         echo "Not implemented. :D\n";
         break;