Include subalbums in album archives.
This commit is contained in:
parent
ef9040870a
commit
212b241d0e
@ -195,12 +195,6 @@ final class Album {
|
|||||||
// Call plugins
|
// Call plugins
|
||||||
Plugins::get()->activate(__METHOD__, 0, func_get_args());
|
Plugins::get()->activate(__METHOD__, 0, func_get_args());
|
||||||
|
|
||||||
// Illicit chars
|
|
||||||
$badChars = array_merge(
|
|
||||||
array_map('chr', range(0,31)),
|
|
||||||
array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
|
|
||||||
);
|
|
||||||
|
|
||||||
// Photos query
|
// Photos query
|
||||||
switch($this->albumIDs) {
|
switch($this->albumIDs) {
|
||||||
case 's':
|
case 's':
|
||||||
@ -216,12 +210,10 @@ final class Album {
|
|||||||
$zipTitle = 'Recent';
|
$zipTitle = 'Recent';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
|
|
||||||
$zipTitle = 'Unsorted';
|
$zipTitle = 'Unsorted';
|
||||||
}
|
|
||||||
|
|
||||||
// Get title from database when album is not a SmartAlbum
|
// Get title from database when album is not a SmartAlbum
|
||||||
if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
|
if ($this->albumIDs!=0 && is_numeric($this->albumIDs)) {
|
||||||
|
|
||||||
$query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
|
$query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
|
||||||
$album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
|
$album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
|
||||||
@ -241,9 +233,11 @@ final class Album {
|
|||||||
$zipTitle = $album->title;
|
$zipTitle = $album->title;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Escape title
|
// Escape title
|
||||||
$zipTitle = str_replace($badChars, '', $zipTitle);
|
$zipTitle = $this->cleanZipName($zipTitle);
|
||||||
|
|
||||||
$filename = LYCHEE_DATA . $zipTitle . '.zip';
|
$filename = LYCHEE_DATA . $zipTitle . '.zip';
|
||||||
|
|
||||||
@ -254,56 +248,16 @@ final class Album {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute query
|
// Add photos to zip
|
||||||
$photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
|
switch($this->albumIDs) {
|
||||||
|
case 's':
|
||||||
if ($album===null) return false;
|
case 'f':
|
||||||
|
case 'r':
|
||||||
// Check if album empty
|
$this->addPhotosToZip($zip, $zipTitle, $photos);
|
||||||
if ($photos->num_rows==0) {
|
break;
|
||||||
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
|
default:
|
||||||
return false;
|
$this->addAlbumToZip($zip, $zipTitle, $this->albumIDs);
|
||||||
}
|
break;
|
||||||
|
|
||||||
// Parse each path
|
|
||||||
$files = array();
|
|
||||||
while ($photo = $photos->fetch_object()) {
|
|
||||||
|
|
||||||
// Parse url
|
|
||||||
$photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
|
|
||||||
|
|
||||||
// Parse title
|
|
||||||
$photo->title = str_replace($badChars, '', $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 = $zipTitle . '/' . $photo->title . $extension;
|
|
||||||
|
|
||||||
// Check for duplicates
|
|
||||||
if (!empty($files)) {
|
|
||||||
$i = 1;
|
|
||||||
while (in_array($zipFileName, $files)) {
|
|
||||||
|
|
||||||
// Set new title for photo
|
|
||||||
$zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
|
|
||||||
|
|
||||||
$i++;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to array
|
|
||||||
$files[] = $zipFileName;
|
|
||||||
|
|
||||||
// Add photo to zip
|
|
||||||
$zip->addFile($photo->url, $zipFileName);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finish zip
|
// Finish zip
|
||||||
@ -325,6 +279,84 @@ 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.
|
* @return boolean Returns true when successful.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user