init commit2
This commit is contained in:
93
www/client_webapp/css/style.css
Normal file
93
www/client_webapp/css/style.css
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Toggle */
|
||||
|
||||
.switch {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
width: 52px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.switch input {
|
||||
opacity: 0;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
.slider {
|
||||
position: absolute;
|
||||
cursor: pointer;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: #ccc;
|
||||
-webkit-transition: .4s;
|
||||
transition: .4s;
|
||||
}
|
||||
|
||||
.slider:before {
|
||||
position: absolute;
|
||||
content: "";
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
left: 4px;
|
||||
bottom: 2px;
|
||||
background-color: white;
|
||||
-webkit-transition: .4s;
|
||||
transition: .4s;
|
||||
}
|
||||
|
||||
input:checked+.slider {
|
||||
background-color: #2196F3;
|
||||
}
|
||||
|
||||
input:focus+.slider {
|
||||
box-shadow: 0 0 1px #2196F3;
|
||||
}
|
||||
|
||||
input:checked+.slider:before {
|
||||
-webkit-transform: translateX(26px);
|
||||
-ms-transform: translateX(26px);
|
||||
transform: translateX(26px);
|
||||
}
|
||||
|
||||
/* Rounded sliders */
|
||||
|
||||
.slider.round {
|
||||
border-radius: 34px;
|
||||
}
|
||||
|
||||
.slider.round:before {
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
#inputStreamDelay {
|
||||
padding: 0;
|
||||
text-align: center;
|
||||
width: 55px;
|
||||
}
|
||||
|
||||
.mp_login_form {
|
||||
color: #000;
|
||||
width: 400px;
|
||||
margin: auto;
|
||||
margin-top: 50px;
|
||||
}
|
||||
|
||||
#textEmail {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.btn {
|
||||
font-size: 16px;
|
||||
background: #00aedb;
|
||||
}
|
||||
|
||||
.btn i {
|
||||
margin-right: 5px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.accordionLeft {
|
||||
display: block;
|
||||
}
|
||||
449
www/client_webapp/src/file-system-access-api.js
Normal file
449
www/client_webapp/src/file-system-access-api.js
Normal file
@@ -0,0 +1,449 @@
|
||||
"use strict";
|
||||
|
||||
var dirListing = new Object();
|
||||
const fileExtension = ".txt";
|
||||
const setIntervalMS = 5000;
|
||||
var dir_ref = null;
|
||||
var timer = 0;
|
||||
var elements = new Object();
|
||||
var user_email;
|
||||
var dbuserid;
|
||||
var dir_upload_all = null;
|
||||
const filesizeLimitBytes = 20000000; // 20 MB
|
||||
const FileNotOlderThanMin = 11;
|
||||
const FileNotOlderThanMinMS = FileNotOlderThanMin * 60 * 1000;
|
||||
var db;
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function(event) {
|
||||
defer(function() {
|
||||
console.log("jQuery is now loaded");
|
||||
init();
|
||||
});
|
||||
});
|
||||
|
||||
function defer(method) {
|
||||
if (window.jQuery) {
|
||||
method();
|
||||
}
|
||||
else {
|
||||
setTimeout(function() { defer(method) }, 50);
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
elements = {
|
||||
"toggleRun": jQuery("#toggleRun"),
|
||||
"toggleStreamDelay": jQuery('#toggleStreamDelay'),
|
||||
"openDirectoryButton": jQuery('#open-directory'),
|
||||
"textHandhistoryDirectory": jQuery("#textHandhistoryDirectory"),
|
||||
"textHandhistoryDirectory": jQuery("#appAccordion a#fl-accordion-5faaf49b08290-label-0")[0],
|
||||
"textHandhistoryDirectoryUploadAll": jQuery("#appAccordion span#textHandhistoryDirectoryUploadAll#textHandhistoryDirectoryUploadAll")[0],
|
||||
"inputStreamDelay": jQuery('#inputStreamDelay'),
|
||||
"sampleRequest": jQuery("#sample-request"),
|
||||
"sampleRequest2": jQuery("#sample-request2"),
|
||||
"wp_user_id": jQuery('input[name="wp_user_id"]').val(),
|
||||
"openDirectoryUploadAllButton": jQuery('#open-directory-uploadall'),
|
||||
"toggleRunUploadAll": jQuery("#toggleRunUploadAll"),
|
||||
"db_user_id": null,
|
||||
// "linkLogoutHref": jQuery("#linkLogout .fl-rich-text p a")[0].href,
|
||||
}
|
||||
featureDetection();
|
||||
console.log(elements);
|
||||
eventListeners();
|
||||
enableDisableToggle(elements.toggleRun, dir_ref);
|
||||
enableDisableToggle(elements.toggleRunUploadAll, dir_upload_all);
|
||||
enableDisableStreamDelay();
|
||||
initDBinstance();
|
||||
get_db_user_id();
|
||||
}
|
||||
|
||||
function get_db_user_id() {
|
||||
let url = myScriptVars.root + 'client/v1/dbuseridwp';
|
||||
console.log("API query: " + url);
|
||||
jQuery.ajax({
|
||||
method: 'POST',
|
||||
url: url,
|
||||
data: { "_wpnonce": myScriptVars.nonce, "wp_user_id": elements.wp_user_id },
|
||||
dataType: 'json',
|
||||
success: function(data) {
|
||||
elements.db_user_id = data;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function initDBinstance() {
|
||||
|
||||
let openRequest = indexedDB.open("pkrstarsbot", 1);
|
||||
|
||||
openRequest.onupgradeneeded = function(event) {
|
||||
console.log("the existing database version is less than 2 or it doesnt exist");
|
||||
let _db = openRequest.result;
|
||||
switch (event.oldVersion) { // existing db version
|
||||
case 0:
|
||||
console.log("Creating db...")
|
||||
case 1:
|
||||
console.log("db already created");
|
||||
}
|
||||
|
||||
if (!_db.objectStoreNames.contains('dirrefs')) {
|
||||
console.log("Creating store...");
|
||||
_db.createObjectStore('dirrefs', { keyPath: 'id' });
|
||||
}
|
||||
};
|
||||
|
||||
openRequest.onsuccess = function() {
|
||||
let _db = openRequest.result;
|
||||
|
||||
_db.onversionchange = function() {
|
||||
_db.close();
|
||||
alert("Database is outdated, please reload the page.")
|
||||
};
|
||||
|
||||
console.log("db is ready:");
|
||||
console.log("\t-> Version: " + _db.version);
|
||||
console.log("\t-> Name: " + _db.name);
|
||||
db = _db;
|
||||
retrieveFileHandle('dir_ref', 'dirrefs');
|
||||
};
|
||||
|
||||
openRequest.onblocked = function() {
|
||||
console.log("It seams there is another open connection to the same database and it wasn't closed after db.onversionchange");
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function featureDetection() {
|
||||
if ("showDirectoryPicker" in window && window.indexedDB) {
|
||||
console.log("Browser is suppported.");
|
||||
}
|
||||
else {
|
||||
console.log("Browser is not supported.");
|
||||
if (window.confirm("Your browser does not support this page!\n\nOK = Logout and Open new tab to see which browsers are supported\nCancel = Logout")) {
|
||||
window.open('https://caniuse.com/native-filesystem-api', '_blank');
|
||||
}
|
||||
|
||||
let url = myScriptVars.root + 'client/v1/logoutuser';
|
||||
console.log("API query: " + url);
|
||||
jQuery.ajax({
|
||||
method: 'POST',
|
||||
url: url,
|
||||
data: { _wpnonce: myScriptVars.nonce },
|
||||
dataType: 'json',
|
||||
success: function(data) {
|
||||
console.log(data);
|
||||
window.location.replace("https://zinomedia.de/wp_hq/pkrstarsbot/login/");
|
||||
throw new Error("Browser not supported");
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyPermission(fileHandle, mode) {
|
||||
const options = {};
|
||||
if (mode == 'readWrite') {
|
||||
options.mode = 'readwrite';
|
||||
}
|
||||
|
||||
// Request permission. If the user grants permission, return true.
|
||||
if ((await fileHandle.requestPermission(options)) === 'granted') {
|
||||
return true;
|
||||
}
|
||||
// The user didn't grant permission, so return false.
|
||||
return false;
|
||||
}
|
||||
|
||||
async function getItemByID(id, storeName, callback) {
|
||||
let request = db.transaction(storeName).objectStore(storeName).get(id);
|
||||
request.onsuccess = function(e) {
|
||||
console.log(e);
|
||||
let item = e.target.result;
|
||||
// verifyPermission()
|
||||
callback(item);
|
||||
}
|
||||
}
|
||||
|
||||
async function retrieveFileHandle(id, storeName, callback = null) {
|
||||
console.log("\t-> Retrieving " + id + " from " + storeName + "...");
|
||||
let request = db.transaction(storeName).objectStore(storeName).get(id);
|
||||
request.onsuccess = function(e) {
|
||||
let item = e.target.result;
|
||||
if (item) {
|
||||
receivedfileHandle(item);
|
||||
|
||||
if (callback)
|
||||
callback(item);
|
||||
}
|
||||
else {
|
||||
console.log("Warning: item " + id + " does not exist, cannot retrieve.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function receivedfileHandle(item) {
|
||||
if (verifyPermission(item.handle, 'readonly')) {
|
||||
console.log("\t-> Retrieving " + item.id + " successful (permission granted)");
|
||||
dir_ref = item.handle;
|
||||
elements.textHandhistoryDirectory.textContent = "Select your users handhistory directory | " + dir_ref.name + " ✓";
|
||||
enableDisableToggle(elements.toggleRun, dir_ref);
|
||||
}
|
||||
else {
|
||||
console.log("\t-> Retrieving " + item.id + " successful (permission not granted, renew?)");
|
||||
}
|
||||
}
|
||||
|
||||
function storeItem(storeName, item) {
|
||||
let transaction = db.transaction(storeName, "readwrite");
|
||||
|
||||
// get an object store to operate on it
|
||||
let store = transaction.objectStore(storeName);
|
||||
|
||||
var req = store.openCursor(item.id);
|
||||
req.onsuccess = function(e) {
|
||||
var cursor = e.target.result;
|
||||
if (cursor) {
|
||||
console.log("entry already exists");
|
||||
cursor.update(item);
|
||||
}
|
||||
else {
|
||||
console.log("entry does not yet exist");
|
||||
store.add(item)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function eventListeners() {
|
||||
elements.sampleRequest.bind('click', async() => {
|
||||
|
||||
let item = {
|
||||
id: 'open-directory',
|
||||
price: 1337,
|
||||
created: new Date(),
|
||||
};
|
||||
storeItem('dirrefs', item);
|
||||
});
|
||||
|
||||
|
||||
elements.sampleRequest2.bind('click', async() => {
|
||||
// getStoreItemByID('open-directory', 'dirrefs', itemReceived);
|
||||
retrieveFileHandle('dir_ref', 'dirrefs');
|
||||
});
|
||||
|
||||
elements.toggleRun.bind('change', function(event) {
|
||||
if (event.target.checked) {
|
||||
if (dir_ref) {
|
||||
timer = setInterval(async() => {
|
||||
console.log("\n" + '#'.repeat(40) + "\nRUN @ " + new Date() + "\n");
|
||||
let _dirListing = await loadDirEntry(dir_ref);
|
||||
let changed = findModifiedFile(_dirListing);
|
||||
FileUpload(changed);
|
||||
}, setIntervalMS);
|
||||
}
|
||||
else {
|
||||
console.log("dir_ref is not defined");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
elements.openDirectoryButton.bind('click', async() => {
|
||||
if (timer != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
dir_ref = await self.showDirectoryPicker();
|
||||
if (!dir_ref) {
|
||||
// User cancelled
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Save the reference to open directory later.
|
||||
let item = {
|
||||
id: 'dir_ref',
|
||||
handle: dir_ref,
|
||||
created: new Date(),
|
||||
};
|
||||
storeItem('dirrefs', item);
|
||||
|
||||
elements.textHandhistoryDirectory.textContent = "Select your users handhistory directory | " + dir_ref.name + " ✓";
|
||||
enableDisableToggle(elements.toggleRun, dir_ref);
|
||||
});
|
||||
|
||||
elements.openDirectoryUploadAllButton.bind('click', async() => {
|
||||
dir_upload_all = await self.showDirectoryPicker();
|
||||
if (!dir_upload_all) {
|
||||
return;
|
||||
}
|
||||
elements.textHandhistoryDirectoryUploadAll.textContent = "Select your users handhistory directory | " + dir_upload_all.name + " ✓";
|
||||
enableDisableToggle(elements.toggleRunUploadAll, dir_upload_all);
|
||||
});
|
||||
|
||||
elements.toggleStreamDelay.bind('change', function(event) {
|
||||
enableDisableStreamDelay();
|
||||
});
|
||||
|
||||
elements.toggleRunUploadAll.bind('change', async() => {
|
||||
if (event.target.checked) {
|
||||
if (dir_upload_all) {
|
||||
console.log("\n" + '#'.repeat(40) + "\nRUN UPLOADALL @ " + new Date() + "\n");
|
||||
let _dirListing = await loadDirEntry(dir_upload_all, 1); // 1 = skip ageCheck
|
||||
console.log(_dirListing);
|
||||
FileUpload(_dirListing);
|
||||
elements.toggleRunUploadAll.prop("checked", false);
|
||||
}
|
||||
else {
|
||||
console.log("directory is not defined");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function enableDisableStreamDelay() {
|
||||
if (elements.toggleStreamDelay.is(':checked')) {
|
||||
let delay = elements.inputStreamDelay.val();
|
||||
if (!delay) {
|
||||
elements.toggleStreamDelay.prop("checked", false);
|
||||
return;
|
||||
}
|
||||
elements.inputStreamDelay.disabled = true;
|
||||
elements.inputStreamDelay.css("background-color", "#ccc");
|
||||
elements.inputStreamDelay.prop("disabled", true);
|
||||
updateStreamdelay(delay, elements.db_user_id);
|
||||
}
|
||||
else {
|
||||
elements.inputStreamDelay.disabled = false;
|
||||
elements.inputStreamDelay.css("background-color", "#fff");
|
||||
elements.inputStreamDelay.prop("disabled", false);
|
||||
}
|
||||
}
|
||||
|
||||
function updateStreamdelay(delay, db_user_id) {
|
||||
let url = myScriptVars.root + 'client/v1/streamdelay';
|
||||
console.log("API query: " + url);
|
||||
jQuery.ajax({
|
||||
method: 'POST',
|
||||
url: url,
|
||||
data: { delay: delay, db_user_id: db_user_id, _wpnonce: myScriptVars.nonce },
|
||||
dataType: 'json',
|
||||
success: function(data) {
|
||||
console.log(data);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function enableDisableToggle(toggle, ref) {
|
||||
if (ref) {
|
||||
toggle.prop("disabled", false);
|
||||
}
|
||||
else {
|
||||
toggle.prop("disabled", true);
|
||||
}
|
||||
}
|
||||
|
||||
function getFileAgeMin(lastModifiedDate) {
|
||||
return Math.floor(((Math.abs(lastModifiedDate - new Date())) / 1000) / 60);
|
||||
}
|
||||
|
||||
async function loadDirEntry(dir_ref, skipAgeCheck = 0) {
|
||||
const _dirListing = new Object();
|
||||
|
||||
for await (const entry of dir_ref.values()) {
|
||||
if (entry.kind == "file" && entry.name.match(/\.[0-9a-z]+$/i) == fileExtension) {
|
||||
var details = await entry.getFile();
|
||||
details['nameNoExt'] = entry.name.replace(/\.[^/.]+$/, "");
|
||||
if (skipAgeCheck) {
|
||||
_dirListing[entry.name] = details;
|
||||
}
|
||||
else {
|
||||
let fileAgeMin = getFileAgeMin(details.lastModifiedDate);
|
||||
if (fileAgeMin <= FileNotOlderThanMin) {
|
||||
_dirListing[entry.name] = details;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return _dirListing;
|
||||
}
|
||||
|
||||
function findModifiedFile(_dirListing) {
|
||||
var changed = new Object();
|
||||
|
||||
Object.keys(_dirListing).forEach(name => {
|
||||
var obj = _dirListing[name];
|
||||
console.log("\n" + name + ": ");
|
||||
|
||||
var _modificationTime = obj.lastModifiedDate;
|
||||
var _size = obj.size;
|
||||
|
||||
if (dirListing.hasOwnProperty(name)) {
|
||||
const modificationTime = dirListing[name].lastModifiedDate;
|
||||
const sizeBytes = dirListing[name].size;
|
||||
//console.log("\t-> " + _modificationTime + " vs " + modificationTime);
|
||||
|
||||
console.log("\t-> age: " + getFileAgeMin(modificationTime) + " minutes");
|
||||
console.log("\t-> size: " + formatBytes(sizeBytes));
|
||||
if (_modificationTime.getTime() === modificationTime.getTime()) {
|
||||
console.log("\t-> no changes");
|
||||
}
|
||||
else {
|
||||
console.log("\t-> changed!");
|
||||
// FileUpload(obj);
|
||||
changed[name] = obj;
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log("\t-> indexing...");
|
||||
}
|
||||
|
||||
dirListing[name] = _dirListing[name];
|
||||
});
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
function formatBytes(a, b = 2) {
|
||||
if (0 === a) return "0 Bytes";
|
||||
const c = 0 > b ? 0 : b,
|
||||
d = Math.floor(Math.log(a) / Math.log(1024));
|
||||
return parseFloat((a / Math.pow(1024, d)).toFixed(c)) + " " + ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"][d]
|
||||
}
|
||||
|
||||
function FileUpload(changed) {
|
||||
if (Object.keys(changed).length === 0 && changed.constructor === Object) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("\n" + '-'.repeat(40) + "\nFILE UPLOAD\n\n");
|
||||
|
||||
// changed.forEach(function(obj) {
|
||||
Object.keys(changed).forEach(name => {
|
||||
let obj = changed[name];
|
||||
var uploadURI = "https://zinomedia.de/pkrstarsbot/upload.php?name=" + escape(obj.nameNoExt) + "&UserID=" + elements.db_user_id;
|
||||
const reader = new FileReader();
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
console.log(obj.name + ":");
|
||||
if (obj.size > filesizeLimitBytes) {
|
||||
console.log("\t-> skipping upload: file is larger than " + filesizeLimitBytes);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("\t-> uploadURI: " + uploadURI);
|
||||
|
||||
xhr.upload.addEventListener("load", function(e) {
|
||||
console.log("\t-> Upload complete");
|
||||
}, false);
|
||||
xhr.open("POST", uploadURI, true);
|
||||
reader.onload = function(evt) {
|
||||
xhr.send(evt.target.result);
|
||||
};
|
||||
reader.readAsText(obj);
|
||||
});
|
||||
}
|
||||
29
www/client_webapp/src/upload.php
Executable file
29
www/client_webapp/src/upload.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
// $debug = "/var/www/pkrstarsbot/debug";
|
||||
// foreach($_GET as $key => $value)
|
||||
// {
|
||||
// file_put_contents($debug, "$key=$value", FILE_APPEND);
|
||||
// }
|
||||
|
||||
// ARGS
|
||||
if (!isset($_GET['UserID']) OR !isset($_GET['name'])) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// file_put_contents($debug, 'pass', FILE_APPEND);
|
||||
$UserID = $_GET['UserID'];
|
||||
$name = $_GET['name'];
|
||||
}
|
||||
|
||||
// VARS
|
||||
$UsernameDir = "/home/zino/projects/pkrstarsbot/server/upload/$UserID";
|
||||
$file = "$UsernameDir/$name.txt";
|
||||
|
||||
// Create UsernameDir if not existent
|
||||
if (!file_exists($UsernameDir)) {
|
||||
mkdir($UsernameDir, 0777, false);
|
||||
}
|
||||
|
||||
// WRITE FILE TO USERS DIR
|
||||
file_put_contents($file, file_get_contents('php://input'));
|
||||
?>
|
||||
Reference in New Issue
Block a user