feat: add clear channels api endpoint

This commit is contained in:
antebrl
2025-01-22 23:19:18 +00:00
parent 540914f411
commit 320b716664
7 changed files with 27 additions and 0 deletions

View File

@@ -53,4 +53,9 @@ module.exports = {
res.status(500).json({ error: error.message });
}
},
clearChannels(req, res) {
ChannelService.clearChannels();
res.status(204).send();
}
};

View File

@@ -21,6 +21,7 @@ app.use(express.json());
const apiRouter = express.Router();
apiRouter.get('/', channelController.getChannels);
apiRouter.get('/current', channelController.getCurrentChannel);
apiRouter.delete('/clear', channelController.clearChannels);
apiRouter.get('/playlist', centralChannelController.playlist);
apiRouter.get('/:channelId', channelController.getChannel);
apiRouter.delete('/:channelId', channelController.deleteChannel);

View File

@@ -10,6 +10,12 @@ class ChannelService {
this.currentChannel = this.channels[0];
}
clearChannels() {
ChannelStorage.clear();
this.channels = ChannelStorage.load();
this.currentChannel = this.channels[0];
}
getChannels() {
return this.channels;
}

View File

@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const Channel = require('../models/Channel');
const { clear } = require('console');
const storageFilePath = path.resolve('/channels/channels.json');
@@ -55,5 +56,14 @@ module.exports = {
} catch (err) {
console.error('Error saving data to storage:', err);
}
},
clear() {
try {
fs.unlinkSync(storageFilePath);
console.log('Data cleared successfully.');
} catch (err) {
console.error('Error clearing data from storage:', err);
}
}
}

View File

@@ -27,6 +27,9 @@ class PlaylistService {
}
} else {
content = playlist;
const fs = require('fs');
fs.writeFileSync(`/channels/${playlistName}.txt`, playlist, { encoding: 'utf-8' }, (err) => err && console.error(err));
console.log('Adding playlist to playlist.m3u8');
}
const parsedPlaylist = m3uParser.parse(content);

View File

@@ -56,6 +56,7 @@ class PlaylistUpdater {
try {
// Fetch and renew playlist
await PlaylistService.deletePlaylist(playlist.playlist);
console.log('Adding playlist with playlistUpdate:', playlist.playlistUpdate);
await PlaylistService.addPlaylist(
playlist.playlist,
playlist.playlistName,

View File

@@ -4,6 +4,7 @@ module.exports = (io, socket) => {
socket.on('add-channel', ({ name, url, avatar, mode, headersJson }) => {
try {
console.log('Adding solo channel:', url);
const newChannel = ChannelService.addChannel({ name: name, url: url, avatar: avatar, mode: mode, headersJson: headersJson });
io.emit('channel-added', newChannel); // Broadcast to all clients
} catch (err) {