-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnativeEndpoint.js
54 lines (46 loc) · 2.01 KB
/
nativeEndpoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs');
const path = require('path');
function addNativeEndpoint(app) {
// Does not work for app.listen - it saves a file but it is adding extra characters
// For functions-framework it hangs never returning any response
app.post('/upload/native', (req, res) => {
console.log(req.headers)
if (req.headers['content-type'] && req.headers['content-type'].startsWith('multipart/form-data')) {
let body = '';
const boundary = req.headers['content-type'].split('boundary=')[1];
let fileName = '';
let fileData = '';
let isFile = false;
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const parts = body.split(`--${boundary}`);
for (let part of parts) {
if (part.includes('filename=')) {
fileName = part.match(/filename="(.+)"/)[1];
isFile = true;
}
if (isFile && part.includes('\r\n\r\n')) {
fileData = part.split('\r\n\r\n')[1].trim();
break;
}
}
if (!fileData) {
return res.status(400).send('No file uploaded.');
}
const targetPath = path.join(__dirname, 'uploads', 'file_native.jpg');
fs.writeFile(targetPath, fileData, 'binary', (err) => {
if (err) {
console.error('Error saving file:', err);
return res.status(500).send('Error saving file.');
}
res.send('File uploaded and saved successfully');
});
});
} else {
res.status(400).send('Invalid content type. Expected multipart/form-data.');
}
});
}
module.exports = { addNativeEndpoint };