-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
153 lines (131 loc) · 5.09 KB
/
server.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const passportSetup = require('./config/passport-setup');
const authRoutes = require('./routes/auth');
const multer = require('multer');
const fs = require('fs');
const {exec} = require('child_process');
const cookieSession = require('cookie-session');
const cors = require('cors'); // Make sure to install with npm install cors
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const port = 3010;
app.use(cors()); // Use CORS to avoid cross-origin issues
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json()); // Parse JSON bodies
// Connect to MongoDB
mongoose.connect(process.env.MONGO_DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to MongoDB:', err));
// Set up Multer for handling file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadPath = path.join(__dirname, 'audio/', );
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
}
console.log('Upload path:', uploadPath);
console.log(file.originalname);
cb(null, uploadPath);
},
filename: (req, file, cb) => {
cb(null, "not-translated.wav");
},
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
const allowedFileTypes = ['.wav'];
const fileExt = path.extname(file.originalname).toLowerCase();
if (allowedFileTypes.includes(fileExt)) {
cb(null, true);
} else {
print(file.path);
cb(new Error('Invalid file type. Only WAV files are allowed.'));
}
},
});
app.post('/upload', upload.single('file'), (req, res) => {
try {
if (!req.file) {
throw new Error('No file uploaded.');
}
const filePath = path.join(__dirname, 'audio', "not-translated.wav");
console.log('dddd');
console.log('File path:', filePath);
//const fileContent = fs.readFileSync(filePath, 'utf-8');
//console.log(`File content: ${fileContent}`);
console.log('dddd');
exec('node translate.js ' + filePath + ' ' + filePath.replace(".", "_translated."), (error, stdout, stderr) => {
if (error) {
console.error(`Error executing TheShit.js: ${error}`);
return res.status(500).json({ success: false, message: 'Error executing TheShit.js.' });
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
// res.json({ success: true, message: 'File uploaded and processed successfully!' });
// webScrapeExec();
});
}
catch (error) {
console.error('Error processing file:', error.message);
var error = "Error processing file: " + error.message
// res.send(<script>alert("You need to upload a correct error!"); window.location.href = "/page_location"; </script>);
// alert("'Error processing fileefef: ' + error.message ");
res.status(500).json({ success: false, message: 'Error processing fileefef: ' + error.message });
}
});
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [process.env.SESSION_COOKIE_KEY]
}));
// Initialize passport
app.use(passport.initialize());
app.use(passport.session());
// Set up routes
app.use('/api', authRoutes);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'Login.html'));
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
// Streaming wav file
app.get('/stream', (req, res) => {
const file = __dirname + '/audio/not-translated_translated.wav';
const stat = fs.statSync(file);
const total = stat.size;
if (req.headers.range) {
}
fs.exists(file, (exists) => {
if (exists) {
const range = req.headers.range;
const parts = range.replace(/bytes=/, '').split('-');
const partialStart = parts[0];
const partialEnd = parts[1];
const start = parseInt(partialStart, 10);
const end = partialEnd ? parseInt(partialEnd, 10) : total - 1;
const chunksize = (end - start) + 1;
const rstream = fs.createReadStream(file, {start: start, end: end});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes', 'Content-Length': chunksize,
'Content-Type': 'audio/wav'
});
rstream.pipe(res);
} else {
res.send('Error - 404');
res.end();
// res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'audio/mpeg' });
// fs.createReadStream(path).pipe(res);
}
});
});
app.get('/download', (req, res) => {
const file = __dirname + '/audio/not-translated_translated.wav';
res.download(file);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));