BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate.class.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. class Validate
  3. {
  4. # Line 171
  5. /**
  6. * Extension Parser
  7. *
  8. * Takes an associative array of file types and extension, e.g.,
  9. * $Archives = [
  10. * '7z' => ['7z'],
  11. * 'bzip2' => ['bz2', 'bzip2'],
  12. * 'gzip' => ['gz', 'gzip', 'tgz', 'tpz'],
  13. * ...
  14. * ];
  15. *
  16. * Then it finds all the extensions in a torrent file list,
  17. * organizes them by file size, and returns the heaviest match.
  18. *
  19. * That way, you can have, e.g., 5 GiB FASTQ sequence data in one file,
  20. * and 100 other small files, and get the format of the actual data.
  21. *
  22. * todo: Incorporate into $this->ValidateForm (remove if statements first)
  23. */
  24. public function ParseExtensions($FileList, $Category, $FileTypes)
  25. {
  26. # Sort $Tor->file_list() output by size
  27. $UnNested = array_values($FileList[1]);
  28. $Sorted = (usort($UnNested, function ($a, $b) {
  29. return $b <=> $a;
  30. })) ? $UnNested : null; # Ternary wrap because ↑ returns true
  31. # Harvest the wheat
  32. # todo: Entries seem duplicated here
  33. $Heaviest = array_slice($Sorted, 0, 20);
  34. $Matches = [];
  35. foreach ($Heaviest as $Heaviest) {
  36. # Collect the last 2 period-separated tokens
  37. $Extensions = array_slice(explode('.', strtolower($Heaviest[1])), -2, 2);
  38. $Matches = array_merge($Extensions);
  39. # Distill the file format
  40. $FileTypes = $FileTypes[$Category];
  41. $FileTypeNames = array_keys($FileTypes);
  42. # todo: Find the smallest possible number of iterations
  43. # todo: Reduce nesting by one level
  44. foreach ($Matches as $Match) {
  45. $Match = strtolower($Match);
  46. foreach ($FileTypeNames as $FileTypeName) {
  47. $SearchMe = [ $FileTypeName, $FileTypes[$FileTypeName] ];
  48. if (in_array($Match, $SearchMe[1])) {
  49. return $SearchMe[0];
  50. break;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. # Line 227