Added cron job, updated webserver, added retry functionality

This commit is contained in:
Myx
2023-10-13 02:42:04 +02:00
parent e3f9dc2899
commit d25d2c8f54
7 changed files with 219 additions and 31 deletions

13
client/readme.md Normal file
View File

@@ -0,0 +1,13 @@
### Webserver
A simple webserver for managing sounds played by the bot.
``I havent paid much attention to this and it can be refactored, rewritten and just more structured. I didn't feel like i bothered too much so i gave it minimal attention and left it at "I just works" stage.``
//Feels weird to write basic js in the browser since it was so long ago..
## How it works,
the bot should give you an address on startup, but if you miss it, it should be http://localhost:8080 if you host it somewhere its the ip-address of the server instead of localhost.
i tested ``foundation`` because the sake of it for the design, i dont think i will use it again on a project.

View File

@@ -1,3 +1,57 @@
function loadFiles() {
// Fetch the JSON data from the /sounds endpoint
fetch('/sounds')
.then(response => response.json())
.then(data => {
// Get the fileList element
const fileList = document.getElementById('fileList');
// Clear the current list
fileList.innerHTML = '';
// Add each file to the list
data.forEach(file => {
// Create a new list item
const li = document.createElement('li');
li.className = 'grid-x';
// Create a div for the file name and add it to the list item
const fileNameDiv = document.createElement('div');
fileNameDiv.className = 'cell auto';
fileNameDiv.textContent = file;
li.appendChild(fileNameDiv);
// Create a div for the trash icon and add it to the list item
const trashIconDiv = document.createElement('div');
trashIconDiv.className = 'cell shrink';
trashIconDiv.style.cursor = 'pointer';
trashIconDiv.textContent = '🗑️';
li.appendChild(trashIconDiv);
// Attach a click event listener to the trash icon div
trashIconDiv.addEventListener('click', () => {
// Send a DELETE request to the server to remove the file
fetch('/sounds/' + file, { method: 'DELETE' })
.then(response => response.text())
.then(message => {
console.log(message);
// Remove the list item from the fileList element
fileList.removeChild(li);
})
.catch(error => console.error('Error:', error));
});
// Add the list item to the fileList element
fileList.appendChild(li);
});
})
.catch(error => console.error('Error:', error));
}
// Call loadFiles when the script is loaded
loadFiles();
document.getElementById('uploadForm').addEventListener('submit', function(event) {
event.preventDefault();
@@ -37,10 +91,13 @@ document.getElementById('uploadForm').addEventListener('submit', function(event)
.then(data => {
console.log(data);
alert('File uploaded successfully.');
// Call loadFiles again to update the file list
loadFiles();
})
.catch(error => {
console.error(error);
alert('An error occurred while uploading the file.');
});
});
});
});

View File

@@ -23,10 +23,17 @@
<button class="button expanded" type="submit">Upload</button>
</div>
</form>
<div class="callout">
<h3>Uploaded Files</h3>
<div style="height:200px;overflow:auto;">
<ul id="fileList" class="vertical menu"></ul>
</div>
</div>
</div>
</div>
</div>
<script src="client.js"></script>
</body>
</html>
</html>

View File

@@ -30,6 +30,29 @@ app.post('/upload', upload.single('myFile'), async (req, res) => {
res.send('File uploaded successfully.');
});
app.get('/sounds', (_req, res) => {
const fs = require('fs');
const directoryPath = path.join(__dirname, '../sounds');
fs.readdir(directoryPath, function (err: any, files: any[]) {
if (err) {
return console.log(LoggerColors.Red, 'Unable to scan directory: ' + err);
}
res.send(files);
});
});
app.delete('/sounds/:filename', (req, res) => {
const fs = require('fs');
const directoryPath = path.join(__dirname, '../sounds');
const filePath = directoryPath + '/' + req.params.filename;
fs.unlink(filePath, (err: any) => {
if (err) {
return console.log(LoggerColors.Red, 'Unable to delete file: ' + err);
}
res.send('File deleted successfully.');
});
});
app.use(express.static(path.join(__dirname, "web")));
export function startServer() {
@@ -37,4 +60,4 @@ export function startServer() {
app.listen(port, () => {
console.log(LoggerColors.Cyan,`Add sounds at http://localhost:${port}, or drop in the sounds folder.`);
});
}
}