From a559447e14c7c1944c2252ad0f5073ef1dbf37f8 Mon Sep 17 00:00:00 2001 From: localhost Date: Tue, 3 Dec 2024 21:29:03 +0100 Subject: [PATCH] redis stuff --- src/router/latest.ts | 6 +----- src/router/search.ts | 8 ++------ src/router/transparency.ts | 6 +----- src/router/video.ts | 6 +----- src/router/websocket.ts | 7 +------ src/utils/redis.ts | 30 ++++++++++++++++++++++++++++++ 6 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/utils/redis.ts diff --git a/src/router/latest.ts b/src/router/latest.ts index d1ba98a..3f71e4e 100644 --- a/src/router/latest.ts +++ b/src/router/latest.ts @@ -1,14 +1,10 @@ import { Elysia } from 'elysia'; -import { Redis } from 'ioredis' import { db } from '@/utils/database' import { createSitemapXML, createSitemapIndexXML } from '@/utils/sitemap' +import redis from '@/utils/redis'; const app = new Elysia() -const redis = new Redis({ - host: process.env.REDIS_HOST, - password: process.env.REDIS_PASS, -}); app.get('/latest', async () => { const cached = await redis.get('latest') diff --git a/src/router/search.ts b/src/router/search.ts index f3f954a..35bc72c 100644 --- a/src/router/search.ts +++ b/src/router/search.ts @@ -1,15 +1,11 @@ import { Elysia, t } from 'elysia'; -import { Redis } from 'ioredis' import { RedisRateLimiter } from 'rolling-rate-limiter' import { db } from '@/utils/database' -import { validateVideo, validatePlaylist, validateChannel } from '@/utils/regex' +import { validateVideo, validateChannel } from '@/utils/regex' +import redis from '@/utils/redis'; const app = new Elysia() -const redis = new Redis({ - host: process.env.REDIS_HOST, - password: process.env.REDIS_PASS, -}); const limiter = new RedisRateLimiter({ client: redis, diff --git a/src/router/transparency.ts b/src/router/transparency.ts index 0deb6c4..fa6a5c0 100644 --- a/src/router/transparency.ts +++ b/src/router/transparency.ts @@ -1,13 +1,9 @@ import { Elysia } from 'elysia'; -import { Redis } from 'ioredis' import { db } from '@/utils/database' +import redis from '@/utils/redis'; const app = new Elysia() -const redis = new Redis({ - host: process.env.REDIS_HOST, - password: process.env.REDIS_PASS, -}); app.get('/transparency/list', async () => { const cached = await redis.get('transparency') diff --git a/src/router/video.ts b/src/router/video.ts index be277b5..ccd9096 100644 --- a/src/router/video.ts +++ b/src/router/video.ts @@ -1,16 +1,12 @@ import { Elysia } from 'elysia'; -import { Redis } from 'ioredis' import DOMPurify from 'isomorphic-dompurify' import { db } from '@/utils/database' import { getChannel, getChannelVideos } from '@/utils/metadata'; import { convertRelativeToDate } from '@/utils/common'; +import redis from '@/utils/redis'; const app = new Elysia() -const redis = new Redis({ - host: process.env.REDIS_HOST, - password: process.env.REDIS_PASS, -}); interface processedVideo { id: string; diff --git a/src/router/websocket.ts b/src/router/websocket.ts index ce502f6..6cb0b9b 100644 --- a/src/router/websocket.ts +++ b/src/router/websocket.ts @@ -1,5 +1,4 @@ import { Elysia, t } from 'elysia'; -import { Redis } from 'ioredis' import * as fs from 'node:fs' import { db } from '@/utils/database' @@ -8,12 +7,9 @@ import { checkCaptcha, createDatabaseVideo } from '@/utils/common'; import { downloadVideo } from '@/utils/download'; import { uploadVideo } from '@/utils/upload'; import { getChannelVideos } from '@/utils/metadata'; +import redis from '@/utils/redis'; const app = new Elysia() -const redis = new Redis({ - host: process.env.REDIS_HOST, - password: process.env.REDIS_PASS, -}); const videoIds: Record = {} const sendError = (ws: any, message: string) => { @@ -54,7 +50,6 @@ const handleUpload = async (ws: any, videoId: string, isChannel: boolean = false } }; - app.ws('/save', { query: t.Object({ url: t.String() diff --git a/src/utils/redis.ts b/src/utils/redis.ts new file mode 100644 index 0000000..efc9270 --- /dev/null +++ b/src/utils/redis.ts @@ -0,0 +1,30 @@ +import { Redis } from 'ioredis' +import * as fs from 'node:fs' + +const redis = new Redis({ + host: process.env.REDIS_HOST, + password: process.env.REDIS_PASS, +}); + +redis.on('ready', async function () { + console.log('connected to redis') + + const keys = await redis.keys('*') + const filteredKeys = keys.filter(key => !key.startsWith('blacklist:')) + if (filteredKeys.length) await redis.del(filteredKeys) + + setInterval(async () => { + const files = fs.readdirSync('videos') + const webmFiles = files.filter((file) => file.endsWith('.mp4')) + webmFiles.forEach(async (f) => { + const videoId = f.replace('.mp4', '') + const isActive = await redis.get(videoId) + if (!isActive) { + fs.unlinkSync(`./videos/${f}`) + console.log(`deleted file ${f} because there is no active download of it`) + } + }) + }, 5 * 60000) +}) + +export default redis \ No newline at end of file