Browse Source

Improved getArchive with file names (#111)

Tobias Reich 11 years ago
parent
commit
6530902d13
1 changed files with 47 additions and 24 deletions
  1. 47 24
      php/modules/Album.php

+ 47 - 24
php/modules/Album.php

@@ -232,53 +232,76 @@ class Album extends Module {
 		# Photos query
 		switch($this->albumIDs) {
 			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';
 				break;
 			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';
 				break;
 			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';
 		}
 
+		# Set title
+		$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;
+		$filename = __DIR__ . "/../../data/$zipTitle.zip";
+
+		# Create zip
+		$zip = new ZipArchive();
+		if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
+
 		# 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
+		$files = array();
 		while ($photo = $photos->fetch_object()) {
-			$files[$i] = __DIR__ . '/../../uploads/big/' . $photo->url;
-			$i++;
-		}
 
-		# Set title
-		$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;
-		$filename = __DIR__ . "/../../data/$zipTitle.zip";
+			# Parse url
+			$photo->url = __DIR__ . '/../../uploads/big/' . $photo->url;
 
-		# Create zip
+			# 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';
 
-		$zip = new ZipArchive();
-		if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
+			# Check if readable
+			if (!@is_readable($photo->url)) continue;
+
+			# Get extension of image
+			$extension = array_reverse(explode('.', $photo->url));
+			$extension = $extension[0];
 
-		# Add each photo
-		foreach ($files AS $file) {
+			# 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++;
+
+				}
+			}
 
-			if (!@is_readable($file)) continue;
+			# Add to array
+			$files[] = $zipFileName;
 
-			$photoName = explode('/', $file);
-			$photoName = array_reverse($photoName);
-			$photoName = $photoName[0];
-			$zip->addFile($file, $zipTitle . '/' . $photoName);
+			# Add photo to zip
+			$zip->addFile($photo->url, $zipFileName);
 
 		}