@kraih /GZip.pm
Created 4 years ago

package Mojolicious::Plugin::GZip;
use Mojo::Base 'Mojolicious::Plugin';
use IO::Compress::Gzip 'gzip';
# Just set "gzip => 1" in the stash and it will try to compress your content
sub register {
my ($self, $app) = @_;
$app->hook(
after_dispatch => sub {
my $self = shift;
# Check if GZip compression is desirable
return unless $self->stash->{gzip};
return unless ($self->req->headers->accept_encoding // '') =~ /gzip/i;
my $res = $self->res;
my $asset = $res->content->asset;
return if $asset->is_file || !$asset->size;
return if $asset->start_range || $asset->end_range;
# Compress content
gzip \(my $dummy = $asset->slurp), \my $compressed;
$res->body($compressed);
$res->headers->content_encoding('gzip');
}
);
}
1;
@augensalat

Bad for big files, because both, original and compressed file are loaded into RAM.

@augensalat

oh, didn't spot the
return unless !$asset->is_file
so it's probably only for small files anyway

nm

@augensalat

...much better... ;-)

@mche

Attach files by dragging & dropping, selecting them, or pasting from the clipboard. Uploading your files… We don’t support that file type. with a PNG, GIF, JPG, DOCX, PPTX, XLSX, TXT, PDF, or ZIP. Attaching documents requires write permission to this repository. with a PNG, GIF, or JPG. We don’t support that file type. with a PNG, GIF, or JPG. Yowza, that’s a big file. with a file smaller than 10MB. This file is empty. with a file that’s not empty. This file is hidden. with another file. Something went really wrong, and we can’t process that file.

Nothing to preview