Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
feature (standalone gallery): continous check for new pictures
Browse files Browse the repository at this point in the history
Adjustments by Andreas Blaesius:
- eslint fixes
- adjustments to work on latest Photobooth
- slightly adjust commit message

Change-Id: I58320492085549ad2975a4a10021567edfb02fc9
  • Loading branch information
EccoB authored and andi34 committed Oct 24, 2020
1 parent 9563e5c commit 172bdd3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions gallery.php
Expand Up @@ -3,6 +3,16 @@
require_once('lib/config.php');
require_once('lib/db.php');

// 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());
exit(json_encode($resp));

}

$images = getImagesFromDB();
$imagelist = ($config['newest_first'] === true) ? array_reverse($images) : $images;
?>
Expand Down Expand Up @@ -91,6 +101,7 @@
<script type="text/javascript" src="resources/js/theme.js"></script>
<script type="text/javascript" src="resources/js/core.js"></script>
<script type="text/javascript" src="resources/js/gallery.js"></script>
<script type="text/javascript" src="resources/js/gallery_updatecheck.js"></script>
<script src="node_modules/@andreasremdt/simple-translator/dist/umd/translator.min.js"></script>
<script type="text/javascript" src="resources/js/i18n.js"></script>
</body>
Expand Down
7 changes: 7 additions & 0 deletions lib/db.php
Expand Up @@ -33,3 +33,10 @@ function isImageInDB($filename) {

return in_array($filename, $images);
}

function getDBSize(){
if(file_exists(DB_FILE)){
return (int) filesize(DB_FILE);
}
return 0;
}
47 changes: 47 additions & 0 deletions src/js/gallery_updatecheck.js
@@ -0,0 +1,47 @@
/* globals photoBooth */
/*
* This script checks for new pictures in regular intervals.
* If changes are detected, the page will automatically be reloaded.
*
* Needs:
* jQuery
* photoBooth Javascript
*
* Remarks:
* - Not made for highly demanded pages (as pages is requested regulary
* and would pile up in high load with thausend of users)
* - Instead of reloading, adding the pictures directly would be an
* improvement, but would need further changes in gallery-templates
*
*/

//Size of the DB - is used to determine changes
let lastDBSize = -1;
// Interval, the pages is checked (/ms)
const interval = 1000 * 5;
//URL to request for changes
const ajaxurl = 'gallery.php/?status';

/*
* This function will be called if there are new pictures
*/

function dbUpdated() {
console.log('DB is updated - refreshing');
//location.reload(true); //Alternative
photoBooth.reloadPage();
}

const checkForUpdates = function () {
$.getJSON({
url: ajaxurl,
success: function (result) {
const currentDBSize = result.dbsize;
if (lastDBSize != currentDBSize && lastDBSize != -1) {
dbUpdated();
}
lastDBSize = currentDBSize;
}
});
};
setInterval(checkForUpdates, interval);

0 comments on commit 172bdd3

Please sign in to comment.