Skip to content

Commit

Permalink
lib(db): add DatabaseManager for database actions
Browse files Browse the repository at this point in the history
Change-Id: I2f59f4056e47b8e9bf81dad8b6655a5fb8665ba6
  • Loading branch information
andi34 committed Jun 4, 2023
1 parent ea63f4f commit 4ffac42
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 69 deletions.
4 changes: 2 additions & 2 deletions api/applyEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}

$file = $_POST['file'];

$database = new DatabaseManager(DB_FILE, IMG_DIR);
$quality = 100;
$imageModified = false;
$image_filter = false;
Expand Down Expand Up @@ -118,7 +118,7 @@
// insert into database
if ($config['database']['enabled']) {
if ($_POST['style'] !== 'chroma' || ($_POST['style'] === 'chroma' && $config['live_keying']['show_all'] === true)) {
appendImageToDB($image);
$database->appendFileToDB($image);
}
}

Expand Down
5 changes: 3 additions & 2 deletions api/applyVideoEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_once '../lib/collage.php';

$file = $_POST['file'];
$database = new DatabaseManager(DB_FILE, IMG_DIR);
$tmpFolder = $config['foldersAbs']['tmp'] . DIRECTORY_SEPARATOR;
$imageFolder = $config['foldersAbs']['images'] . DIRECTORY_SEPARATOR;
$thumbsFolder = $config['foldersAbs']['thumbs'] . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -68,7 +69,7 @@
}
imagedestroy($imageResource);
if ($config['database']['enabled']) {
appendImageToDB(basename($newFile));
$database->appendFileToDB(basename($newFile));
}
$picture_permissions = $config['picture']['permissions'];
chmod($newFile, octdec($picture_permissions));
Expand Down Expand Up @@ -137,7 +138,7 @@
/* TODO gallery doesn't support videos atm
// insert into database
if ($config['database']['enabled']) {
appendImageToDB($file);
$database->appendFileToDB($file);
}*/

// Change permissions
Expand Down
3 changes: 2 additions & 1 deletion api/chromakeying/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once '../../lib/resize.php';

$file = Image::create_new_filename($config['picture']['naming']);
$database = new DatabaseManager(DB_FILE, IMG_DIR);

if ($config['database']['file'] != 'db') {
$file = $config['database']['file'] . '_' . $file;
Expand Down Expand Up @@ -34,7 +35,7 @@

// insert into database
if ($config['database']['enabled']) {
appendImageToDB($file);
$database->appendFileToDB($file);
}

// Change permissions
Expand Down
3 changes: 2 additions & 1 deletion api/deletePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
}

if ($config['database']['enabled']) {
deleteImageFromDB($file);
$database = new DatabaseManager(DB_FILE, IMG_DIR);
$database->deleteFileFromDB($file);
}

$LogData = [
Expand Down
3 changes: 2 additions & 1 deletion api/rebuildImageDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
require_once '../lib/config.php';
require_once '../lib/db.php';

rebuildPictureDB();
$database = new DatabaseManager(DB_FILE, IMG_DIR);
die(json_encode($database->rebuildDB()));
3 changes: 2 additions & 1 deletion api/sendPic.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
}

$postImage = basename($_POST['image']);
if (!isImageInDB($postImage)) {
$database = new DatabaseManager(DB_FILE, IMG_DIR);
if (!$database->isFileInDB($postImage)) {
$LogData = [
'success' => false,
'error' => 'Image not found in database',
Expand Down
8 changes: 5 additions & 3 deletions gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
require_once('lib/config.php');
require_once('lib/db.php');

$database = new DatabaseManager(DB_FILE, IMG_DIR);

// Check if there is a request for the status of the database
if (isset($_GET['status'])){
// Request for DB-Status,
// Currently reports back the DB-Size to give the Client the ability
// to detect changes
$resp = array('dbsize'=>getDBSize());
$resp = array('dbsize'=>$database->getDBSize());
exit(json_encode($resp));
}

if ($config['database']['enabled']) {
$images = getImagesFromDB();
$images = $database->getFilesFromDB();
} else {
$images = getImagesFromDirectory($config['foldersAbs']['images']);
$images = $database->getFilesFromDirectory();
}

$imagelist = $config['gallery']['newest_first'] === true && !empty($images) ? array_reverse($images) : $images;
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
require_once 'lib/db.php';
require_once 'lib/filter.php';

$database = new DatabaseManager(DB_FILE, IMG_DIR);
if ($config['database']['enabled']) {
$images = getImagesFromDB();
$images = $database->getFilesFromDB();
} else {
$images = getImagesFromDirectory($config['foldersAbs']['images']);
$images = $database->getFilesFromDirectory();
}

$imagelist = $config['gallery']['newest_first'] === true && !empty($images) ? array_reverse($images) : $images;

$btnClass = 'btn btn--' . $config['ui']['button'];
Expand Down
183 changes: 132 additions & 51 deletions lib/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,157 @@
define('MAIL_FILE', $config['foldersAbs']['data'] . DIRECTORY_SEPARATOR . $config['mail']['file'] . '.txt');
define('IMG_DIR', $config['foldersAbs']['images']);

function getImagesFromDB() {
// get data from db.txt
if (file_exists(DB_FILE)) {
return json_decode(file_get_contents(DB_FILE));
/**
* Class DatabaseManager
*
* Manages the database, including adding and deleting files.
*/
class DatabaseManager {
/**
* @var string The absolute path and name of the file containing the database of files.
*/
private $db_file;

/**
* @var string The absolute path to the directory containing the files.
*/
private $file_dir;

/**
* DatabaseManager constructor.
*
* Sets up the object with the necessary file and directory paths.
*/
public function __construct($db_file, $file_dir) {
if (!$db_file) {
throw new InvalidArgumentExeption('Invalid database.');
}
$this->db_file = $db_file;
if (!$file_dir) {
throw new InvalidArgumentExeption('Invalid file path.');
}
$this->file_dir = $file_dir;
}

return [];
}

function getImagesFromDirectory($directory) {
$dh = opendir($directory);
/**
* Get the list of files from the database file.
*
* @return array The list of files from the database file.
*/
public function getFilesFromDB() {
// get data from database
if (file_exists($this->db_file)) {
return json_decode(file_get_contents($this->db_file));
}

while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
return [];
}
closedir($dh);
$images = preg_grep('/\.(jpg|jpeg|JPG|JPEG)$/i', $files);
return $images;
}

function appendImageToDB($filename) {
$images = getImagesFromDB();
/**
* Get the list of images from the images directory.
*
* @return array The list of images from the images directory.
*/
public function getFilesFromDirectory() {
$dh = opendir($this->file_dir);

if (!in_array($filename, $images)) {
$images[] = $filename;
file_put_contents(DB_FILE, json_encode($images));
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
closedir($dh);
$images = preg_grep('/\.(jpg|jpeg|JPG|JPEG)$/i', $files);
return $images;
}
}

function deleteImageFromDB($filename) {
$images = getImagesFromDB();
/**
* Append a new file by filename to the database file.
*
* @param string $filename The filename of the file to add to the database file.
*/
public function appendFileToDB($filename) {
if (!$filename) {
throw new InvalidArgumentExeption('Invalid filename.');
}
$files = $this->getFilesFromDB();

if (in_array($filename, $images)) {
unset($images[array_search($filename, $images)]);
file_put_contents(DB_FILE, json_encode(array_values($images)));
if (!in_array($filename, $files)) {
$files[] = $filename;
file_put_contents($this->db_file, json_encode($files));
}
}

if (file_exists(DB_FILE) && empty($images)) {
unlink(DB_FILE);
}
}
/**
* Delete an file by filename from the database file.
*
* @param string $filename The filename of the file to delete from the database file.
*/
public function deleteFileFromDB($filename) {
if (!$filename) {
throw new InvalidArgumentExeption('Invalid filename.');
}
$files = $this->getFilesFromDB();

function isImageInDB($filename) {
$images = getImagesFromDB();
if (in_array($filename, $files)) {
unset($files[array_search($filename, $files)]);
file_put_contents($this->db_file, json_encode(array_values($files)));
}

return in_array($filename, $images);
}
if (file_exists($this->db_file) && empty($files)) {
unlink($this->db_file);
}
}

function getDBSize() {
if (file_exists(DB_FILE)) {
return (int) filesize(DB_FILE);
/**
* Check if an filename exists in the database file.
*
* @param string $filename The filename of the file to check.
*
* @return bool Whether the filename exists in the database file.
*/
public function isFileInDB($filename) {
if (!$filename) {
throw new InvalidArgumentExeption('Invalid filename.');
}
$files = $this->getFilesFromDB();

return in_array($filename, $files);
}
return 0;
}

function rebuildPictureDB() {
$output = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(IMG_DIR, FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS)) as $value) {
if ($value->isFile()) {
$output[] = [$value->getMTime(), $value->getFilename()];
/**
* Returns the size of the database file.
*
* @return int The size of the database file in bytes.
*/
public function getDBSize() {
if (file_exists($this->db_file)) {
return (int) filesize($this->db_file);
}
return 0;
}

usort($output, function ($a, $b) {
return strlen($a[0]) <=> strlen($b[0]);
});
/**
* Rebuilds the image database by scanning the image directory and creating a new database
* file with the names of all files sorted by modification time.
*
* @return string The string "success" if the database was rebuilt successfully, or "error"
* if an error occurred during the rebuilding process.
*/
public function rebuildDB() {
$output = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->file_dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS)) as $value) {
if ($value->isFile()) {
$output[] = [$value->getMTime(), $value->getFilename()];
}
}

if (file_put_contents(DB_FILE, json_encode(array_column($output, 1))) === 'false') {
echo json_encode('error');
} else {
echo json_encode('success');
usort($output, function ($a, $b) {
return strlen($a[0]) <=> strlen($b[0]);
});

if (file_put_contents($this->db_file, json_encode(array_column($output, 1))) === 'false') {
return 'error';
} else {
return 'success';
}
}
}
5 changes: 3 additions & 2 deletions livechroma.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
) {
require_once 'lib/db.php';

$database = new DatabaseManager(DB_FILE, IMG_DIR);
if ($config['database']['enabled']) {
$images = getImagesFromDB();
$images = $database->getFilesFromDB();
} else {
$images = getImagesFromDirectory($config['foldersAbs']['images']);
$images = $database->getFilesFromDirectory();
}
$imagelist = $config['gallery']['newest_first'] === true ? array_reverse($images) : $images;

Expand Down
5 changes: 3 additions & 2 deletions slideshow/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
require_once('../lib/config.php');
require_once('../lib/db.php');

$database = new DatabaseManager(DB_FILE, IMG_DIR);
if ($config['database']['enabled']) {
$images = getImagesFromDB();
$images = $database->getFilesFromDB();
} else {
$images = getImagesFromDirectory($config['foldersAbs']['images']);
$images = $database->getFilesFromDirectory();
}

$imagelist = !empty($images) ? array_reverse($images) : $images;
Expand Down

0 comments on commit 4ffac42

Please sign in to comment.