compress attachments

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
cas
Posts: 1622
Joined: 11 Mar 2006, 16:08
Contact:

compress attachments

Post by cas »

In case you would like to compress your attachments, there is a relative easy way to achieve this.
The positive side is that attachments take less storage on Disk or in Database and they are consuming less bandwidth on internet connections.
Downside is that the files that have been compressed, can only be viewed after downloading and not directly within Mantis itself.

Not yet available as plugin but it is pretty straight forward.

You need to add the following settings to config/config_inc.php:

Code: Select all

$g_zip_attachments		=	ON;
$g_zip_attachments_included_extensions		= "txt,csv";
$g_zip_attachments_minimum_size		= 1024;
$g_zip_attachments_minimum_compress_ratio 	= 40;
and next insert some code into core/file_api.php.
Simply find function file_attach_files, mostly the first function in that script.
Directly after this codeline

Code: Select all

if( !empty( $t_file['name'] ) ) {
insert the following code:

Code: Select all

## Zipfiles addition
$t_zip_attachments = config_get( 'zip_attachments' );
if ( $t_zip_attachments == 1 && extension_loaded( 'zip' ) ) {
	$t_zip_attachments_included_extensions = explode( ",", config_get( 'zip_attachments_included_extensions' ) );
	$t_zip_attachments_minimum_size = config_get( 'zip_attachments_minimum_size' ) ;
	$t_zip_attachments_minimum_compress_ratio = config_get( 'zip_attachments_minimum_compress_ratio' ) ;
	if ( $t_file['size'] > $t_zip_attachments_minimum_size ) {
		$t_extension = strtolower( pathinfo( $t_file['name'] , PATHINFO_EXTENSION ) );
		if ( in_array( $t_extension , $t_zip_attachments_included_extensions ) ) {
			$t_orgsize_attachment = filesize( $t_file['tmp_name'] );
			$t_new_filename  = $t_file['tmp_name'].".zip";
			$zip = new ZipArchive();
			$zip->open( $t_new_filename, ZIPARCHIVE::CREATE );
			$zip->addFile( $t_file['tmp_name'] );
			$zip->close();
			$t_zipsize_attachment = filesize( $t_new_filename );
			$t_actual_zip_ratio = ( $t_zipsize_attachment / $t_orgsize_attachment * 1000 );
			if ( $t_actual_zip_ratio >= $t_zip_attachments_minimum_compress_ratio ){
				$t_file['name'] 	.=".zip";
				$t_file['tmp_name'] .=".zip";
			}
		}
	}
}
## Zipfiles end
The configuration is very simple:
$g_zip_attachments = ON;
- Compression will be attempted, set to OFF to disable compression.
$g_zip_attachments_included_extensions = "txt,csv";
- A list of file extensions for which compression will be attempted.
$g_zip_attachments_minimum_size = 1024;
- Files with a size smaller than defined here, will not be compressed.
$g_zip_attachments_minimum_compress_ratio = 40;
- Only compress is the size reduction is above the defined threshold

This customization also works with the Attachment plugin enabled.
cas
Posts: 1622
Joined: 11 Mar 2006, 16:08
Contact:

Re: compress attachments

Post by cas »

Now available as plugin to be downloaded here:
https://github.com/mantisbt-plugins/ZipFiles
Post Reply