From da1f35ec4b25624fddfd5e9d1f0a15085c68bd33 Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sat, 20 Apr 2024 23:57:37 +0300 Subject: [PATCH] add caching to the segment responses. --- app/main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 5e5a4edc..e6fcc3e5 100644 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,9 @@ import asyncio from datetime import datetime import json import os +import pathlib import random +import time from Config import Config from DownloadQueue import DownloadQueue from Utils import ObjectSerializer, Notifier @@ -18,6 +20,7 @@ import logging import caribou import sqlite3 from aiocron import crontab +from aiohttp.helpers import parse_http_date import magic LOG = logging.getLogger('app') @@ -440,6 +443,11 @@ class Main: vc: int = int(request.query.get('vc', 0)) ac: int = int(request.query.get('ac', 0)) + if request.if_modified_since: + file_path = os.path.join(self.config.download_path, file) + if os.path.exists(file_path) and request.if_modified_since.timestamp() == os.path.getmtime(file_path): + return web.Response(status=304) + if not file: raise web.HTTPBadRequest(reason='file is required') @@ -457,10 +465,14 @@ class Main: body=await segmenter.stream(path=self.config.download_path, file=file), headers={ 'Content-Type': 'video/mpegts', - 'Cache-Control': 'no-cache', 'X-Accel-Buffering': 'no', 'Access-Control-Allow-Origin': '*', - 'Access-Control-Max-Age': "300", + 'Pragma': 'public', + 'Cache-Control': f'public, max-age={time.time() + 31536000}', + 'Last-Modified': time.strftime('%a, %d %b %Y %H:%M:%S GMT', datetime.fromtimestamp( + os.path.getmtime(os.path.join(self.config.download_path, file))).timetuple() + ), + 'Expires': time.strftime('%a, %d %b %Y %H:%M:%S GMT', datetime.fromtimestamp(time.time() + 31536000).timetuple()), } )