Video Cache should "work"" now ?!

This commit is contained in:
Violet 2023-10-30 14:09:17 +00:00
parent d0fdc8a05f
commit ccd462b62a
2 changed files with 28 additions and 18 deletions

View file

@ -19,8 +19,7 @@ const searchCacheDur = (process.env.SEARCH_DUR || 24) * 3600000
const playerPath = path.join(resources, 'player.html') const playerPath = path.join(resources, 'player.html')
const searchPath = path.join(resources, 'searchPage.html') const searchPath = path.join(resources, 'searchPage.html')
const cssHeader = `<style> ${fs.readFileSync(cssPath)} </style>` const cssHeader = `<link rel="stylesheet" href="/mainStyle.css">`
if (fs.existsSync(cachePath)) { if (fs.existsSync(cachePath)) {
@ -52,7 +51,7 @@ async function cacher(id, ready) {
var vidInfo = await ytdl.getBasicInfo(id) var vidInfo = await ytdl.getBasicInfo(id)
var video = ytdl(id, { filter: 'videoandaudio', quality: "highest", format: 'mp4' }) var video = ytdl(id, { filter: 'videoandaudio', quality: "highest", format: 'mp4' })
.on("progress", (chunk, ct, et) => { .on("progress", (chunk, ct, et) => {
if (debounce && (ct / et) > 0.015) { if (debounce && (ct / et) > 0.015) {
debounce = false debounce = false
videoCache[id] = { videoCache[id] = {
@ -60,7 +59,7 @@ async function cacher(id, ready) {
"size": et, "size": et,
"downloaded": false, "downloaded": false,
"download%": 0, "download%": 0,
"lastUsed": new Date().getTime(), "lastUsed": Date.now(),
"duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000 "duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000
} }
@ -163,7 +162,6 @@ app.get("/search", async (req, res) => {
const item = searchCache[itemName]; const item = searchCache[itemName];
if (item[1] < Date.now()) { if (item[1] < Date.now()) {
console.log("Deleted!")
delete searchCache[search] delete searchCache[search]
} }
} }
@ -173,10 +171,10 @@ app.get("/search", async (req, res) => {
searchCache[search][1] = Date.now() + searchCacheDur searchCache[search][1] = Date.now() + searchCacheDur
} else { } else {
youtube.search(search, { type: "all" }) youtube.search(search, { type: "all" })
.then((result)=> { .then((result) => {
searchReturn(result) searchReturn(result)
searchCache[search] = [result, Date.now() + searchCacheDur] searchCache[search] = [result, Date.now() + searchCacheDur]
}) })
} }
}) })
@ -208,34 +206,32 @@ app.get("/video", async (req, res) => {
const end = parts[1] const end = parts[1]
? parseInt(parts[1], 10) ? parseInt(parts[1], 10)
: fileSize - 1 : fileSize - 1
if (start >= fs.statSync(vidpath).size + 1) { if (start >= fs.statSync(vidpath).size + 1) {
return return
} }
const chunksize = (end - start) + 1 const chunksize = (end - start) + 1
const head = { const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`, 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes', 'Accept-Ranges': 'bytes',
'Content-Length': chunksize, 'Content-Length': chunksize,
'Content-Type': 'video/mp4', 'Content-Type': 'video/mp4',
} }
res.writeHead(206, head) res.writeHead(206, head)
if (fs.existsSync(vidpath)) { if (fs.existsSync(vidpath)) {
fs.createReadStream(vidpath, { start: start }).pipe(res) fs.createReadStream(vidpath, { start: start }).pipe(res)
} }
} }
} }
console.log(videoCache)
if (id in videoCache) { if (id in videoCache) {
if ("path" in videoCache[id]) { if ("path" in videoCache[id]) {
ready(videoCache[id].path) ready(videoCache[id].path)
videoCache[id].lastUsed = new Date().getTime() videoCache[id].lastUsed = Date.now()
} }
} else { } else {
videoCache[id] = [] videoCache[id] = []
@ -253,6 +249,20 @@ app.get("/video", async (req, res) => {
res.writeHead(200, head) res.writeHead(200, head)
fs.createReadStream(path).pipe(res) fs.createReadStream(path).pipe(res)
} }
var tA = Object.keys(videoCache)
for (let index = 0; index < tA.length; index++) {
const itemName = tA[index];
const item = videoCache[itemName]
const itemPath = item.path
if ("lastUsed" in item && item.lastUsed + (item.duration * 2.5) < Date.now()) {
delete videoCache[itemName]
if (fs.existsSync(itemPath)) {
fs.unlinkSync(itemPath)
}
}
}
}) })
app.get("/watch", async (req, res) => { app.get("/watch", async (req, res) => {

View file

@ -7,7 +7,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous"> integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="./mainStyle.css"> <link rel="stylesheet" href="/mainStyle.css">
<title>SimpleTube</title> <title>SimpleTube</title>
</head> </head>