diff --git a/core/permissions.php b/core/permissions.php index ed928380..21429ae7 100644 --- a/core/permissions.php +++ b/core/permissions.php @@ -102,4 +102,5 @@ abstract class Permissions public const BULK_IMPORT = "bulk_import"; public const BULK_EXPORT = "bulk_export"; + public const BULK_DOWNLOAD = "bulk_download"; } diff --git a/core/userclass.php b/core/userclass.php index 39d78667..a24c879e 100644 --- a/core/userclass.php +++ b/core/userclass.php @@ -99,6 +99,7 @@ new UserClass("user", "base", [ Permissions::SEND_PM => true, Permissions::READ_PM => true, Permissions::SET_PRIVATE_IMAGE => true, + Permissions::BULK_DOWNLOAD => true, ]); new UserClass("hellbanned", "user", [ @@ -201,6 +202,7 @@ new UserClass("admin", "base", [ Permissions::BULK_IMPORT =>true, Permissions::BULK_EXPORT =>true, + Permissions::BULK_DOWNLOAD => true, Permissions::SET_PRIVATE_IMAGE => true, Permissions::SET_OTHERS_PRIVATE_IMAGES => true, diff --git a/ext/bulk_download/info.php b/ext/bulk_download/info.php new file mode 100644 index 00000000..bb031282 --- /dev/null +++ b/ext/bulk_download/info.php @@ -0,0 +1,14 @@ +"matthew@darkholme.net"]; + public $license = self::LICENSE_WTFPL; + public $description = "Allows bulk downloading images."; + public $dependencies = [BulkActionsInfo::KEY]; +} diff --git a/ext/bulk_download/main.php b/ext/bulk_download/main.php new file mode 100644 index 00000000..547e2aff --- /dev/null +++ b/ext/bulk_download/main.php @@ -0,0 +1,78 @@ +set_default_int(BulkDownloadConfig::SIZE_LIMIT, parse_shorthand_int('100MB')); + } + + public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event) + { + global $user, $config; + + if ($user->can(Permissions::BULK_DOWNLOAD)) { + $event->add_action(BulkDownload::DOWNLOAD_ACTION_NAME, "Download ZIP"); + } + } + + public function onSetupBuilding(SetupBuildingEvent $event) + { + $sb = new SetupBlock("Bulk Download"); + + $sb->start_table(); + $sb->add_shorthand_int_option(BulkDownloadConfig::SIZE_LIMIT, "Size Limit", true); + $sb->end_table(); + + $event->panel->add_block($sb); + } + + public function onBulkAction(BulkActionEvent $event) + { + global $user, $page, $config; + + if ($user->can(Permissions::BULK_DOWNLOAD)&& + ($event->action == BulkDownload::DOWNLOAD_ACTION_NAME)) { + $download_filename = $user->name . '-' . date('YmdHis') . '.zip'; + $zip_filename = tempnam(sys_get_temp_dir(), "shimmie_bulk_download"); + $zip = new ZipArchive; + $size_total = 0; + $max_size = $config->get_int(BulkDownloadConfig::SIZE_LIMIT); + + if ($zip->open($zip_filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === true) { + foreach ($event->items as $image) { + $img_loc = warehouse_path(Image::IMAGE_DIR, $image->hash, false); + $size_total += filesize($img_loc); + if ($size_total>$max_size) { + throw new BulkDownloadException("Bulk download limited to ".human_filesize($max_size)); + } + + + $filename = urldecode($image->get_nice_image_name()); + $filename = str_replace(":", ";", $filename); + $zip->addFile($img_loc, $filename); + } + + $zip->close(); + + $page->set_mode(PageMode::FILE); + $page->set_file($zip_filename, true); + $page->set_filename($download_filename); + + $event->redirect = false; + } + } + } +}