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 Flight::set("organizer.settings.path", "config/settings.json"); Flight::map("read_settings", function () { if (($settings = file_get_contents(Flight::get("organizer.settings.path"))) !== false) { return json_decode($settings, true); } else { return false; } }); Flight::map("load_settings", function () { $settings = Flight::read_settings(); Flight::set("organizer.settings", $settings); return $settings; }); Flight::map("setting", function ($path) { $settings = Flight::get("organizer.settings"); foreach (explode(".", $path) as $key) { $settings = $settings[$key]; } return $settings; }); Flight::map("write_settings", function ($settings) { $settings = json_encode($settings); return file_put_contents(Flight::get("organizer.settings.path"), $settings); }); Flight::map("dump_settings", function () { return Flight::write_settings(Flight::get("organizer.settings")); });
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"); Flight::render("layout"); }); Flight::route("POST /settings", function () { if (($settings = Flight::get("organizer.settings")) == null) { $settings = array("paths" => array()); } $settings["from"]["folders"] = explode("\n", trim($_POST["from"]["folders"])); $settings["from"]["pattern"] = $_POST["from"]["pattern"]; $settings["to"]["movies"] = rtrim(trim($_POST["to"]["movies"]), "/"); $settings["to"]["tv"] = rtrim(trim($_POST["to"]["tv"]), "/"); $result = Flight::write_settings($settings); if ($result !== false) { $settings = Flight::load_settings(); Flight::render("_success", array("message" => "Changes saved."), "success"); } else { Flight::render("_error", array("message" => "Changes could not be saved."), "error"); } Flight::render("settings", array("settings" => $settings), "content"); Flight::render("layout"); }); Flight::start();
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; }