diff --git a/Blacklight/Releases.php b/Blacklight/Releases.php index 896e90573..9998af2f9 100755 --- a/Blacklight/Releases.php +++ b/Blacklight/Releases.php @@ -7,8 +7,10 @@ use App\Models\Release; use App\Models\Category; use App\Models\Settings; use Blacklight\utility\Utility; +use Chumper\Zipper\Zipper; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\File; /** * Class Releases. @@ -1246,13 +1248,16 @@ class Releases /** * @param array $guids + * * @return string * @throws \Exception */ - public function getZipped($guids): string + public function getZipped(array $guids = []): string { $nzb = new NZB(); - $zipFile = new \ZipFile(); + $zipped = new Zipper(); + $zippedFileName = now()->format('Ymdhis').'.nzb.zip'; + $zippedFilePath = resource_path().'/tmp/'.$zippedFileName; foreach ($guids as $guid) { $nzbPath = $nzb->NZBPath($guid); @@ -1266,12 +1271,15 @@ class Releases if ($r) { $filename = $r['searchname']; } - $zipFile->addFile($nzbContents, $filename.'.nzb'); + $zipped->make($zippedFilePath)->addString($filename.'.nzb', $nzbContents); } } } - return $zipFile->file(); + $zipped->close(); + + return File::isFile($zippedFilePath) ? $zippedFilePath : ''; + } /** diff --git a/Changelog b/Changelog index 29251ca52..89dc30a37 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-11-15 DariusIII + * Chg: Use chumper/zipper to generate zipped nzb files * Chg: Remove YencServiceProvider from app.php * Chg: Make PhpYenc the only yenc available, remove rest of the files used * Chg: Update app/Extensions/util/Versions.php diff --git a/app/Http/Controllers/GetNzbController.php b/app/Http/Controllers/GetNzbController.php index c14c5d3d2..24bc4d281 100644 --- a/app/Http/Controllers/GetNzbController.php +++ b/app/Http/Controllers/GetNzbController.php @@ -79,7 +79,7 @@ class GetNzbController extends BasePageController } $zip = $rel->getZipped($guids); - if (\strlen($zip) > 0) { + if ($zip !== '') { User::incrementGrabs($uid, \count($guids)); foreach ($guids as $guid) { Release::updateGrab($guid); @@ -90,12 +90,10 @@ class GetNzbController extends BasePageController } } - return response()->streamDownload(function () use ($zip) { - echo $zip; - }, now()->format('Ymdhis').'.nzb.zip', ['Content-type:' => 'application/octet-stream']); + return response()->download($zip, now()->format('Ymdhis').'.nzb.zip', ['Content-type:' => 'application/zip'])->deleteFileAfterSend(true); } - $this->show404(); + return response()->json(['message' => 'Unable to create .zip file'], 404); } $nzbPath = (new NZB())->getNZBPath($request->input('id')); diff --git a/composer.json b/composer.json index 8144bdbba..0cd1753d6 100755 --- a/composer.json +++ b/composer.json @@ -85,6 +85,7 @@ "barracudanetworks/forkdaemon-php": "~1.0", "bhuvidya/laravel-countries": "^1.0", "canihavesomecoffee/thetvdbapi": "^1.0", + "chumper/zipper": "^1.0", "czproject/git-php": "^3.14", "dariusiii/rarinfo": "^2.5", "dborsatto/php-giantbomb": "^1.0", diff --git a/composer.lock b/composer.lock index e18e7883b..152a8488d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0eed6470579e605d5cdf09623326591d", + "content-hash": "3058d014ff7c9235555b4011923dd4b1", "packages": [ { "name": "aharen/omdbapi", @@ -340,6 +340,66 @@ "description": "PHP implementation for the 2nd version of the TheTVDb API.", "time": "2018-05-19T09:06:38+00:00" }, + { + "name": "chumper/zipper", + "version": "v1.0.2", + "source": { + "type": "git", + "url": "https://github.com/Chumper/Zipper.git", + "reference": "6a1733c34d67c3952b8439afb36ad4ea5c3ceacb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Chumper/Zipper/zipball/6a1733c34d67c3952b8439afb36ad4ea5c3ceacb", + "reference": "6a1733c34d67c3952b8439afb36ad4ea5c3ceacb", + "shasum": "" + }, + "require": { + "illuminate/filesystem": "^5.0", + "illuminate/support": "^5.0", + "php": ">=5.6.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.7" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Chumper\\Zipper\\ZipperServiceProvider" + ], + "aliases": { + "Zipper": "Chumper\\Zipper\\Zipper" + } + } + }, + "autoload": { + "psr-4": { + "Chumper\\Zipper\\": "src/Chumper/Zipper" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Nils Plaschke", + "email": "github@nilsplaschke.de", + "homepage": "http://nilsplaschke.de", + "role": "Developer" + } + ], + "description": "This is a little neat helper for the ZipArchive methods with handy functions", + "homepage": "http://github.com/Chumper/zipper", + "keywords": [ + "archive", + "laravel", + "zip" + ], + "time": "2017-07-17T08:05:10+00:00" + }, { "name": "clue/stream-filter", "version": "v1.4.0", diff --git a/libs/ZipFile.php b/libs/ZipFile.php deleted file mode 100644 index 9d3b08646..000000000 --- a/libs/ZipFile.php +++ /dev/null @@ -1,184 +0,0 @@ - - * - * http://www.zend.com/codex.php?id=470&single=1 - * by Denis125 - * - * a patch from Peter Listiak for last modified - * date and time of the compressed file - * - * Official ZIP file format: http://www.pkware.com/appnote.txt - * - * @access public - * @package phpMyAdmin - */ -class ZipFile -{ - /** - * Array to store compressed data - * - * @var array $datasec - */ - public $datasec = []; - - /** - * Central directory - * - * @var array $ctrl_dir - */ - public $ctrl_dir = []; - - /** - * End of central directory record - * - * @var string $eof_ctrl_dir - */ - public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; - - /** - * Last offset position - * - * @var integer $old_offset - */ - public $old_offset = 0; - - - /** - * Converts an Unix timestamp to a four byte DOS date and time format (date - * in high two bytes, time in low two bytes allowing magnitude comparison). - * - * @param integer the current Unix timestamp - * - * @return integer the current date in a four byte DOS format - * - * @access private - */ - private function unix2DosTime($unixtime = 0): int - { - $timearray = ($unixtime === 0) ? getdate() : getdate($unixtime); - - if ($timearray['year'] < 1980) { - $timearray['year'] = 1980; - $timearray['mon'] = 1; - $timearray['mday'] = 1; - $timearray['hours'] = 0; - $timearray['minutes'] = 0; - $timearray['seconds'] = 0; - } // end if - - return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | - ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); - } // end of the 'unix2DosTime()' method - - - /** - * Adds "file" to archive - * - * @param string file contents - * @param string name of the file in the archive (may contains the path) - * @param integer the current timestamp - * - * @access public - */ - public function addFile($data, $name, $time = 0): void - { - $name = str_replace('\\', '/', $name); - - $dtime = dechex($this->unix2DosTime($time)); - $hexdtime = '\x' . $dtime[6] . $dtime[7] - . '\x' . $dtime[4] . $dtime[5] - . '\x' . $dtime[2] . $dtime[3] - . '\x' . $dtime[0] . $dtime[1]; - eval('$hexdtime = "' . $hexdtime . '";'); - - $fr = "\x50\x4b\x03\x04"; - $fr .= "\x14\x00"; // ver needed to extract - $fr .= "\x00\x00"; // gen purpose bit flag - $fr .= "\x08\x00"; // compression method - $fr .= $hexdtime; // last mod time and date - - // "local file header" segment - $unc_len = strlen($data); - $crc = crc32($data); - $zdata = gzcompress($data); - $zdata = substr(substr($zdata, 0, -4), 2); // fix crc bug - $c_len = strlen($zdata); - $fr .= pack('V', $crc); // crc32 - $fr .= pack('V', $c_len); // compressed filesize - $fr .= pack('V', $unc_len); // uncompressed filesize - $fr .= pack('v', strlen($name)); // length of filename - $fr .= pack('v', 0); // extra field length - $fr .= $name; - - // "file data" segment - $fr .= $zdata; - - // add this entry to array - $this -> datasec[] = $fr; - - // now add to central directory record - $cdrec = "\x50\x4b\x01\x02"; - $cdrec .= "\x00\x00"; // version made by - $cdrec .= "\x14\x00"; // version needed to extract - $cdrec .= "\x00\x00"; // gen purpose bit flag - $cdrec .= "\x08\x00"; // compression method - $cdrec .= $hexdtime; // last mod time & date - $cdrec .= pack('V', $crc); // crc32 - $cdrec .= pack('V', $c_len); // compressed filesize - $cdrec .= pack('V', $unc_len); // uncompressed filesize - $cdrec .= pack('v', strlen($name)); // length of filename - $cdrec .= pack('v', 0); // extra field length - $cdrec .= pack('v', 0); // file comment length - $cdrec .= pack('v', 0); // disk number start - $cdrec .= pack('v', 0); // internal file attributes - $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set - - $cdrec .= pack('V', $this -> old_offset); // relative offset of local header - $this -> old_offset += strlen($fr); - - $cdrec .= $name; - - // optional extra field, file comment goes here - // save to central directory - $this -> ctrl_dir[] = $cdrec; - } // end of the 'addFile()' method - - - /** - * Dumps out file - * - * @return string the zipped file - * - * @access public - */ - public function file(): string - { - $data = implode('', $this -> datasec); - $ctrldir = implode('', $this -> ctrl_dir); - - return - $data . - $ctrldir . - $this -> eof_ctrl_dir . - pack('v', count($this -> ctrl_dir)) . // total # of entries "on this disk" - pack('v', count($this -> ctrl_dir)) . // total # of entries overall - pack('V', strlen($ctrldir)) . // size of central dir - pack('V', strlen($data)) . // offset to start of central dir - "\x00\x00"; // .zip file comment length - } // end of the 'file()' method - -}