mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
add standalone test for web uploading - https://github.com/emsesp/EMS-ESP32/issues/1564
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
[*.{js,json,yml}]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
4
mock-api/.gitattributes
vendored
4
mock-api/.gitattributes
vendored
@@ -1,4 +0,0 @@
|
||||
/.yarn/** linguist-vendored
|
||||
/.yarn/releases/* binary
|
||||
/.yarn/plugins/**/* binary
|
||||
/.pnp.* binary linguist-generated
|
||||
9
mock-api/.gitignore
vendored
9
mock-api/.gitignore
vendored
@@ -1,9 +0,0 @@
|
||||
.pnp.*
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
uploads/*
|
||||
!uploads/README.md
|
||||
@@ -64,4 +64,4 @@ rest_server.get(ES_LOG_ENDPOINT, (_req, res) => {
|
||||
});
|
||||
|
||||
// start eventsource server
|
||||
rest_server.listen(port, () => console.log(`EMS-ESP EventSource server running on http://localhost:${port}/`));
|
||||
rest_server.listen(port, () => console.log(`EMS-ESP EventSource mock server running on http://localhost:${port}/`));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,13 +6,14 @@
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"mock-api": "bun --watch rest_server.ts",
|
||||
"mock-es": "bun --watch es_server.ts"
|
||||
"mock-es": "bun --watch es_server.ts",
|
||||
"mock-upload": "bun --watch upload_server.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@msgpack/msgpack": "^2.8.0",
|
||||
"compression": "^1.7.4",
|
||||
"express": "^4.19.2",
|
||||
"itty-router": "^5.0.5",
|
||||
"itty-router": "^5.0.9",
|
||||
"multer": "^1.4.5-lts.1"
|
||||
},
|
||||
"packageManager": "yarn@4.1.1",
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
import { AutoRouter, error, status } from 'itty-router';
|
||||
import { Encoder } from '@msgpack/msgpack';
|
||||
|
||||
// import busboy from 'busboy';
|
||||
// import multer from 'multer';
|
||||
// const upload = multer({ dest: '../mock-api/uploads' });
|
||||
|
||||
const encoder = new Encoder();
|
||||
|
||||
const router = AutoRouter({
|
||||
port: 3080,
|
||||
missing: () => error(404, 'Error, not found')
|
||||
missing: () => error(404, 'Error, endpoint not found')
|
||||
});
|
||||
|
||||
const REST_ENDPOINT_ROOT = '/rest/';
|
||||
@@ -24,17 +20,6 @@ const headers = {
|
||||
// GLOBAL VARIABLES
|
||||
let countWifiScanPoll = 0; // wifi network scan
|
||||
|
||||
// FUNCTIONS
|
||||
// const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
||||
// function delay_blocking(milliseconds) {
|
||||
// var start = new Date().getTime();
|
||||
// for (var i = 0; i < 1e7; i++) {
|
||||
// if (new Date().getTime() - start > milliseconds) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
function updateMask(entity: any, de: any, dd: any) {
|
||||
const current_mask = parseInt(entity.slice(0, 2), 16);
|
||||
|
||||
@@ -364,7 +349,6 @@ const ESPSYSTEM_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'ESPSystemStatus';
|
||||
const SECURITY_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'securitySettings';
|
||||
const RESTART_ENDPOINT = REST_ENDPOINT_ROOT + 'restart';
|
||||
const FACTORY_RESET_ENDPOINT = REST_ENDPOINT_ROOT + 'factoryReset';
|
||||
const UPLOAD_FILE_ENDPOINT = REST_ENDPOINT_ROOT + 'uploadFile';
|
||||
|
||||
// SYSTEM SIGNIN
|
||||
const VERIFY_AUTHORIZATION_ENDPOINT = REST_ENDPOINT_ROOT + 'verifyAuthorization';
|
||||
@@ -2394,107 +2378,9 @@ router
|
||||
.get(VERIFY_AUTHORIZATION_ENDPOINT, () => verify_authentication)
|
||||
.post(RESTART_ENDPOINT, () => status(200))
|
||||
.post(FACTORY_RESET_ENDPOINT, () => status(200))
|
||||
.post(UPLOAD_FILE_ENDPOINT, () => status(404)) // TODO remove upload when fixed
|
||||
.post(SIGN_IN_ENDPOINT, () => signin)
|
||||
.get(GENERATE_TOKEN_ENDPOINT, () => generate_token);
|
||||
|
||||
// uploads // TODO fix uploading later
|
||||
|
||||
// const progress_middleware = async (req: any) => {
|
||||
// console.log('progress_middleware');
|
||||
// let progress = 0;
|
||||
// const file_size = req.headers['content-length'];
|
||||
|
||||
// // set event listener
|
||||
// req.on('data', async (chunk) => {
|
||||
// progress += chunk.length;
|
||||
// const percentage = (progress / file_size) * 100;
|
||||
// console.log(`Progress: ${Math.round(percentage)}%`);
|
||||
// delay_blocking(200); // slow it down
|
||||
// });
|
||||
// // next(); // invoke next middleware which is multer
|
||||
// };
|
||||
|
||||
// const withContent = async (request) => {
|
||||
// const { headers } = request;
|
||||
// const type = headers.get('content-type');
|
||||
|
||||
// // console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(request)));
|
||||
|
||||
// if (type?.includes('form-data')) {
|
||||
// console.log('withContent: got formdata');
|
||||
// // request.content = await request.formData();
|
||||
|
||||
// // const bb = busboy({ headers: request.headers });
|
||||
// // console.log('bb created');
|
||||
// // bb.on('file', (name, file, info) => {
|
||||
// // const { filename, encoding, mimeType } = info;
|
||||
// // console.log(`File [${name}]: filename: %j, encoding: %j, mimeType: %j`, filename, encoding, mimeType);
|
||||
// // request.filename = filename;
|
||||
|
||||
// // file
|
||||
// // .on('data', (data) => {
|
||||
// // console.log(`File [${name}] got ${data.length} bytes`);
|
||||
// // })
|
||||
// // .on('close', () => {
|
||||
// // console.log(`File [${name}] done`);
|
||||
// // });
|
||||
// // });
|
||||
// // bb.on('field', (name, val, info) => {
|
||||
// // console.log(`Field [${name}]: value: %j`, val);
|
||||
// // });
|
||||
// // bb.on('close', () => {
|
||||
// // console.log('Done parsing form!');
|
||||
// // // res.writeHead(303, { Connection: 'close', Location: '/' });
|
||||
// // // res.end();
|
||||
// // });
|
||||
// }
|
||||
// };
|
||||
|
||||
// const makeMiddleware = (req) => {
|
||||
// console.log('makeMiddleware');
|
||||
// // const bb = busboy({ headers: req.headers });
|
||||
|
||||
// // bb.on('error', (err) => {
|
||||
// // // Send this error along to the global error handler
|
||||
// // console.log('Error' + err);
|
||||
// // return;
|
||||
// // });
|
||||
// // bb.on('file', (name, file, info) => {
|
||||
// // const { filename, encoding, mimeType } = info;
|
||||
// // console.log(`File [${name}]: filename: %j, encoding: %j, mimeType: %j`, filename, encoding, mimeType);
|
||||
// // req.filename = filename;
|
||||
|
||||
// // file
|
||||
// // .on('data', (data) => {
|
||||
// // console.log(`File [${name}] got ${data.length} bytes`);
|
||||
// // })
|
||||
// // .on('close', () => {
|
||||
// // console.log(`File [${name}] done`);
|
||||
// // });
|
||||
// // });
|
||||
// // bb.end(req.rawBody);
|
||||
// // req.pipe(bb);
|
||||
// };
|
||||
|
||||
// router.post(UPLOAD_FILE_ENDPOINT, withContent, makeMiddleware, progress_middleware, ({ filename }) => {
|
||||
// console.log('filename: ' + filename);
|
||||
|
||||
// // if (req.file) {
|
||||
// // const filename = req.file.originalname;
|
||||
// // const ext = filename.substring(filename.lastIndexOf('.') + 1);
|
||||
// // console.log(req.file);
|
||||
// // console.log('ext: ' + ext);
|
||||
|
||||
// // if (ext === 'bin' || ext === 'json') {
|
||||
// // return res.sendStatus(200);
|
||||
// // } else if (ext === 'md5') {
|
||||
// // return res.json({ md5: 'ef4304fc4d9025a58dcf25d71c882d2c' });
|
||||
// // }
|
||||
// // }
|
||||
// return new Response('OK', { status: 200 });
|
||||
// });
|
||||
|
||||
//
|
||||
// EMS-ESP Project stuff
|
||||
//
|
||||
|
||||
57
mock-api/upload_server.ts
Normal file
57
mock-api/upload_server.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import express from 'express';
|
||||
import multer from 'multer';
|
||||
|
||||
const rest_server = express();
|
||||
const port = 3082;
|
||||
const upload = multer({ dest: './uploads' });
|
||||
|
||||
const UPLOAD_FILE_ENDPOINT = '/rest/uploadFile';
|
||||
|
||||
// delay functions, 2 different types
|
||||
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
||||
|
||||
function delay_blocking(milliseconds) {
|
||||
var start = new Date().getTime();
|
||||
// for (var i = 0; i < 1e7; i++) {
|
||||
while (true) {
|
||||
if (new Date().getTime() - start > milliseconds) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function progress_middleware(req, res, next) {
|
||||
let progress = 0;
|
||||
const file_size = req.headers['content-length'];
|
||||
console.log('Uploading file. Size ' + file_size + ' bytes');
|
||||
|
||||
// set event listener
|
||||
req.on('data', async (chunk) => {
|
||||
progress += chunk.length;
|
||||
const percentage = (progress / file_size) * 100;
|
||||
console.log(`Progress: ${Math.round(percentage)}%`);
|
||||
// await delay(1000); // slow it down
|
||||
delay_blocking(1000); // slow it down
|
||||
});
|
||||
next(); // invoke next middleware which is multer
|
||||
}
|
||||
|
||||
rest_server.post(UPLOAD_FILE_ENDPOINT, progress_middleware, upload.single('file'), (req, res) => {
|
||||
if (req.file) {
|
||||
const filename = req.file.originalname;
|
||||
const ext = filename.substring(filename.lastIndexOf('.') + 1);
|
||||
console.log(req.file);
|
||||
|
||||
if (ext === 'bin' || ext === 'json') {
|
||||
console.log('Received firmware or json file, extension: ' + ext);
|
||||
return res.sendStatus(200);
|
||||
} else if (ext === 'md5') {
|
||||
return res.json({ md5: 'ef4304fc4d9025a58dcf25d71c882d2c' });
|
||||
}
|
||||
}
|
||||
console.log('Invalid file extension');
|
||||
return res.sendStatus(400);
|
||||
});
|
||||
|
||||
// start server
|
||||
rest_server.listen(port, () => console.log(`EMS-ESP File Upload mock server running on http://localhost:${port}/`));
|
||||
@@ -140,7 +140,7 @@ __metadata:
|
||||
"@types/multer": "npm:^1.4.11"
|
||||
compression: "npm:^1.7.4"
|
||||
express: "npm:^4.19.2"
|
||||
itty-router: "npm:^5.0.5"
|
||||
itty-router: "npm:^5.0.9"
|
||||
multer: "npm:^1.4.5-lts.1"
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
@@ -526,10 +526,10 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"itty-router@npm:^5.0.5":
|
||||
version: 5.0.5
|
||||
resolution: "itty-router@npm:5.0.5"
|
||||
checksum: 10/b10ddeb65568e4ed5eb5a99b5e5e660bae62d53ab88ffdf56a9045d6323a87f561a000f3d8835dcd15451c565fd01083e049122e46d55ffd24fa675f9446971c
|
||||
"itty-router@npm:^5.0.9":
|
||||
version: 5.0.9
|
||||
resolution: "itty-router@npm:5.0.9"
|
||||
checksum: 10/b11684cfcb08658620d878ad1a62bcd140491909610d48bd3b83e278cd898acfa9fcd63af126c6705647a5c69911f82f5a54bb2de8dc2ea395dbaf2bcb6faf45
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user