Moved zip archive code into separate class.

This commit is contained in:
Nils Asmussen 2016-08-20 15:17:11 +02:00
parent 727c74c294
commit 97a379dc47
3 changed files with 172 additions and 107 deletions

View File

@ -2,8 +2,6 @@
namespace Lychee\Modules;
use ZipArchive;
final class Album {
private $albumIDs = null;
@ -236,41 +234,20 @@ final class Album {
break;
}
// Escape title
$zipTitle = $this->cleanZipName($zipTitle);
$archive = new Archive($zipTitle);
$filename = LYCHEE_DATA . $zipTitle . '.zip';
// Create zip
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
return false;
}
// Add photos to zip
switch($this->albumIDs) {
case 's':
case 'f':
case 'r':
$this->addPhotosToZip($zip, $zipTitle, $photos);
break;
if (!$archive->addPhotos($zipTitle, $photos)) return false;
break;
default:
$this->addAlbumToZip($zip, $zipTitle, $this->albumIDs);
if (!$archive->addAlbum($zipTitle, $this->albumIDs)) return false;
break;
}
// Finish zip
$zip->close();
// Send zip
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
header("Content-Length: " . filesize($filename));
readfile($filename);
// Delete zip
unlink($filename);
$archive->send();
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
@ -279,84 +256,6 @@ final class Album {
}
private function cleanZipName($name) {
// Illicit chars
$badChars = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
);
return str_replace($badChars, '', $name);
}
private function addAlbumToZip($zip, $path, $albumID) {
// Fetch album title
$photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $albumID));
$this->addPhotosToZip($zip, $path, $photos);
// Fetch subalbums
$query = Database::prepare(Database::get(), "SELECT id, title FROM ? WHERE parent = '?'", array(LYCHEE_TABLE_ALBUMS, $albumID));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
// Add them recursively
while($album = $albums->fetch_assoc()) {
$this->addAlbumToZip($zip, $path . '/' . $this->cleanZipName($album['title']), $album['id']);
}
}
private function addPhotosToZip($zip, $path, $query) {
// Execute query
$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
// Parse each path
$files = array();
while ($photo = $photos->fetch_object()) {
// Parse url
$photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
// Parse title
$photo->title = $this->cleanZipName($photo->title);
if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
// Check if readable
if (!@is_readable($photo->url)) continue;
// Get extension of image
$extension = getExtension($photo->url, false);
// Set title for photo
$zipFileName = $path . '/' . $photo->title . $extension;
// Check for duplicates
if (!empty($files)) {
$i = 1;
while (in_array($zipFileName, $files)) {
// Set new title for photo
$zipFileName = $path . '/' . $photo->title . '-' . $i . $extension;
$i++;
}
}
// Add to array
$files[] = $zipFileName;
// Add photo to zip
$zip->addFile($photo->url, $zipFileName);
}
}
/**
* @return boolean Returns true when successful.
*/

167
php/Modules/Archive.php Normal file
View File

@ -0,0 +1,167 @@
<?php
namespace Lychee\Modules;
use ZipArchive;
/**
* Allows the creation of zip archive for albums and photos.
*/
final class Archive {
private $title;
private $filename;
private $zip;
/**
* Creates a new zip archive in LYCHEE_DATA with given title.
*
* @param string $title the title
*/
public function __construct($title) {
// Escape title
$this->title = $this->cleanZipName($title);
$this->filename = LYCHEE_DATA . $this->title . '.zip';
// Create zip
$this->zip = new ZipArchive();
if ($this->zip->open($this->filename, ZIPARCHIVE::CREATE)!==TRUE) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
$this->zip = null;
}
}
public function __destruct() {
if ($this->zip!==null) $this->zip->close();
unlink($this->filename);
}
/**
* Closes the zip file and sends it to the browser.
*/
public function send() {
// Finish zip
$this->zip->close();
$this->zip = null;
// Send zip
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"" . $this->title . ".zip\"");
header("Content-Length: " . filesize($this->filename));
readfile($this->filename);
}
/**
* Adds the given album including subalbums at given path to the zip archive.
*
* @param string $path the path in the zip archive
* @param int $albumID the id of the album
* @return true on success
*/
public function addAlbum($path, $albumID) {
// Fetch photos
$query = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $albumID));
if (!$this->addPhotos($path, $query)) return false;
// Fetch subalbums
$query = Database::prepare(Database::get(), "SELECT id, title FROM ? WHERE parent = '?'", array(LYCHEE_TABLE_ALBUMS, $albumID));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($albums===false) return false;
// Add them recursively
while($album = $albums->fetch_assoc()) {
if (!$this->addAlbum($path . '/' . $this->cleanZipName($album['title']), $album['id'])) return false;
}
return true;
}
/**
* Adds the photos that are selected by the given query at given path to the zip archive.
*
* @param string $path the path in the zip archive
* @param mysqli_stmt $query the SQL query to execute
* @return true on success
*/
public function addPhotos($path, $query) {
if ($this->zip===null) return false;
$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if (!$photos) return false;
// Parse each path
$files = array();
while ($photo = $photos->fetch_object()) {
// Parse url
$photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
// Parse title
$photo->title = $this->cleanZipName($photo->title);
if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
// Check if readable
if (!@is_readable($photo->url)) continue;
// Get extension of image
$extension = getExtension($photo->url, false);
// Set title for photo
$zipFileName = $path . '/' . $photo->title . $extension;
// Check for duplicates
if (!empty($files)) {
$i = 1;
while (in_array($zipFileName, $files)) {
// Set new title for photo
$zipFileName = $path . '/' . $photo->title . '-' . $i . $extension;
$i++;
}
}
// Add to array
$files[] = $zipFileName;
// Add photo to zip
$this->zip->addFile($photo->url, $zipFileName);
}
return true;
}
private function cleanZipName($name) {
// Illicit chars
$badChars = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
);
return str_replace($badChars, '', $name);
}
}
?>

View File

@ -2,7 +2,6 @@
namespace Lychee\Modules;
use ZipArchive;
use Imagick;
use ImagickPixel;