Improved getArchive with file names (#111)

This commit is contained in:
Tobias Reich 2014-04-12 19:55:05 +02:00
parent 17b6475b6c
commit 6530902d13

View File

@ -232,53 +232,76 @@ class Album extends Module {
# Photos query # Photos query
switch($this->albumIDs) { switch($this->albumIDs) {
case 's': case 's':
$photos = "SELECT title, type, url FROM lychee_photos WHERE public = '1';"; $photos = "SELECT title, url FROM lychee_photos WHERE public = '1';";
$zipTitle = 'Public'; $zipTitle = 'Public';
break; break;
case 'f': case 'f':
$photos = "SELECT title, type, url FROM lychee_photos WHERE star = '1';"; $photos = "SELECT title, url FROM lychee_photos WHERE star = '1';";
$zipTitle = 'Starred'; $zipTitle = 'Starred';
break; break;
default: default:
$photos = "SELECT title, type, url FROM lychee_photos WHERE album = '$this->albumIDs';"; $photos = "SELECT title, url FROM lychee_photos WHERE album = '$this->albumIDs';";
$zipTitle = 'Unsorted'; $zipTitle = 'Unsorted';
} }
# Execute query
$photos = $this->database->query($photos);
# Check if album empty
if ($photos->num_rows==0) return false;
# Init vars
$files = array();
$i = 0;
# Parse each path
while ($photo = $photos->fetch_object()) {
$files[$i] = __DIR__ . '/../../uploads/big/' . $photo->url;
$i++;
}
# Set title # Set title
$album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;"); $album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title; if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title;
$filename = __DIR__ . "/../../data/$zipTitle.zip"; $filename = __DIR__ . "/../../data/$zipTitle.zip";
# Create zip # Create zip
$zip = new ZipArchive(); $zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false; if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
# Add each photo # Execute query
foreach ($files AS $file) { $photos = $this->database->query($photos);
if (!@is_readable($file)) continue; # Check if album empty
if ($photos->num_rows==0) return false;
$photoName = explode('/', $file); # Parse each path
$photoName = array_reverse($photoName); $files = array();
$photoName = $photoName[0]; while ($photo = $photos->fetch_object()) {
$zip->addFile($file, $zipTitle . '/' . $photoName);
# Parse url
$photo->url = __DIR__ . '/../../uploads/big/' . $photo->url;
# Parse title
$badChars = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
);
$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 = array_reverse(explode('.', $photo->url));
$extension = $extension[0];
# 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);
} }