public function __construct($paths, $pattern) { $this->paths = $paths; $this->pattern = $pattern; $this->ignores = $this->update_ignores(\Flight::setting("ignores")); $settings = \Flight::get("organizer.settings"); $settings["ignores"] = $this->ignores; \Flight::write_settings($settings); }
<?php require "lib/flight/Flight.php"; require "lib/organizer/settings.php"; Flight::load_settings(); require "lib/organizer/explorer.php"; Flight::register("explorer", "organizer\\Explorer", array(Flight::setting("from.folders"), Flight::setting("from.pattern"))); Flight::route("/", function () { Flight::render("index", array(), "content"); Flight::render("layout"); }); Flight::route("POST /", function () { require "lib/organizer/organizer.php"; $files = array(); for ($i = 0; $i < count($_POST["paths"]); $i++) { $files[$_POST["paths"][$i]] = array("name" => $_POST["names"][$i], "type" => $_POST["types"][$i]); } $settings = Flight::get("organizer.settings"); $organizer = new organizer\Organizer($settings); $result = $organizer->organize($files); header("Content-type: application/json"); echo json_encode($result); }); Flight::route("/entries", function () { Flight::render("_entries", array("paths" => Flight::explorer()->get_entries())); }); Flight::route("/settings", function () { if (($settings = Flight::get("organizer.settings")) == null) { $settings = array("paths" => array()); } Flight::render("settings", array("settings" => $settings), "content");
public function organize($paths) { $result = array(); if (($ignores = \Flight::setting("ignores")) == null) { $ignores = array(); } /* * Iterate over each path and attempt to move it to its new location, * based on the settings this Organizer was initialized with. */ foreach ($paths as $path => $options) { // Make sure the original file exists and is able to be (re)moved if (!is_writable($path)) { $result[$path] = array("status" => "error", "error" => "Original file does not exist or is not writable"); continue; } // Validate and act on media type if ($options["type"] === "movie") { $options["type"] = "movies"; } if (isset($this->settings["to"][$options["type"]])) { $dest_dir = $this->settings["to"][$options["type"]]; } else { if ($options["type"] === "delete") { if (unlink($path)) { $result[$path] = array("status" => "success", "deleted" => TRUE); continue; } else { $result[$path] = array("status" => "error", "error" => "Original file could not be deleted", "deleted" => FALSE); continue; } } else { if ($options["type"] === "ignore") { $ignores[$path] = time(); $result[$path] = array("status" => "success", "ignored" => TRUE); continue; } else { $result[$path] = array("status" => "error", "error" => "Media type is unrecognized", "type" => $options["type"]); continue; } } } // Construct destination directory $pathinfo = pathinfo($options["name"]); if ($options["type"] === "movies") { $dest_dir .= "/" . $pathinfo["filename"]; } else { if ($options["type"] === "tv") { if (preg_match("/^\\s*(.*)\\s*-\\s*S(\\d+)E\\d+/", $options["name"], $matches)) { $show = trim($matches[1]); $season = intval($matches[2]); $dest_dir .= "/" . $show . "/Season " . $season; } else { $result[$path] = array("status" => "error", "error" => "TV episode name is formatted incorrectly"); continue; } } } mkdir($dest_dir, 0755, TRUE); // May return FALSE if directory already exists if (!is_writable($dest_dir)) { $result[$path] = array("status" => "error", "error" => "Destination folder cannot be written to", "path" => $dest_dir); continue; } // Construct destination path $dest_path = $dest_dir . "/" . $options["name"]; if (file_exists($dest_path)) { $result[$path] = array("status" => "error", "error" => "Destination file already exists", "path" => $dest_path); continue; } // Attempt to move original file to destination if (rename($path, $dest_path)) { $result[$path] = array("status" => "success", "path" => $dest_path); } else { $result[$path] = array("status" => "error", "error" => "Could not move original file to destination", "path" => $dest_path); } } // Write settings with any new ignores added $settings = \Flight::get("organizer.settings"); $settings["ignores"] = $ignores; \Flight::write_settings($settings); return $result; }