2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2007-12-08 03:10:07 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-02-23 19:41:20 +00:00
|
|
|
class ArchiveFileHandler extends DataHandlerExtension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
protected array $SUPPORTED_MIME = [MimeType::ZIP];
|
2020-02-23 19:41:20 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string('archive_extract_command', 'unzip -d "%d" "%f"');
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Archive Handler Options");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_text_option("archive_tmp_dir", "Temporary folder: ");
|
|
|
|
$sb->add_text_option("archive_extract_command", "<br>Extraction command: ");
|
|
|
|
$sb->add_label("<br>%f for archive, %d for temporary directory");
|
|
|
|
}
|
2007-12-08 17:13:15 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-06-14 16:05:55 +00:00
|
|
|
if ($this->supported_mime($event->mime)) {
|
2020-01-26 13:19:35 +00:00
|
|
|
global $config, $page;
|
2024-02-21 21:31:20 +00:00
|
|
|
$tmpdir = shm_tempnam("archive");
|
|
|
|
unlink($tmpdir);
|
|
|
|
mkdir($tmpdir, 0755, true);
|
2019-05-28 16:59:38 +00:00
|
|
|
$cmd = $config->get_string('archive_extract_command');
|
|
|
|
$cmd = str_replace('%f', $event->tmpname, $cmd);
|
|
|
|
$cmd = str_replace('%d', $tmpdir, $cmd);
|
2024-01-20 20:48:47 +00:00
|
|
|
assert(is_string($cmd));
|
2019-05-28 16:59:38 +00:00
|
|
|
exec($cmd);
|
2020-06-24 16:02:24 +00:00
|
|
|
if (file_exists($tmpdir)) {
|
2020-06-24 14:17:44 +00:00
|
|
|
try {
|
2024-02-20 21:28:14 +00:00
|
|
|
$results = add_dir($tmpdir, Tag::explode($event->metadata['tags']));
|
2024-01-05 13:53:21 +00:00
|
|
|
foreach ($results as $r) {
|
2024-08-31 16:05:18 +00:00
|
|
|
if (is_a($r, UploadError::class)) {
|
2024-01-05 13:53:21 +00:00
|
|
|
$page->flash($r->name." failed: ".$r->error);
|
|
|
|
}
|
2024-08-31 16:05:18 +00:00
|
|
|
if (is_a($r, UploadSuccess::class)) {
|
2024-02-20 00:22:25 +00:00
|
|
|
$event->images[] = Image::by_id_ex($r->image_id);
|
2024-01-05 13:53:21 +00:00
|
|
|
}
|
2020-06-24 14:17:44 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
deltree($tmpdir);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-08 03:10:07 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event): void
|
2020-02-25 12:18:47 +00:00
|
|
|
{
|
|
|
|
}
|
2020-02-23 19:41:20 +00:00
|
|
|
|
|
|
|
// we don't actually do anything, just accept one upload and spawn several
|
2020-02-25 12:18:47 +00:00
|
|
|
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function check_contents(string $tmpname): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:49:19 +00:00
|
|
|
protected function create_thumb(Image $image): bool
|
2020-02-25 12:18:47 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-08 03:10:07 +00:00
|
|
|
}
|