From 8812593f3853eecf93f86530b0304f99a845aa90 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Mon, 29 Nov 2021 18:43:56 +0100 Subject: [PATCH] update 29 --- Dockerfile | 2 +- frontend/src/App.jsx | 35 +++++++++++++++++-- jsconfig.json | 12 +++++++ lib/updater.js | 82 ++++++++++++++++++++++++++++++++++++++++++++ server.js | 6 +++- 5 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 jsconfig.json create mode 100644 lib/updater.js diff --git a/Dockerfile b/Dockerfile index c29e509..7de5453 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM node:16-bullseye -RUN mkdir -p /usr/src/yt-dlp-webui/downloadsd +RUN mkdir -p /usr/src/yt-dlp-webui/download VOLUME /usr/src/yt-dlp-webui/downloads WORKDIR /usr/src/yt-dlp-webui COPY package*.json ./ diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index f1821ff..11d5a52 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -24,6 +24,7 @@ export function App() { const [url, setUrl] = useState('') const [showToast, setShowToast] = useState(false) const [invalidIP, setInvalidIP] = useState(false) + const [updatedBin, setUpdatedBin] = useState(false) const [showSettings, setShowSettings] = useState(false) useEffect(() => { @@ -50,6 +51,13 @@ export function App() { }) }, []) + useEffect(() => { + socket.on('updated', () => { + setUpdatedBin(true) + setHalt(false) + }) + }, []) + const sendUrl = () => { setHalt(true) socket.emit('send-url', url) @@ -77,6 +85,11 @@ export function App() { setHalt(false) } + const updateBinary = () => { + setHalt(true) + socket.emit('update-bin') + } + return ( @@ -100,12 +113,13 @@ export function App() {
{message}
- {' '} - {' '} + {' '} + {' '} + + {progress ? : null} - {progress ? : null}
setShowSettings(!showSettings)}>Settings @@ -126,6 +140,9 @@ export function App() { /> :3022 +
: null } @@ -149,6 +166,18 @@ export function App() { {`Connected to ${localStorage.getItem('server-addr') || 'localhost'}`} + setUpdatedBin(false)} + bg={'primary'} + delay={1500} + autohide + className="mt-5" + > + + Updated yt-dlp binary! + +
diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..235a80a --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2020", + "jsx": "preserve", + "strictFunctionTypes": true + }, + "exclude": [ + "node_modules", + "**/node_modules/*" + ] +} \ No newline at end of file diff --git a/lib/updater.js b/lib/updater.js new file mode 100644 index 0000000..8743f8e --- /dev/null +++ b/lib/updater.js @@ -0,0 +1,82 @@ +const https = require('https'); +const fs = require('fs'); +const path = require('path'); + +// endpoint to github API +const options = { + hostname: 'api.github.com', + path: '/repos/yt-dlp/yt-dlp/releases/latest', + headers: { + 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0' + }, + method: 'GET', + port: 443, +} + +// build the binary url based on the release tag +function buildDonwloadOptions(release) { + return { + hostname: 'github.com', + path: `/yt-dlp/yt-dlp/releases/download/${release}/yt-dlp`, + headers: { + 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0' + }, + method: 'GET', + port: 443, + } +} + +// main +async function update() { + // ensure that the binary has been removed + try { + fs.rmSync(path.join(__dirname, 'yt-dlp')) + } + catch (e) { + console.log('file not found!') + } + // body buffer + let chunks = [] + https.get(options, res => { + // push the http packets chunks into the buffer + res.on('data', chunk => { + chunks.push(chunk) + }); + // the connection has ended so build the body from the buffer + // parse it as a JSON and get the tag_name + res.on('end', () => { + const buffer = Buffer.concat(chunks) + const release = JSON.parse(buffer.toString())['tag_name'] + console.log('The latest release is:', release) + // invoke the binary downloader + downloadBinary(buildDonwloadOptions(release)) + }) + }) +} + +function downloadBinary(url) { + https.get(url, res => { + // if it is a redirect follow the url + if (res.statusCode === 301 || res.statusCode === 302) { + return downloadBinary(res.headers.location) + } + let bin = fs.createWriteStream(path.join(__dirname, 'yt-dlp')) + res.pipe(bin) + // once the connection has ended make the file executable + res.on('end', () => { + fs.chmod(path.join(__dirname, 'yt-dlp'), 0o775, err => { + err ? console.error('failed updating!') : console.log('done!') + }) + }) + }) +} + +function updateFromFrontend(socket) { + update().then(() => { + socket.emit('updated') + }) +} + +module.exports = { + ytdlpUpdater: updateFromFrontend +} \ No newline at end of file diff --git a/server.js b/server.js index 7fccfb5..2134ac2 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ const { createServer } = require('http'); const cors = require('@koa/cors'); const logger = require('./lib/logger'); const { download, abortDownload } = require('./lib/downloader'); +const { ytdlpUpdater } = require('./lib/updater'); const app = new Koa() const server = createServer(app.callback()) @@ -23,10 +24,13 @@ io.on('connection', socket => { logger('ws', args) download(socket, args) }) - socket.on('abort', () => { abortDownload(socket) }) + + socket.on('update-bin', () => { + ytdlpUpdater(socket) + }) }) io.on('disconnect', () => {