Added bulk download extension

This commit is contained in:
Matthew Barbour 2020-06-21 19:17:58 -05:00 committed by Shish
parent ac63992efa
commit 72de50aa71
4 changed files with 95 additions and 0 deletions

View file

@ -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";
}

View file

@ -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,

View file

@ -0,0 +1,14 @@
<?php declare(strict_types=1);
class BulkDownloadInfo extends ExtensionInfo
{
public const KEY = "bulk_download";
public $key = self::KEY;
public $name = "Bulk Download";
public $authors = ["Matthew Barbour"=>"matthew@darkholme.net"];
public $license = self::LICENSE_WTFPL;
public $description = "Allows bulk downloading images.";
public $dependencies = [BulkActionsInfo::KEY];
}

View file

@ -0,0 +1,78 @@
<?php declare(strict_types=1);
class BulkDownloadConfig
{
public const SIZE_LIMIT = "bulk_download_size_limit";
}
class BulkDownloadException extends BulkActionException
{
}
class BulkDownload extends Extension
{
private const DOWNLOAD_ACTION_NAME = "bulk_download";
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->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;
}
}
}
}