aerni / zipper
动态压缩 Statamic 资产
v2.4.0
2024-08-13 19:09 UTC
Requires
- php: ^8.2
- laravel/framework: ^10.0 || ^11.0
- statamic/cms: ^5.0
- stechstudio/laravel-zipstream: ^5.0
Requires (Dev)
- nunomaduro/collision: ^7.0 || ^8.0
- orchestra/testbench: ^8.0 || ^9.0
- phpunit/phpunit: ^10.0
README
Zipper
此插件提供了一种简单的动态压缩 Statamic 资产的方法。
安装
使用 Composer 安装插件
composer require aerni/zipper
发布包的配置(可选)
php please vendor:publish --tag=zipper-config
基本用法
要创建资产的压缩包,您必须调用 zip
标签后跟包含您的资产的 variable
。此标签返回处理创建压缩包的路由的 URL。压缩包将以流的形式传输,而不会保存到磁盘,但您可以选择将文件保存到磁盘以供以后使用。
在您的内容文件中的某个地方
images: - sega-genesis.jpg - snes.jpg
在您的视图中的某个地方
{{ zip:images }}
文件名
您可以使用 filename
参数可选地传递文件名。默认情况下,文件名在创建 Zip 对象时使用当前时间戳。下面的示例将压缩包名称绑定到页面标题。
{{ zip:images :filename="title" }}
链接过期
如果您想在特定时间后使链接过期,您可以在配置中全局设置过期时间,或在标签上使用 expiry
参数。过期时间以分钟为单位。请注意,标签上的过期时间将覆盖配置中的过期时间。
{{ zip:images expiry="60" }}
清理旧引用
Zipper 每次返回 URL 时都会保存 Zip 类的加密实例。在用户下载压缩包时,这些类会被检索并解密。这些引用文件存储在 storage/zipper/{id}
中。
随着时间的推移,保存的引用文件数量会增加。为了控制这一点,Zipper 提供了一个计划任务,每天删除旧的引用文件。只需确保您的调度器正在运行即可。
清理范围
在配置中可以选择一些清理范围。
清理命令
您还可以使用 clean
命令随意删除引用文件。默认范围是 expired
。
php please zipper:clean php please zipper:clean --scope=all php please zipper:clean --scope=force
高级用法
您还可以像下面这样以编程方式使用此插件。
use Aerni\Zipper\Zip; // Prepare an array of Statamic assets, paths, or URLs. $files = [ Statamic\Assets\Asset, '/home/ploi/site.com/storage/app/assets/file_1.jpg', 'https://site.com/path/to/file_2.jpg', ]; // Make a zip with the files above. $zip = Zip::make($files); // Set an optional filename. This defaults to the timestamp when the object was created. $zip->filename('obi-wan-kenobi'); // Set an optional expiry time in minutes. This defaults to the expiry set in the config. $zip->expiry(60); // Get the URL that handles creating the zip. $zip->url(); // Create a new zip or download a previously cached zip. $zip->get();