2013-08-16 21:42:24 +00:00
|
|
|
<?php
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
require_once "config.php";
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class CronUploader extends Extension
|
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
public const NAME = "cron_uploader";
|
|
|
|
|
|
|
|
// TODO: Checkbox option to only allow localhost + a list of additional IP addresses that can be set in /cron_upload
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-06-15 17:15:47 +00:00
|
|
|
const QUEUE_DIR = "queue";
|
|
|
|
const UPLOADED_DIR = "uploaded";
|
|
|
|
const FAILED_DIR = "failed_to_upload";
|
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public $output_buffer = [];
|
2019-06-15 17:15:47 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
// Set default values
|
|
|
|
CronUploaderConfig::set_defaults();
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
if($event->parent=="system") {
|
|
|
|
$event->add_nav_link("cron_docs", new Link('cron_upload'), "Cron Upload");
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Checks if the cron upload page has been accessed
|
|
|
|
* and initializes the upload.
|
|
|
|
*/
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
global $user;
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->page_matches("cron_upload")) {
|
2019-10-10 15:16:15 +00:00
|
|
|
$key = $event->get_arg(0);
|
|
|
|
if (!empty($key)) {
|
|
|
|
$this->process_upload($key); // Start upload
|
2019-10-17 19:22:33 +00:00
|
|
|
} elseif ($user->can(Permissions::CRON_ADMIN)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->display_documentation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
global $database;
|
|
|
|
|
|
|
|
$documentation_link = make_http(make_link("cron_upload"));
|
|
|
|
|
|
|
|
$users = $database->get_pairs("SELECT name, id FROM users UNION ALL SELECT '', null order by name");
|
|
|
|
|
|
|
|
$sb = new SetupBlock("Cron Uploader");
|
|
|
|
$sb->start_table();
|
|
|
|
$sb->add_int_option(CronUploaderConfig::COUNT, "Upload per run", true);
|
|
|
|
$sb->add_text_option(CronUploaderConfig::DIR, "Root dir", true);
|
|
|
|
$sb->add_text_option(CronUploaderConfig::KEY, "Key", true);
|
|
|
|
$sb->add_choice_option(CronUploaderConfig::USER, $users,"User", true);
|
|
|
|
$sb->end_table();
|
|
|
|
$sb->add_label("<a href='$documentation_link'>Read the documentation</a> for cron setup instructions.");
|
|
|
|
|
|
|
|
$event->panel->add_block($sb);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2013-08-16 21:42:24 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
$failed_dir = $this->get_failed_dir();
|
|
|
|
$results = get_dir_contents($failed_dir);
|
|
|
|
|
|
|
|
$failed_dirs = [];
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$path = join_path($failed_dir, $result);
|
|
|
|
if (is_dir($path)) {
|
|
|
|
$failed_dirs[] = $result;
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 16:35:36 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->theme->display_form($failed_dirs);
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
public function onAdminAction(AdminActionEvent $event)
|
|
|
|
{
|
|
|
|
$action = $event->action;
|
|
|
|
switch ($action) {
|
|
|
|
case "cron_uploader_clear_queue":
|
|
|
|
$event->redirect = true;
|
|
|
|
$this->clear_folder(self::QUEUE_DIR);
|
|
|
|
break;
|
|
|
|
case "cron_uploader_clear_uploaded":
|
|
|
|
$event->redirect = true;
|
|
|
|
$this->clear_folder(self::UPLOADED_DIR);
|
|
|
|
break;
|
|
|
|
case "cron_uploader_clear_failed":
|
|
|
|
$event->redirect = true;
|
|
|
|
$this->clear_folder(self::FAILED_DIR);
|
|
|
|
break;
|
|
|
|
case "cron_uploader_restage":
|
|
|
|
$event->redirect = true;
|
|
|
|
if (array_key_exists("failed_dir", $_POST) && !empty($_POST["failed_dir"])) {
|
|
|
|
$this->restage_folder($_POST["failed_dir"]);
|
|
|
|
}
|
|
|
|
break;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function restage_folder(string $folder)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
if (empty($folder)) {
|
|
|
|
throw new Exception("folder empty");
|
|
|
|
}
|
|
|
|
$queue_dir = $this->get_queue_dir();
|
|
|
|
$stage_dir = join_path($this->get_failed_dir(), $folder);
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
if (!is_dir($stage_dir)) {
|
|
|
|
throw new Exception("Could not find $stage_dir");
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->prep_root_dir();
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$results = get_dir_contents($queue_dir);
|
2013-08-16 22:51:57 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
if (count($results) > 0) {
|
|
|
|
flash_message("Queue folder must be empty to re-stage", "error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = get_dir_contents($stage_dir);
|
|
|
|
|
|
|
|
if (count($results) == 0) {
|
|
|
|
if(rmdir($stage_dir)===false) {
|
|
|
|
flash_message("Nothing to stage from $folder, cannot remove folder");
|
|
|
|
} else {
|
|
|
|
flash_message("Nothing to stage from $folder, removing folder");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$original_path = join_path($stage_dir, $result);
|
|
|
|
$new_path = join_path($queue_dir, $result);
|
|
|
|
|
|
|
|
rename($original_path, $new_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
flash_message("Re-staged $folder to queue");
|
|
|
|
rmdir($stage_dir);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function clear_folder($folder)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
$path = join_path(CronUploaderConfig::get_dir(), $folder);
|
|
|
|
deltree($path);
|
|
|
|
flash_message("Cleared $path");
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function get_cron_url()
|
|
|
|
{
|
|
|
|
return make_http(make_link("/cron_upload/" . CronUploaderConfig::get_key()));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function get_cron_cmd()
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
return "curl --silent " . $this->get_cron_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function display_documentation()
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$this->prep_root_dir();
|
|
|
|
|
|
|
|
$queue_dir = $this->get_queue_dir();
|
|
|
|
$uploaded_dir = $this->get_uploaded_dir();
|
|
|
|
$failed_dir = $this->get_failed_dir();
|
|
|
|
|
|
|
|
$queue_dirinfo = scan_dir($queue_dir);
|
|
|
|
$uploaded_dirinfo = scan_dir($uploaded_dir);
|
|
|
|
$failed_dirinfo = scan_dir($failed_dir);
|
2019-06-15 16:01:13 +00:00
|
|
|
|
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$running = false;
|
|
|
|
$lockfile = fopen($this->get_lock_file(), "w");
|
|
|
|
try {
|
|
|
|
if (!flock($lockfile, LOCK_EX | LOCK_NB)) {
|
|
|
|
$running = true;
|
|
|
|
} else {
|
|
|
|
flock($lockfile, LOCK_UN);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
fclose($lockfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
$logs = [];
|
|
|
|
if (Extension::is_enabled(LogDatabaseInfo::KEY)) {
|
|
|
|
$logs = $database->get_all(
|
|
|
|
"SELECT * FROM score_log WHERE section = :section ORDER BY date_sent DESC LIMIT 100",
|
|
|
|
["section" => self::NAME]
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->theme->display_documentation(
|
|
|
|
$running, $queue_dirinfo, $uploaded_dirinfo, $failed_dirinfo,
|
|
|
|
$this->get_cron_cmd(), $this->get_cron_url(), $logs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_queue_dir()
|
|
|
|
{
|
|
|
|
$dir = CronUploaderConfig::get_dir();
|
|
|
|
return join_path($dir, self::QUEUE_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_uploaded_dir()
|
|
|
|
{
|
|
|
|
$dir = CronUploaderConfig::get_dir();
|
|
|
|
return join_path($dir, self::UPLOADED_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_failed_dir()
|
|
|
|
{
|
|
|
|
$dir = CronUploaderConfig::get_dir();
|
|
|
|
return join_path($dir, self::FAILED_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function prep_root_dir(): string
|
|
|
|
{
|
|
|
|
// Determine directory (none = default)
|
|
|
|
$dir = CronUploaderConfig::get_dir();
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Make the directory if it doesn't exist yet
|
2019-10-10 15:16:15 +00:00
|
|
|
if (!is_dir($this->get_queue_dir())) {
|
|
|
|
mkdir($this->get_queue_dir(), 0775, true);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
if (!is_dir($this->get_uploaded_dir())) {
|
|
|
|
mkdir($this->get_uploaded_dir(), 0775, true);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
if (!is_dir($this->get_failed_dir())) {
|
|
|
|
mkdir($this->get_failed_dir(), 0775, true);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
return $dir;
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function get_lock_file(): string
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
$root_dir = CronUploaderConfig::get_dir();
|
|
|
|
return join_path($root_dir, ".lock");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Uploads the image & handles everything
|
|
|
|
*/
|
2019-10-10 15:16:15 +00:00
|
|
|
public function process_upload(string $key, ?int $upload_count = null): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
global $database;
|
2019-06-12 22:50:00 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
if ($key!=CronUploaderConfig::get_key()) {
|
|
|
|
throw new SCoreException("Cron upload key incorrect");
|
|
|
|
}
|
|
|
|
$user_id = CronUploaderConfig::get_user();
|
|
|
|
if(empty($user_id)) {
|
|
|
|
throw new SCoreException("Cron upload user not set");
|
|
|
|
}
|
|
|
|
$user = User::by_id($user_id);
|
|
|
|
if ($user == null) {
|
|
|
|
throw new SCoreException("No user found for cron upload user $user_id");
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
send_event(new UserLoginEvent($user));
|
|
|
|
$this->log_message(SCORE_LOG_INFO, "Logged in as user {$user->name}");
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$lockfile = fopen($this->get_lock_file(), "w");
|
|
|
|
if (!flock($lockfile, LOCK_EX | LOCK_NB)) {
|
|
|
|
throw new SCoreException("Cron upload process is already running");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
try {
|
|
|
|
//set_time_limit(0);
|
|
|
|
|
|
|
|
// Gets amount of imgs to upload
|
|
|
|
if ($upload_count == null) {
|
|
|
|
$upload_count = CronUploaderConfig::get_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
$output_subdir = date('Ymd-His', time());
|
|
|
|
$image_queue = $this->generate_image_queue($upload_count);
|
|
|
|
|
|
|
|
|
|
|
|
// Throw exception if there's nothing in the queue
|
|
|
|
if (count($image_queue) == 0) {
|
|
|
|
$this->log_message(SCORE_LOG_WARNING, "Your queue is empty so nothing could be uploaded.");
|
|
|
|
$this->handle_log();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Randomize Images
|
|
|
|
//shuffle($this->image_queue);
|
|
|
|
|
|
|
|
$merged = 0;
|
|
|
|
$added = 0;
|
|
|
|
$failed = 0;
|
|
|
|
|
|
|
|
// Upload the file(s)
|
|
|
|
for ($i = 0; $i < $upload_count && sizeof($image_queue) > 0; $i++) {
|
|
|
|
$img = array_pop($image_queue);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$database->beginTransaction();
|
|
|
|
$this->log_message(SCORE_LOG_INFO, "Adding file: {$img[0]} - tags: {$img[2]}");
|
|
|
|
$result = $this->add_image($img[0], $img[1], $img[2]);
|
|
|
|
$database->commit();
|
|
|
|
$this->move_uploaded($img[0], $img[1], $output_subdir, false);
|
|
|
|
if ($result->merged) {
|
|
|
|
$merged++;
|
|
|
|
} else {
|
|
|
|
$added++;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
try {
|
|
|
|
$database->rollback();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$failed++;
|
|
|
|
$this->move_uploaded($img[0], $img[1], $output_subdir, true);
|
|
|
|
$this->log_message(SCORE_LOG_ERROR, "(" . gettype($e) . ") " . $e->getMessage());
|
|
|
|
$this->log_message(SCORE_LOG_ERROR, $e->getTraceAsString());
|
|
|
|
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-06-12 22:50:00 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-12 22:50:00 +00:00
|
|
|
|
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->log_message(SCORE_LOG_INFO, "Items added: $added");
|
|
|
|
$this->log_message(SCORE_LOG_INFO, "Items merged: $merged");
|
|
|
|
$this->log_message(SCORE_LOG_INFO, "Items failed: $failed");
|
|
|
|
|
|
|
|
|
|
|
|
// Display upload log
|
|
|
|
$this->handle_log();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} finally {
|
|
|
|
flock($lockfile, LOCK_UN);
|
|
|
|
fclose($lockfile);
|
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private function move_uploaded(string $path, string $filename, string $output_subdir, bool $corrupt = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
$relativeDir = dirname(substr($path, strlen(CronUploaderConfig::get_dir()) + 7));
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
if($relativeDir==".") {
|
|
|
|
$relativeDir = "";
|
|
|
|
}
|
2019-06-02 18:34:24 +00:00
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
// Determine which dir to move to
|
|
|
|
if ($corrupt) {
|
|
|
|
// Move to corrupt dir
|
2019-10-10 15:16:15 +00:00
|
|
|
$newDir = join_path($this->get_failed_dir(), $output_subdir, $relativeDir);
|
|
|
|
$info = "ERROR: Image was not uploaded. ";
|
2019-06-14 12:47:50 +00:00
|
|
|
} else {
|
2019-10-10 15:16:15 +00:00
|
|
|
$newDir = join_path($this->get_uploaded_dir(), $output_subdir, $relativeDir);
|
2019-06-14 12:47:50 +00:00
|
|
|
$info = "Image successfully uploaded. ";
|
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
$newDir = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $newDir);
|
2019-06-02 18:34:24 +00:00
|
|
|
|
2019-06-14 12:16:58 +00:00
|
|
|
if (!is_dir($newDir)) {
|
|
|
|
mkdir($newDir, 0775, true);
|
|
|
|
}
|
2019-06-02 18:34:24 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$newFile = join_path($newDir, $filename);
|
2019-05-28 16:59:38 +00:00
|
|
|
// move file to correct dir
|
2019-10-10 15:16:15 +00:00
|
|
|
rename($path, $newFile);
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->log_message(SCORE_LOG_INFO, $info . "Image \"$filename\" moved from queue to \"$newDir\".");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2013-08-16 21:42:24 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Generate the necessary DataUploadEvent for a given image and tags.
|
|
|
|
*/
|
2019-06-20 01:14:19 +00:00
|
|
|
private function add_image(string $tmpname, string $filename, string $tags): DataUploadEvent
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
assert(file_exists($tmpname));
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-06-20 01:14:19 +00:00
|
|
|
$tagArray = Tag::explode($tags);
|
2019-10-10 15:16:15 +00:00
|
|
|
if (count($tagArray) == 0) {
|
2019-06-20 01:14:19 +00:00
|
|
|
$tagArray[] = "tagme";
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$pathinfo = pathinfo($filename);
|
|
|
|
$metadata = [];
|
|
|
|
$metadata ['filename'] = $pathinfo ['basename'];
|
2019-06-14 12:16:58 +00:00
|
|
|
if (array_key_exists('extension', $pathinfo)) {
|
2019-06-12 22:50:00 +00:00
|
|
|
$metadata ['extension'] = $pathinfo ['extension'];
|
2019-06-14 12:16:58 +00:00
|
|
|
}
|
2019-06-20 01:14:19 +00:00
|
|
|
$metadata ['tags'] = $tagArray; // doesn't work when not logged in here, handled below
|
2019-05-28 16:59:38 +00:00
|
|
|
$metadata ['source'] = null;
|
|
|
|
$event = new DataUploadEvent($tmpname, $metadata);
|
|
|
|
send_event($event);
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Generate info message
|
|
|
|
$infomsg = ""; // Will contain info message
|
|
|
|
if ($event->image_id == -1) {
|
2019-06-09 19:17:13 +00:00
|
|
|
throw new Exception("File type not recognised. Filename: {$filename}");
|
2019-06-20 00:40:25 +00:00
|
|
|
} elseif ($event->merged === true) {
|
2019-10-10 15:16:15 +00:00
|
|
|
$infomsg = "Image merged. ID: {$event->image_id} - Filename: {$filename}";
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2019-06-12 22:50:00 +00:00
|
|
|
$infomsg = "Image uploaded. ID: {$event->image_id} - Filename: {$filename}";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->log_message(SCORE_LOG_INFO, $infomsg);
|
2019-06-19 23:59:18 +00:00
|
|
|
|
|
|
|
// Set tags
|
|
|
|
$img = Image::by_id($event->image_id);
|
2019-06-20 01:14:19 +00:00
|
|
|
$img->set_tags(array_merge($tagArray, $img->get_tag_array()));
|
2019-06-19 23:59:18 +00:00
|
|
|
|
2019-06-20 01:14:19 +00:00
|
|
|
return $event;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
private const PARTIAL_DOWNLOAD_EXTENSIONS = ['crdownload','part'];
|
|
|
|
|
|
|
|
private function is_skippable_file(string $path) {
|
|
|
|
$info = pathinfo($path);
|
|
|
|
|
|
|
|
if(in_array(strtolower($info['extension']),self::PARTIAL_DOWNLOAD_EXTENSIONS)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generate_image_queue(string $root_dir, ?int $limit = null): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
$base = $this->get_queue_dir();
|
|
|
|
$output = [];
|
2019-06-15 16:01:13 +00:00
|
|
|
|
|
|
|
if (!is_dir($base)) {
|
2019-10-10 15:16:15 +00:00
|
|
|
$this->log_message(SCORE_LOG_WARNING, "Image Queue Directory could not be found at \"$base\".");
|
|
|
|
return [];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
|
|
|
$ite = new RecursiveDirectoryIterator($base, FilesystemIterator::SKIP_DOTS);
|
|
|
|
foreach (new RecursiveIteratorIterator($ite) as $fullpath => $cur) {
|
2019-10-10 15:16:15 +00:00
|
|
|
if (!is_link($fullpath) && !is_dir($fullpath) && !$this->is_skippable_file($fullpath)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$pathinfo = pathinfo($fullpath);
|
2019-06-01 17:12:36 +00:00
|
|
|
|
2019-06-14 12:16:58 +00:00
|
|
|
$relativePath = substr($fullpath, strlen($base));
|
2019-06-01 17:12:36 +00:00
|
|
|
$tags = path_to_tags($relativePath);
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$img = [
|
2019-06-15 16:01:13 +00:00
|
|
|
0 => $fullpath,
|
|
|
|
1 => $pathinfo ["basename"],
|
|
|
|
2 => $tags
|
2019-05-28 16:59:38 +00:00
|
|
|
];
|
2019-10-10 15:16:15 +00:00
|
|
|
$output[] = $img;
|
|
|
|
if (!empty($limit) && count($output) >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
return $output;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
|
|
|
|
private function log_message(int $severity, string $message): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-10 15:16:15 +00:00
|
|
|
global $database;
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
log_msg(self::NAME, $severity, $message);
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$time = "[" . date('Y-m-d H:i:s') . "]";
|
|
|
|
$this->output_buffer[] = $time . " " . $message;
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
$log_path = $this->get_log_file();
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-10-10 15:16:15 +00:00
|
|
|
file_put_contents($log_path, $time . " " . $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_log_file(): string
|
|
|
|
{
|
|
|
|
return join_path(CronUploaderConfig::get_dir(), "uploads.log");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* This is run at the end to display & save the log.
|
|
|
|
*/
|
|
|
|
private function handle_log()
|
|
|
|
{
|
|
|
|
global $page;
|
2019-06-15 16:01:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Display message
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_type("text/plain");
|
2019-10-10 15:16:15 +00:00
|
|
|
$page->set_data(implode("\r\n", $this->output_buffer));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2013-08-16 21:42:24 +00:00
|
|
|
}
|
2019-10-10 15:16:15 +00:00
|
|
|
|