comparison faptool.php @ 993:fbc2e6514dfa fap2014

Cleanups and improved comments.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Dec 2014 16:52:45 +0200
parents 3e9e0aa21a18
children ffacd904fd1f
comparison
equal deleted inserted replaced
992:5d04bd1a7a10 993:fbc2e6514dfa
188 default: 188 default:
189 echo "Unsupported archive file type: ".$atype."\n"; 189 echo "Unsupported archive file type: ".$atype."\n";
190 return FALSE; 190 return FALSE;
191 } 191 }
192 192
193 // Create temporary directory 193 // Delete any pre-existing directory
194 wtPurgeDir($path); 194 wtPurgeDir($path);
195
196 // Create temporary directory for unpacking
195 wtMakeDir($path, 0700); 197 wtMakeDir($path, 0700);
196 198
199 // Save current working directory and chdir to target
197 $cwd = getcwd(); 200 $cwd = getcwd();
198 if (!is_dir($path) || chdir($path) === false) 201 if (!is_dir($path) || chdir($path) === false)
199 { 202 {
200 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; 203 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n";
201 return FALSE; 204 return FALSE;
202 } 205 }
203 206
204 // Unpack archive 207 // Unpack archive
205 $ret = wtExec($exe, $args); 208 $ret = wtExec($exe, $args);
206 209
210 // Return to stored cwd
207 chdir($cwd); 211 chdir($cwd);
208 return $ret; 212 return $ret;
209 }
210
211
212 function wtNiceName($compo, $entry, $efile = FALSE)
213 {
214 return sprintf(
215 "%d: %s by %s%s",
216 $entry["id"],
217 $entry["name"],
218 $entry["author"],
219 ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : "");
220 } 213 }
221 214
222 215
223 function wtHandleEntryPreview($compo, $entry, $mode) 216 function wtHandleEntryPreview($compo, $entry, $mode)
224 { 217 {
348 } 341 }
349 if ($valid) 342 if ($valid)
350 return TRUE; 343 return TRUE;
351 } 344 }
352 345
353 // Either we now have a sourcefile or generated file 346 //
347 // Either we now have a "user specified" source file or
348 // a generated source file - in any case, now convert it
349 // to the final form(s).
350 //
354 switch ($compo["preview_type"]) 351 switch ($compo["preview_type"])
355 { 352 {
356 case EFILE_AUDIO: 353 case EFILE_AUDIO:
354 // Convert to each specified sample type (MP3, Ogg, etc.)
357 foreach (stGetSetting("sampleTypes") as $stype => $sopts) 355 foreach (stGetSetting("sampleTypes") as $stype => $sopts)
358 { 356 {
359 wtConvertSample( 357 wtConvertSample(
360 $inFilename, 358 $inFilename,
361 $pdata["files"][$stype]["file"], 359 $pdata["files"][$stype]["file"],
363 ); 361 );
364 } 362 }
365 break; 363 break;
366 364
367 case EFILE_IMAGE: 365 case EFILE_IMAGE:
366 // Images we convert to bigger image and thumbnail
368 wtConvertImage( 367 wtConvertImage(
369 $inFilename, 368 $inFilename,
370 $pdata["files"]["image"]["file"], 369 $pdata["files"]["image"]["file"],
371 stGetSetting("previewImageSize"), 370 stGetSetting("previewImageSize"),
372 stGetSetting("previewImageType"), 371 stGetSetting("previewImageType"),
390 function wtHandleEntry($compo, $entry, $mode) 389 function wtHandleEntry($compo, $entry, $mode)
391 { 390 {
392 } 391 }
393 392
394 393
394 //
395 // Crop a filename to specified length
396 //
395 function wtCropFilename($filename, $len) 397 function wtCropFilename($filename, $len)
396 { 398 {
397 if ($len !== FALSE && strlen($filename) > $len) 399 if ($len !== FALSE && strlen($filename) > $len)
398 { 400 {
401 // Check if the filename has a path and attempt
402 // to get the filename extension
399 $fext = ""; 403 $fext = "";
400 $dpos = strrpos($filename, "/"); 404 $dpos = strrpos($filename, "/");
401 if (($spos = strrpos($filename, ".")) !== FALSE && 405 if (($spos = strrpos($filename, ".")) !== FALSE &&
402 ($dpos === FALSE || $dpos < $spos)) 406 ($dpos === FALSE || $dpos < $spos))
403 $fext = substr($filename, $spos); 407 $fext = substr($filename, $spos);
404 408
409 // Is the filename extension too long, if yes, just dump it
405 if (strlen($fext) >= $len) 410 if (strlen($fext) >= $len)
406 $fext = ""; 411 $fext = "";
407 412
408 return substr($filename, 0, $len - strlen($fext) - 1).$fext; 413 return substr($filename, 0, $len - strlen($fext) - 1).$fext;
409 } 414 }
410 else 415 else
411 return $filename; 416 return $filename;
412 } 417 }
413 418
414 419
420 //
421 // Recursively goes through directory/files and subdirectories,
422 // cropping file/dir names to specified length.
423 //
415 function wtCropFilenamesRec($path, $len) 424 function wtCropFilenamesRec($path, $len)
416 { 425 {
417 echo "X: ".$path.", $len\n";
418 if ($len === false && $len <= 4)
419 return FALSE;
420
421 $dir = opendir($path); 426 $dir = opendir($path);
422 while (($dentry = readdir($dir)) !== false) 427 while (($dentry = readdir($dir)) !== false)
423 { 428 {
424 $fname = $path."/".$dentry; 429 $fname = $path."/".$dentry;
425 $nname = $path."/".wtCropFilename($dentry, $len); 430 $nname = $path."/".wtCropFilename($dentry, $len);
448 453
449 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig, $cropNames, $copyOnly) 454 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig, $cropNames, $copyOnly)
450 { 455 {
451 global $setEntryPath; 456 global $setEntryPath;
452 457
458 // Get latest file for the entry
453 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false) 459 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false)
454 { 460 {
455 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; 461 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n";
456 return FALSE; 462 return FALSE;
457 } 463 }
458 464
465 // Create source file full path
459 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); 466 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"]));
467
468 // Check if the file exists
460 if (!file_exists($filename)) 469 if (!file_exists($filename))
461 { 470 {
462 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; 471 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n";
463 return FALSE; 472 return FALSE;
464 } 473 }
465 474
475 // Get file data
466 $edata = stProbeFileInfo($filename, TRUE); 476 $edata = stProbeFileInfo($filename, TRUE);
467 if ($edata === false) 477 if ($edata === false)
468 { 478 {
469 echo 479 echo
470 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; 480 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n";
471 return FALSE; 481 return FALSE;
472 } 482 }
473 483
484 // Create the destination directory
474 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false) 485 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false)
475 return FALSE; 486 return FALSE;
476 487
488 // Form the destination path and/or filename
477 if ($copyOnly) 489 if ($copyOnly)
478 $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"]; 490 $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"];
479 else 491 else
480 $dstFileBase = wtCropFilename($entry["show_id"]."-".($useOrig ? $efile["origname"] : $efile["filename"]), $cropNames); 492 $dstFileBase = wtCropFilename($entry["show_id"]."-".($useOrig ? $efile["origname"] : $efile["filename"]), $cropNames);
481 493
494
495 // Handle based on class/type and whether we are just copying or not
482 if (!$copyOnly && $edata["class"] == EFILE_ARCHIVE) 496 if (!$copyOnly && $edata["class"] == EFILE_ARCHIVE)
483 { 497 {
484 // Entry is an archive file .. 498 // Entry is an archive file, so unpack it
485 $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, ""))); 499 $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, "")));
486 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false) 500 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false)
487 return FALSE; 501 return FALSE;
488 502
489 wtCropFilenamesRec($dstPath, $cropNames); 503 // Crop the filenames from the unpacked archive, if we need to
504 if ($cropNames !== false)
505 wtCropFilenamesRec($dstPath, $cropNames);
490 } 506 }
491 else 507 else
492 { 508 {
509 // We have a single file (or copyOnly mode)
493 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); 510 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase));
494 if (copy($filename, $dstFilename) === false) 511 if (copy($filename, $dstFilename) === false)
495 { 512 {
496 echo "Failed to copy '".$filename."' to '".$dstFilename."'\n"; 513 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n";
497 return FALSE; 514 return FALSE;
498 } 515 }
499 } 516 }
500 517
501 return TRUE; 518 return TRUE;
503 520
504 521
505 // 522 //
506 // Misc helper functions 523 // Misc helper functions
507 // 524 //
525
526 function wtNiceName($compo, $entry, $efile = FALSE)
527 {
528 return sprintf(
529 "%d: %s by %s%s",
530 $entry["id"],
531 $entry["name"],
532 $entry["author"],
533 ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : "");
534 }
535
536
508 function wtExec($exe, $args, $expect = 0) 537 function wtExec($exe, $args, $expect = 0)
509 { 538 {
510 echo "EXEC: ".$exe." @ ".$args."\n"; 539 echo "EXEC: ".$exe." @ ".$args."\n";
511 exec(escapeshellcmd($exe)." ".$args, $output, $code); 540 exec(escapeshellcmd($exe)." ".$args, $output, $code);
512 if ($code !== $expect) 541 if ($code !== $expect)