A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
feat: reclaim disk space, delete downloaded videos wip
Christian Fei3a89ccfb7c35
diff --git a/client/index.html b/client/index.htmlindex 122a485..a4f55ac 100644--- a/client/index.html+++ b/client/index.html@@ -65,6 +65,18 @@<input type="checkbox" id="show-original-thumbnail"/><div><small>Show the creators chosen thumbnail, or a neutral one based on the actual video frames</small></div></form>+ <br>+ <form>+ <div class="flex space-between">+ <div>+ Delete downloaded videos you ignored to reclaim disk space+ </div>+ <button id="reclaim-disk-space" type="submit">Delete videos</button>+ </div>+ <div><small id="ignored-videos-disk-space">Currently <span id="disk-space-used"></span> of disk space used</small></div>+ </form>+ <br>+<!--<br><div>diff --git a/client/lib/router.js b/client/lib/router.jsindex dd40b21..4d7e2d7 100644--- a/client/lib/router.js+++ b/client/lib/router.js@@ -52,8 +52,6 @@ const routes = {} },'/settings': { template: document.getElementById('settings-template'), async initialize () {console.log('initalize /settings')-- document.getElementById('search').setAttribute('disabled', 'disabled')const $showThumbnails = document.getElementById('show-thumbnails')store.get(store.showThumbnailsKey) ? $showThumbnails.setAttribute('checked', 'true') : $showThumbnails.removeAttribute('checked')@@ -77,6 +75,29 @@ const routes = {$showOriginalThumbnail.addEventListener('click', (event) => {store.toggle(store.showOriginalThumbnailKey)})++ const $reclaimDiskSpace = document.getElementById('reclaim-disk-space')+ const $ignoredVideosDiskSpace = document.getElementById('ignored-videos-disk-space')+ const $diskSpaceUsed = document.getElementById('disk-space-used')+ window.fetch('/api/ignored-videos-disk-space')+ .then(response => response.text())+ .then((diskSpaceUsed) => {+ $diskSpaceUsed.innerText = diskSpaceUsed+ })+ $reclaimDiskSpace.addEventListener('click', (event) => {+ event.preventDefault()+ console.log('reclaiming')+ window.fetch('/api/reclaim-disk-space', {+ method: 'POST'+ })+ .then(() => {+ $ignoredVideosDiskSpace.innerText = 'Successfully reclaimed disk space'+ })+ .catch((error) => {+ $ignoredVideosDiskSpace.innerHTML = `Failed to reclaim disk space: <br><pre>${error.message}</pre>`+ })+ })+} },'/404': { template: document.getElementById('not-found-template'), async initialize () {document.getElementById('search').setAttribute('disabled', 'disabled')diff --git a/lib/server.js b/lib/server.jsindex cb07a56..0f33a7c 100644--- a/lib/server.js+++ b/lib/server.js@@ -52,6 +52,10 @@ export function createServer ({repo, connections = []}) {return watchVideoHandler(req, res)if (url.pathname.match(/\/captions\/.*/) && req.method === 'GET')return captionsHandler(req, res)+ if (url.pathname === '/api/ignored-videos-disk-space' && req.method === 'GET')+ return currentDiskSpaceHandler(req, res, repo, connections)+ if (url.pathname === '/api/reclaim-disk-space' && req.method === 'POST')+ return reclaimDiskSpaceHandler(req, res, repo, connections)/* client */if (url.pathname === '/main.css')@@ -234,6 +238,36 @@ export function createServer ({repo, connections = []}) {res.end(JSON.stringify(videos))}+ function currentDiskSpaceHandler(req, res) {+ const videos = repo.getVideos()+ const ignoredVideos = videos.filter(video => video.downloaded)+ const diskSpaceUsed = ignoredVideos.reduce((total, video) => {+ try {+ return total + fs.statSync(`./data/videos/${video.id}.mp4`).size / 1000 / 1000 / 1000+ } catch (err) {+ return total+ }+ }, 0)+ res.writeHead(200, { 'Content-Type': 'text/plain' })+ res.end(diskSpaceUsed.toFixed(3) + 'GB')+ }+ function reclaimDiskSpaceHandler(req, res) {+ const videos = repo.getVideos()+ const ignoredVideos = videos.filter(video => video.downloaded)+ ignoredVideos.forEach((video) => {+ try {+ const files = fs.globSync(`./data/videos/${video.id}*`)+ files.forEach(file => fs.unlinkSync(file))+ repo.upsertVideos([Object.assign(video, {downloaded: false, ignored: false})])+ } catch (err) {+ console.error(err.message)+ }+ })+ repo.saveVideos()+ res.writeHead(200)+ res.end()+ }+function captionsHandler(req, res) {// return vtt file of videoconst captionsPath = './data' + req.url.replace('captions', 'videos') + '.en.vtt'