view faptool.php @ 882:25bcbd6d5682

Moar work on the tool.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Nov 2014 16:33:25 +0200
parents 7c805dccd4f7
children bc64949e7a97
line wrap: on
line source

#!/usr/bin/php
<?
require_once "mconfig.inc.php";
require_once "msite.inc.php";

stCheckCLIExec();


function wtExecOrDie($exe, $args)
{
  echo "EXEC: ".$exe." ".$args."\n";
  //passthru($exe." ".$args) == 0 or die("Error executing ".$exe.":\n".$args);
}


function wtConvertImage($inFilename, $inFileType, $outFilename, $setDim, $setFormat, $setQuality, $thumb)
{
  global $setPreviewPath;

  if (($outDim = stGetSetting($setDim)) === FALSE ||
      ($outFormat = stGetSetting($osetFormat)) === FALSE ||
      ($outQuality = stGetSetting($setQuality)) === FALSE)
  {
    die("Missing one of res/format/quality settings for '".$outFilename."'\n");
  }

  // convert -resize 640x480 -background black -gravity center -extent 640x480
  //         -unsharp "0x0.75+0.75+0.008" lol.lbm -quality 95 test.jpg
}


function wtRenderSample()
{
  $sfreq     = intval(stGetSetting("sampleFreq", 44100));
  $sduration = intval(stGetSetting("sampleDuration", 30));
  $schannels = intval(stGetSetting("sampleChannels", 1));

}


function wtConvertSample()
{
  $sfreq     = intval(stGetSetting("sampleFreq", 44100));
  $sduration = intval(stGetSetting("sampleDuration", 30));
  $schannels = intval(stGetSetting("sampleChannels", 1));

}


function wtPurgeDir($path)
{
  if (file_exists($path))
    wtExecOrDie("/bin/echo", "-fR ".escapeshellarg($path));
}


function wtScanArchive($compo, $efile, $filename)
{
  global $setEntryPath;

  $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $filename));

  echo "Attempting to scan archive file '".$filename."' ...\n";

  // Check file type before doing anything
  switch ($efile["id"])
  {
    case "LHA":
      $exe = "/usr/bin/lha";
      $args = "e ".escapeshellarg($filename);
      break;

    case "ZIP":
      $exe = "/usr/bin/unzip";
      $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename);
      break;

    default:
      die("Unsupported archive file type: ".$efile["id"]."\n");
  }

  // Create temporary directory
  wtPurgeDir($path);
  stMakeDir($path, 0700);

  if (!is_dir($path) || chdir($path) === false)
    die("Failed to chdir to ".$path."\n");

  // Unpack archive
  wtExecOrDie($exe, $args);

  // Scan through files ...
  $dir = opendir($path);
  while (($dentry = readdir($dir)) !== false)
  {
  }
  closedir($dir);

  wtPurgeDir($path);
}


function wtHandleEntryPreview($compo, $entry, $mode)
{
/*
  if (previewFile does not exist || previewFile older than entryFile)
  {
    if (entry is module file)
    {
      render sample
    }
    else
    if (entry is image file)
    {
      convert image
    }
  }

  // Based on compo / entry preview type ..
  
  // Convert preview image file, if any
  // Create thumbnail for it, too
  wtConvertImage($inFilename, $outFilename,
    "previewImageSize", "previewImageType",
    "previewImageQuality", FALSE);

  wtConvertImage($inFilename, $outFilename,
    "previewThumbSize", "previewThumbType",
    "previewThumbQuality", TRUE);
*/
}


//
// Create directories
//
function stMakeDir($path, $perm)
{
  if (!file_exists($path))
  {
    echo " - Creating ".$path."\n";
    if (mkdir($path, $perm, TRUE) === false)
      die("Could not create directory '".$path."'\n");
  }
}


function stInitializeDirs()
{
  global $setEntryPath, $setPreviewPath, $setThumbDir,
    $setEntryPathPerms, $setPrevPathPerms;

  echo "Checking for missing directories ...\n";
  stMakeDir($setEntryPath, $setEntryPathPerms);
  stMakeDir($setPreviewPath, $setPrevPathPerms);
  stMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $setThumbDir)), $setPrevPathPerms);

  foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
  {
    stMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms);
  }
}


//
// Main program starts
//
if ($argc < 2)
{
  echo
    "faptool - Do stuff with FAPWeb database\n".
    "(C) Copyright 2014 ccr/TNSP\n".
    "\n".
    "Usage: ".$argv[0]." <mode> [args]\n".
    "Where mode is one of following:\n".
    "\n".
    "  init\n".
    "     Create directories for entries and previews, if needed.\n".
    "\n".
    "  previews <cmd> [args]\n".
    "    Where <cmd> is one of:\n".
    "    status   - List files and show what is missing, etc.\n".
    "\n";
  exit;
}

// Try to connect to database
$spec = stGetSetting("sqlDB");
if (($db = stConnectSQLDBSpec($spec)) === false)
  die("Could not connect to SQL database '".$spec."'.\n");

echo "Using database spec '".$spec."'.\n";

// Fetch non-"hardcoded" settings from SQL database
stReloadSettings();

// Set some globals for our benefit
$setEntryPath = stGetSetting("entryPath");
$setPreviewPath = stGetSetting("previewPath");
$setPreviewURL = stGetSetting("previewURL");
$setThumbDir = stGetSetting("thumbnailSubDir");
$setEntryPathPerms = stGetSetting("entryPathPerms");
$setPrevPathPerms = stGetSetting("previewPathPerms");

if ($setEntryPath === FALSE || $setPreviewPath === FALSE ||
    $setPreviewURL === FALSE || $setThumbDir === FALSE ||
    $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE)
{
  die("Some required settings not defined in mconfig.inc.php!\n");
}


// Act according to specified command
switch (substr(stCArgLC(1), 0, 4))
{
  case "init":
    //
    // Initialize the data directories etc
    //
    stInitializeDirs();
    break;

  case "prev":
    //
    // Preview files handling
    //
    $mode = substr(stCArgLC(2), 0, 3);
    switch ($mode)
    {
      case "sta":
        // List previews that are found and missing
        foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
        {
          printf(
            "\n".
            "--[ #%03d - %-30s ]---\n".
            "PrevType : %s\n".
            "PrevPath : %s\n".
            "----------------------------------------------\n",
            $compo["id"], $compo["name"],
            $previewTypeList[$compo["preview_type"]][0],
            stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])));

          foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
            wtHandleEntryPreview($compo, $entry, $mode);
        }
        break;

      default:
        if ($mode == "")
          die("ERROR! Previews command requires a sub-command argument.\n");
        else
          die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n");
        break;
    }
    break;

  default:
    echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
    break;
}

?>