redis stuff

This commit is contained in:
localhost 2024-12-03 21:29:03 +01:00
parent f8ca958c12
commit a559447e14
6 changed files with 36 additions and 27 deletions

View File

@ -1,14 +1,10 @@
import { Elysia } from 'elysia'; import { Elysia } from 'elysia';
import { Redis } from 'ioredis'
import { db } from '@/utils/database' import { db } from '@/utils/database'
import { createSitemapXML, createSitemapIndexXML } from '@/utils/sitemap' import { createSitemapXML, createSitemapIndexXML } from '@/utils/sitemap'
import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
});
app.get('/latest', async () => { app.get('/latest', async () => {
const cached = await redis.get('latest') const cached = await redis.get('latest')

View File

@ -1,15 +1,11 @@
import { Elysia, t } from 'elysia'; import { Elysia, t } from 'elysia';
import { Redis } from 'ioredis'
import { RedisRateLimiter } from 'rolling-rate-limiter' import { RedisRateLimiter } from 'rolling-rate-limiter'
import { db } from '@/utils/database' 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 app = new Elysia()
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
});
const limiter = new RedisRateLimiter({ const limiter = new RedisRateLimiter({
client: redis, client: redis,

View File

@ -1,13 +1,9 @@
import { Elysia } from 'elysia'; import { Elysia } from 'elysia';
import { Redis } from 'ioredis'
import { db } from '@/utils/database' import { db } from '@/utils/database'
import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
});
app.get('/transparency/list', async () => { app.get('/transparency/list', async () => {
const cached = await redis.get('transparency') const cached = await redis.get('transparency')

View File

@ -1,16 +1,12 @@
import { Elysia } from 'elysia'; import { Elysia } from 'elysia';
import { Redis } from 'ioredis'
import DOMPurify from 'isomorphic-dompurify' import DOMPurify from 'isomorphic-dompurify'
import { db } from '@/utils/database' import { db } from '@/utils/database'
import { getChannel, getChannelVideos } from '@/utils/metadata'; import { getChannel, getChannelVideos } from '@/utils/metadata';
import { convertRelativeToDate } from '@/utils/common'; import { convertRelativeToDate } from '@/utils/common';
import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
});
interface processedVideo { interface processedVideo {
id: string; id: string;

View File

@ -1,5 +1,4 @@
import { Elysia, t } from 'elysia'; import { Elysia, t } from 'elysia';
import { Redis } from 'ioredis'
import * as fs from 'node:fs' import * as fs from 'node:fs'
import { db } from '@/utils/database' import { db } from '@/utils/database'
@ -8,12 +7,9 @@ import { checkCaptcha, createDatabaseVideo } from '@/utils/common';
import { downloadVideo } from '@/utils/download'; import { downloadVideo } from '@/utils/download';
import { uploadVideo } from '@/utils/upload'; import { uploadVideo } from '@/utils/upload';
import { getChannelVideos } from '@/utils/metadata'; import { getChannelVideos } from '@/utils/metadata';
import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
});
const videoIds: Record<string, string> = {} const videoIds: Record<string, string> = {}
const sendError = (ws: any, message: string) => { const sendError = (ws: any, message: string) => {
@ -54,7 +50,6 @@ const handleUpload = async (ws: any, videoId: string, isChannel: boolean = false
} }
}; };
app.ws('/save', { app.ws('/save', {
query: t.Object({ query: t.Object({
url: t.String() url: t.String()

30
src/utils/redis.ts Normal file
View File

@ -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