Skip to content

Commit

Permalink
Prettier fix
Browse files Browse the repository at this point in the history
  • Loading branch information
milo04011 committed Nov 6, 2024
1 parent 400c80d commit 3fede23
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 163 deletions.
5 changes: 2 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const multer = require('multer');
const fs = require('fs');
const upload = multer({ dest: 'uploads/' });


const app = express();

app.use(bodyParser.json());
Expand All @@ -22,7 +21,7 @@ const kinestheticRoutes = require('./routes/kinesthetic');
app.use('/api/kinesthetic', kinestheticRoutes);

const auditoryRoutes = require('./routes/text-to-speech');
app.use('/api/text-to-speech', auditoryRoutes);
app.use('/api/text-to-speech', auditoryRoutes);

app.get('/', function (request, response) {
// response.send("testing");
Expand All @@ -39,7 +38,7 @@ app.post('/upload', upload.single('file'), function (request, response) {
return response.status(400).json({ error: 'No file uploaded' });
}

fs.readFile(request.file.path, 'utf-8',(err, data) => {
fs.readFile(request.file.path, 'utf-8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return response.status(500).json({ error: 'Error reading file' });
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
dockerfile: Dockerfile
ports:
- '3000:3000'
env_file:
env_file:
- .env
environment:
- EMAIL_USER=${EMAIL_USER}
Expand Down
74 changes: 37 additions & 37 deletions public/js/auditory.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fileInput = document.getElementById('fileInput');
const uploadButton = document.getElementById('uploadButton');
const submitButton = document.getElementById('submitButton');
const uploadStatus = document.getElementById('uploadStatus');
const auditoryFileOutput = document.getElementById('auditoryFileOutput');
const uploadStatus = document.getElementById('uploadStatus');
const auditoryFileOutput = document.getElementById('auditoryFileOutput');
const recommendationBox = document.getElementById('recommendationBox');

let fileContent = '';
Expand All @@ -12,62 +12,62 @@ uploadButton.addEventListener('click', () => {
});

fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file && file.type == 'text/plain') {
const reader = new FileReader();

reader.onload = (event) => {
fileContent = event.target.result;
const file = event.target.files[0];
if (file && file.type == 'text/plain') {
const reader = new FileReader();

uploadStatus.textContent = `File uploaded: ${file.name} of size ${(file.size / 1024).toFixed(2)} KB`;
uploadStatus.classList.remove('error');
uploadStatus.classList.add('success');
};

reader.readAsText(file);
} else {
uploadStatus.textContent = 'Please upload a valid .txt file.';
uploadStatus.classList.add('error');
uploadStatus.classList.remove('success');
}
});
reader.onload = (event) => {
fileContent = event.target.result;

uploadStatus.textContent = `File uploaded: ${file.name} of size ${(file.size / 1024).toFixed(2)} KB`;
uploadStatus.classList.remove('error');
uploadStatus.classList.add('success');
};

reader.readAsText(file);
} else {
uploadStatus.textContent = 'Please upload a valid .txt file.';
uploadStatus.classList.add('error');
uploadStatus.classList.remove('success');
}
});

submitButton.addEventListener('click', async () => {
try {
try {
if (fileContent === '') {
alert('Please upload a file before submitting!');
return;
alert('Please upload a file before submitting!');
return;
}

recommendationBox.innerHTML = `<p>Loading...</p>`;

alert('File upload successful!');

const response = await fetch('/api/text-to-speech', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: fileContent }),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: fileContent }),
});

if (!response.ok) {
throw new Error('Network response was not ok');
throw new Error('Network response was not ok');
}

const data = await response.json();

console.log(data);
} catch (error) {
} catch (error) {
console.log('Error occurred', error);
}
}
});

auditoryFileOutput.addEventListener('click', () => {
if (fileContent === '') {
alert('Please upload a file before trying to download!');
return;
}
if (fileContent === '') {
alert('Please upload a file before trying to download!');
return;
}

window.location.href = '/api/text-to-speech/download';
});
window.location.href = '/api/text-to-speech/download';
});
2 changes: 1 addition & 1 deletion public/visual.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ document.addEventListener('DOMContentLoaded', function () {
const outputText = document.getElementById('output-text');

uploadButton.addEventListener('click', function () {
fileInput.click();
fileInput.click();
});

fileInput.addEventListener('change', function () {
Expand Down
58 changes: 29 additions & 29 deletions routes/text-to-speech.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ const axios = require('axios');

let outputFile = 'audioFile.mp3';

router.post('/', async(req, res) => {
try {
const { text } = req.body;

const language = 'en';
const url = await tts.getAudioUrl(text, {
lang: language,
slow: false,
host: 'https://translate.google.com',
});

const response = await axios.get(url, { responseType: 'arraybuffer' });

fs.writeFileSync(outputFile, Buffer.from(response.data));
} catch (error) {
console.error('Encountered an error!', error);
}
});
router.post('/', async (req, res) => {
try {
const { text } = req.body;
console.log(res);

const language = 'en';
const url = await tts.getAudioUrl(text, {
lang: language,
slow: false,
host: 'https://translate.google.com',
});

router.get('/download', (req, res) => {
if (!fs.existsSync(outputFile)) {
return res.status(404).send('File not found');
}
const response = await axios.get(url, { responseType: 'arraybuffer' });

res.download(outputFile, (err) => {
if (err) {
console.error('Error downloading file:', err);
res.status(500).send('Error downloading file');
}
});
fs.writeFileSync(outputFile, Buffer.from(response.data));
} catch (error) {
console.error('Encountered an error!', error);
}
});

router.get('/download', (req, res) => {
if (!fs.existsSync(outputFile)) {
return res.status(404).send('File not found');
}

res.download(outputFile, (err) => {
if (err) {
console.error('Error downloading file:', err);
res.status(500).send('Error downloading file');
}
});
});

module.exports = router;
module.exports = router;
21 changes: 16 additions & 5 deletions views/auditory.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ <h1 class="title">Auditory</h1>
</div>

<div class="input-box">
<input type="file" id="fileInput" accept=".txt" style="display: none" />
<button class="button-style" id="uploadButton">Click to Upload File</button>
<input
type="file"
id="fileInput"
accept=".txt"
style="display: none"
/>
<button class="button-style" id="uploadButton">
Click to Upload File
</button>
<p class="uploadStatus" id="uploadStatus"></p>
<button class="button-style" id="submitButton">Submit</button>
<button class="button-style" id="submitButton">Submit</button>
</div>
</div>
</section>

<section class="auditory-output">
<div class="output-box">
<button class="button-style" id="auditoryFileOutput">Click to Download Audio File</button>
<button class="button-style" id="auditoryFileOutput">
Click to Download Audio File
</button>
</div>
</section>

Expand Down Expand Up @@ -106,7 +115,9 @@ <h2>Get in Touch</h2>
placeholder="Your Message"
required
></textarea>
<button id="contact-btn" type="submit" class="cta-button">Send Message</button>
<button id="contact-btn" type="submit" class="cta-button">
Send Message
</button>
</form>
</div>
</section>
Expand Down
Loading

0 comments on commit 3fede23

Please sign in to comment.