This commit is contained in:
Ondrej Zara 2019-04-15 15:13:38 +02:00
parent 279b5bc973
commit a6da50e9c9
5 changed files with 45 additions and 12 deletions

View file

@ -665,9 +665,10 @@ nav ul li.active {
margin-left: auto; margin-left: auto;
} }
#yt pre { #yt pre {
margin: 0.5em 0.5ch;
flex-grow: 1; flex-grow: 1;
overflow: auto; overflow: auto;
white-space: pre; white-space: pre-wrap;
} }
#yt.pending header { #yt.pending header {
background-image: linear-gradient(var(--primary), var(--primary)); background-image: linear-gradient(var(--primary), var(--primary));

View file

@ -14,9 +14,10 @@
} }
pre { pre {
margin: 0.5em 0.5ch;
flex-grow: 1; flex-grow: 1;
overflow: auto; overflow: auto;
white-space: pre; white-space: pre-wrap;
} }
&.pending header { &.pending header {

View file

@ -138,9 +138,12 @@ export async function albumArt(songUrl) {
} }
export async function init() { export async function init() {
let response = await fetch("/ticket", {method:"POST"});
let ticket = (await response.json()).ticket;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
ws = new WebSocket(`ws://${location.host}`); ws = new WebSocket(`ws://${location.host}/?ticket=${encodeURIComponent(ticket)}`);
} catch (e) { reject(e); } } catch (e) { reject(e); }
current = {resolve, reject}; current = {resolve, reject};

View file

@ -2,14 +2,15 @@ const static = require("node-static");
const app = new static.Server("./app"); const app = new static.Server("./app");
const port = Number(process.argv[2]) || 8080; const port = Number(process.argv[2]) || 8080;
let tickets = [];
const cmd = "youtube-dl"; const cmd = "youtube-dl";
//const cmd = "./test.sh";
function downloadYoutube(q, response) { function downloadYoutube(q, response) {
response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks
// response.setHeader("Content-Type", "text/plain; charset=utf-8"); // response.setHeader("Content-Type", "text/plain; charset=utf-8");
// FIXME create directory
console.log("YouTube downloading", q); console.log("YouTube downloading", q);
let args = [ let args = [
"-f", "bestaudio", "-f", "bestaudio",
@ -49,15 +50,42 @@ function handleYoutube(request, response) {
}); });
} }
function handleTicket(request, response) {
request.resume().on("end", () => {
let ticket = require("crypto").randomBytes(16).toString("hex");
tickets.push(ticket);
if (tickets.length > 10) { tickets.shift(); }
let data = {ticket};
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify(data));
});
}
function onRequest(request, response) { function onRequest(request, response) {
if (request.method == "POST" && request.url == "/youtube") { switch (true) {
return handleYoutube(request, response); case request.method == "POST" && request.url == "/youtube":
return handleYoutube(request, response);
case request.method == "POST" && request.url == "/ticket":
return handleTicket(request, response);
default:
return request.on("end", () => app.serve(request, response)).resume();
}
}
function requestValidator(request) {
let ticket = request.resourceURL.query["ticket"];
let index = tickets.indexOf(ticket);
if (index > -1) {
tickets.splice(index, 1);
return true;
} else { } else {
request.on("end", () => app.serve(request, response)).resume(); return false;
} }
} }
let httpServer = require("http").createServer(onRequest).listen(port); let httpServer = require("http").createServer(onRequest).listen(port);
require("ws2mpd").ws2mpd(httpServer, /.*/); require("ws2mpd").ws2mpd(httpServer, requestValidator);
//require("ws2mpd").ws2mpd(httpServer, `http://localhost:${port}`); require("ws2mpd").logging(false);
//require("ws2mpd").logging(false);

View file

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"node-static": "^0.7.11", "node-static": "^0.7.11",
"ws2mpd": "^1.1.0" "ws2mpd": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"less": "^3.9.0" "less": "^3.9.0"