Jelajahi Sumber

Get files for a particular page

Diego Najar 5 tahun lalu
induk
melakukan
fd80820e0b
1 mengubah file dengan 41 tambahan dan 0 penghapusan
  1. 41 0
      bl-plugins/api/plugin.php

+ 41 - 0
bl-plugins/api/plugin.php

@@ -191,6 +191,11 @@ class pluginAPI extends Plugin {
 			$username = $parameters[1];
 			$data = $this->getUser($username);
 		}
+		// (GET) /api/files/<page-key>
+		elseif ( ($method==='GET') && ($parameters[0]==='files') && !empty($parameters[1]) ) {
+			$pageKey = $parameters[1];
+			$data = $this->getFiles($pageKey);
+		}
 		else {
 			$this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.'));
 		}
@@ -667,4 +672,40 @@ class pluginAPI extends Plugin {
 			'data'=>$data
 		);
 	}
+
+	/*
+	 | Returns all files uploaded for a specific page, includes any type of file.
+	 |
+	 | @return	array
+         */
+	private function getFiles($pageKey)
+	{
+		$chunk = false;
+		$sortByDate = true;
+		$path = PATH_UPLOADS_PAGES.$pageKey.DS;
+		$listFiles = Filesystem::listFiles($path, '*', '*', $sortByDate, $chunk);
+
+		$files = array();
+		foreach ($listFiles as $file) {
+			$info = array('thumbnail'=>'');
+			$info['file'] = $file;
+			$info['filename'] = basename($file);
+			$info['mime'] = Filesystem::mimeType($file);
+			$info['size'] = Filesystem::getSize($file);
+
+			// Check if thumbnail exists for the file
+			$thumbnail = $path.'thumbnails'.DS.$info['filename'];
+			if (Filesystem::fileExists($thumbnail)) {
+				$info['thumbnail'] = $thumbnail;
+			}
+
+			array_push($files, $info);
+		}
+
+		return array(
+			'status'=>'0',
+			'message'=>'Files for the page key: '.$pageKey,
+			'data'=>$files
+		);
+	}
 }