Skip to content

Commit 38be6dd

Browse files
committed
feat: implement websocket-based uploads
eliminating the need for PHP/PHP-FPM.
1 parent 86e8366 commit 38be6dd

File tree

4 files changed

+45
-56
lines changed

4 files changed

+45
-56
lines changed

.setup/gateway

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,12 @@ server{
6363
proxy_set_header X-Forwarded-User $remote_user;
6464
}
6565

66-
# enable and configure PHP
67-
location ~* \.php$ {
68-
fastcgi_split_path_info ^(.+\.php)(.*)$;
69-
70-
# typically the fpm.sock is used, but check to make sure by using:
71-
# sudo grep -ri "listen = " /etc/php
72-
# should get something like:
73-
# /etc/php/7.3/fpm/pool.d/www.conf:listen = /run/php/php7.3-fpm.sock
74-
# then you know it's fpm.sock
75-
fastcgi_pass unix:/run/php/PHPFPMSOCK; #uncomment if PHP runs on fpm.sock
76-
#fastcgi_pass 127.0.0.1:9000; #uncomment if PHP is running on port 9000
77-
78-
fastcgi_index index.php;
79-
fastcgi_param SCRIPT_FILENAME /home/pi/gateway/www$fastcgi_script_name;
80-
fastcgi_param QUERY_STRING $query_string;
81-
fastcgi_param REQUEST_METHOD $request_method;
82-
fastcgi_param CONTENT_TYPE $content_type;
83-
fastcgi_param CONTENT_LENGTH $content_length;
84-
include fastcgi_params;
85-
}
86-
8766
# redirect server error pages to the static page /50x.html
8867
error_page 500 502 503 504 /50x.html;
8968
location = /50x.html {
9069
root html;
9170
}
92-
71+
9372
## Disable viewing .htaccess & .htpassword
9473
location ~ /\.ht {
9574
deny all;

.setup/gatewaysetup.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ sudo apt-get -y install git apache2-utils
108108
echo -e "${CYAN}************* STEP: Install latest NGINX *************${NC}"
109109
sudo apt-get -y install nginx
110110

111-
echo -e "${CYAN}************* STEP: Install latest PHP *********************${NC}"
112-
sudo apt-get -y install php-common php-cli php-fpm
113-
114111
echo -e "${CYAN}************* STEP: Install nodeJS & npm *************${NC}"
115112
#install latest NodeJS --- https://www.raspberrypi.org/forums/viewtopic.php?t=141770
116113
sudo wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | sudo bash
@@ -179,10 +176,6 @@ echo -e "${YLW}Done. You can add/change http_auth credentials using ${RED}htpass
179176

180177
echo -e "${CYAN}************* STEP: Copy gateway site config to sites-available *************${NC}"
181178
cp -rf $APPSRVDIR/.setup/gateway /etc/nginx/sites-available/gateway
182-
#determine php-fpm version and replace in gateway site config
183-
phpfpmsock=$(grep -ri "listen = /" /etc/php) #search for file containing "listen =" in php path
184-
phpfpmsock=${phpfpmsock##*/} #extract everything after last /
185-
sudo sed -i "s/PHPFPMSOCK/${phpfpmsock}/g" /etc/nginx/sites-available/gateway #replace PHPFPMSOCK with it in site config file
186179
cd /etc/nginx/sites-enabled
187180
sudo rm /etc/nginx/sites-enabled/default
188181
sudo ln -s /etc/nginx/sites-available/gateway

gateway.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,25 @@ io.sockets.on('connection', function (socket) {
777777
console.log('PI SHUTDOWN REQUESTED from ' + address);
778778
require('child_process').exec('sudo /sbin/shutdown now "GATEWAY SHUTDOWN REQUEST"', function (msg) { console.log(msg) });
779779
});
780+
781+
// https://socket.io/how-to/upload-a-file
782+
socket.on('UPLOADIMAGE', function (name, buffer, callback) {
783+
if (!name) {
784+
callback(null, "missing file name");
785+
return;
786+
}
787+
788+
if (!buffer) {
789+
callback(null, "missing file data");
790+
return;
791+
}
792+
793+
target = path.join(userImagesDir, name);
794+
795+
fs.writeFile(target, buffer, function(err) {
796+
callback(name, err);
797+
});
798+
});
780799
});
781800

782801
//entries should contain the node list and also a node that contains the order (if this was ever added)

www/index.html

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,35 +2285,33 @@ <h2>' + (nodeResolveString(node.label||'', node.metrics) || node._id) + ' ' + re
22852285
$("#changeNodeIcon_OK").click("tap", function(event) {
22862286
if ($("#nodeIconUpload").val()) {
22872287
event.preventDefault();
2288-
var file_data = $('#nodeIconUpload').prop('files')[0];
2289-
var form_data = new FormData();
2290-
form_data.append('file', file_data);
2291-
$.ajax({
2292-
type: "POST",
2293-
url: "upload.php",
2294-
enctype: 'multipart/form-data',
2295-
dataType: 'json',
2296-
cache: false,
2297-
contentType: false,
2298-
processData: false,
2299-
data: form_data,
2300-
success: function (data, textStatus, jqXHR) {
2301-
data['status'];
2302-
if (data['status']=='success')
2288+
2289+
var file = $('#nodeIconUpload').prop('files')[0];
2290+
2291+
var types = ['image/jpeg', 'image/png', 'image/gif'];
2292+
if (!types.includes(file.type)) {
2293+
alert('Invalid file type; only JPG/JPEG/PNG/GIF are allowed');
2294+
return;
2295+
}
2296+
2297+
if ((file.size || 0) > 500000) {
2298+
alert('File too large (>500K)');
2299+
return;
2300+
}
2301+
2302+
socket.emit('UPLOADIMAGE', file.name, file, function(filePath, err) {
2303+
if (err) {
2304+
alert('Upload failed: ' + err.message);
2305+
}
2306+
else {
2307+
if (filePath)
23032308
{
2304-
if (data['filePath'])
2305-
{
2306-
$('#nodeDetailImage').attr('src', 'images/'+data['filePath']);
2307-
nodes[selectedNodeId].icon = data['filePath'];
2308-
socket.emit('REFRESHICONS');
2309-
$.mobile.navigate('#nodedetails');
2310-
}
2311-
else alert('Expected filePath back, got nothing');
2309+
$('#nodeDetailImage').attr('src', 'images/'+filePath);
2310+
nodes[selectedNodeId].icon = filePath;
2311+
socket.emit('REFRESHICONS');
2312+
$.mobile.navigate('#nodedetails');
23122313
}
2313-
else alert(data['message']||'Upload failed');
2314-
},
2315-
error: function (data, textStatus, msg) {
2316-
alert(msg||'Upload failed');
2314+
else alert('Expected filePath back, got nothing');
23172315
}
23182316
});
23192317
}

0 commit comments

Comments
 (0)