⬇CSV Import work #808
Some checks are pending
docker / docker_dev (push) Waiting to run

This commit is contained in:
jokob-sk
2024-09-30 10:30:09 +10:00
parent e5d835cfa9
commit 044de61ab5
6 changed files with 45 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
## Development environemnt set up
## Development environment set up
>[!NOTE]
> Replace `/development` with the path where your code files will be stored. The default container name is `netalertx` so there might be a conflict with your running containers.
@@ -52,13 +52,19 @@ A command to stop, remove the container and the image (replace `netalertx` and `
- `sudo docker container stop netalertx ; sudo docker container rm netalertx ; sudo docker image rm netalertx-netalertx`
### Restart hanging python script
### Restart the server backend
SSH into the container and kill & restart the main script loop
Most code changes can be tetsed without rebuilding the container. When working on the python server backend, you only need to restart the server.
1. You can usually restart the backend via Maintenance > Logs > Restart server
![image](/docs/img/DEV_ENV_SETUP/Maintenance_Logs_Restart_server.png)
2. If above doesn't work, SSH into the container and kill & restart the main script loop
- `sudo docker exec -it netalertx /bin/bash`
- `pkill -f "python /app/server" && python /app/server & `
3. If none of the above work, restart the docker image. This is usually the last resort as sometimes the Docker engine becomes unresponsive and the whole engine needs to be restarted.

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -342,6 +342,8 @@ function getLangCode() {
// -----------------------------------------------------------------------------
// String utilities
// -----------------------------------------------------------------------------
// ----------------------------------------------------
function jsonSyntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
@@ -364,6 +366,7 @@ function jsonSyntaxHighlight(json) {
});
}
// ----------------------------------------------------
function isValidBase64(str) {
// Base64 characters set
var base64CharacterSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -373,7 +376,7 @@ function isValidBase64(str) {
return invalidCharacters === '';
}
// ----------------------------------------------------
function isValidJSON(jsonString) {
try {
JSON.parse(jsonString);
@@ -383,6 +386,7 @@ function isValidJSON(jsonString) {
}
}
// ----------------------------------------------------
// method to sanitize input so that HTML and other things don't break
function encodeSpecialChars(str) {
return str
@@ -392,7 +396,7 @@ function encodeSpecialChars(str) {
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// ----------------------------------------------------
function decodeSpecialChars(str) {
return str
.replace(/&/g, '&')
@@ -402,6 +406,16 @@ function decodeSpecialChars(str) {
.replace(/'/g, '\'');
}
// ----------------------------------------------------
// base64 conversion of UTF8 chars
function utf8ToBase64(str) {
// Convert the string to a Uint8Array using TextEncoder
const utf8Bytes = new TextEncoder().encode(str);
// Convert the Uint8Array to a base64-encoded string
return btoa(String.fromCharCode(...utf8Bytes));
}
// -----------------------------------------------------------------------------
// General utilities

View File

@@ -473,12 +473,15 @@ function askImportPastedCSV() {
function ImportPastedCSV()
{
var csv = $('#modal-input-textarea').val();
csvBase64 = btoa(csv)
// Execute
csvBase64 = utf8ToBase64(csv);
$.post('php/server/devices.php?action=ImportCSV', { content: csvBase64 }, function(msg) {
showMessage(msg);
write_notification(`[Maintenance] Devices imported from pasted content`, 'info');
showMessage(msg);
write_notification(`[Maintenance] Devices imported from pasted content`, 'info');
});
}

View File

@@ -476,7 +476,11 @@ function ImportCSV() {
if(isset ($_POST['content']) && !empty ($_POST['content']))
{
// Decode the Base64 string
$data = base64_decode($_POST['content']);
// $data = base64_decode($_POST['content']);
$data = base64_decode($_POST['content'], true); // The second parameter ensures safe decoding
// // Ensure the decoded data is treated as UTF-8 text
// $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
} else if (file_exists($file)) { // try to get the data form the file
@@ -488,6 +492,12 @@ function ImportCSV() {
if($data != "")
{
// data cleanup - new lines breaking the CSV
$data = preg_replace_callback('/"([^"]*)"/', function($matches) {
// Replace all \n within the quotes with a space
return str_replace("\n", " ", $matches[0]); // Replace with a space
}, $data);
$lines = explode("\n", $data);
// Get the column headers from the first line of the CSV

View File

@@ -25,7 +25,7 @@ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https:
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$isLogonPage = strpos($url, 'index.php') !== false;
$authHeader = apache_request_headers()['Authorization'] ?? '';
$sessionLogin = $_SESSION['login'] ?? 0;
$sessionLogin = isset($_SESSION['login']) ? $_SESSION['login'] : 0;
// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {