This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/handle_archive/main.php

74 lines
2.3 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class ArchiveFileHandler extends DataHandlerExtension
{
protected array $SUPPORTED_MIME = [MimeType::ZIP];
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_string('archive_extract_command', 'unzip -d "%d" "%f"');
}
2009-01-04 19:18:37 +00:00
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Archive Handler Options");
$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");
}
public function onDataUpload(DataUploadEvent $event): void
{
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);
$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));
exec($cmd);
2020-06-24 16:02:24 +00:00
if (file_exists($tmpdir)) {
2020-06-24 14:17:44 +00:00
try {
$results = add_dir($tmpdir, Tag::explode($event->metadata['tags']));
foreach ($results as $r) {
2024-08-31 16:05:18 +00:00
if (is_a($r, UploadError::class)) {
$page->flash($r->name." failed: ".$r->error);
}
2024-08-31 16:05:18 +00:00
if (is_a($r, UploadSuccess::class)) {
$event->images[] = Image::by_id_ex($r->image_id);
}
2020-06-24 14:17:44 +00:00
}
} finally {
deltree($tmpdir);
}
}
}
}
public function onDisplayingImage(DisplayingImageEvent $event): void
2020-02-25 12:18:47 +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;
}
protected function create_thumb(Image $image): bool
2020-02-25 12:18:47 +00:00
{
return false;
}
}