aerni / zipper
Zip your Statamic assets on the fly
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
This addon provides a simple way to zip your Statamic assets on the fly.
Installation
Install the addon using Composer:
composer require aerni/zipper
Publish the config of the package (optional):
php please vendor:publish --tag=zipper-config
Basic Usage
To create a zip of your assets, you must call the zip
tag followed by the variable
containing your assets. The tag returns the URL to the route that handles creating the zip. The zip will be streamed without being saved to disk, but you may opt-in to save the file to disk for later use.
Somewhere in your content files:
images: - sega-genesis.jpg - snes.jpg
Somewhere in your views:
{{ zip:images }}
Filename
You may optionally pass a filename using the filename
parameter. The filename defaults to the current timestamp when the Zip object is created. The example below binds the zip name to the page title.
{{ zip:images :filename="title" }}
Link Expiry
If you want to expire your links after a certain time, you can either set the expiry globally in the config or use the expiry
parameter on the tag. The expiry is to be set in minutes. Note that the expiry on the tag will override the expiry in the config.
{{ zip:images expiry="60" }}
Cleanup Old References
Zipper saves an encrypted instance of the Zip class every time it returns a URL. This class is later retrieved and decrypted when a user downloads a zip. These reference files are stored in storage/zipper/{id}
.
With time, the number of saved reference files will grow. To control this, Zipper provides a scheduled command that will delete old reference files daily. Just make sure that your Scheduler is running.
Cleanup Scopes
There are a couple of cleanup scopes to choose from in the config.:
Clean Command
You may also use the clean
command to delete reference files at your will. The scope defaults to expired
.
php please zipper:clean php please zipper:clean --scope=all php please zipper:clean --scope=force
Advanced Usage
You may also use this addon programmatically, as shown below.
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();