Trying out new cache system
This commit is contained in:
@@ -26,12 +26,12 @@ from src.utils.cryptography import verify_password
|
|||||||
|
|
||||||
# Create a Memory object that caches data to the specified directory
|
# Create a Memory object that caches data to the specified directory
|
||||||
cache_dir = settings.CACHE_PATH
|
cache_dir = settings.CACHE_PATH
|
||||||
memory = Memory(location=cache_dir, verbose=0)
|
async def extract_thumbnail(video_path):
|
||||||
|
proc = await asyncio.create_subprocess_exec(
|
||||||
@memory.cache
|
"ffmpeg", "-i", video_path, "-ss", "00:00:02.000", "-vframes", "1", "-f", "image2pipe", "-"
|
||||||
def extract_thumbnail(video_path):
|
, stdout=subprocess.PIPE,
|
||||||
proc = subprocess.run(["ffmpeg", "-i", video_path, "-ss", "00:00:02.000", "-vframes", "1", "-f", "image2pipe", "-"], stdout=subprocess.PIPE)
|
)
|
||||||
byte_data = proc.stdout
|
byte_data = await proc.stdout.read()
|
||||||
return byte_data
|
return byte_data
|
||||||
|
|
||||||
router = APIRouter(prefix="/signs/{sign_id}/video")
|
router = APIRouter(prefix="/signs/{sign_id}/video")
|
||||||
@@ -109,8 +109,17 @@ async def sign_video_thumbnail(
|
|||||||
if not os.path.exists(f"{settings.DATA_PATH}/{sign_video.path}"):
|
if not os.path.exists(f"{settings.DATA_PATH}/{sign_video.path}"):
|
||||||
raise BaseException("Video not found")
|
raise BaseException("Video not found")
|
||||||
|
|
||||||
|
# check if the thumbnail exists in the cache folder
|
||||||
|
if os.path.exists(f"{settings.CACHE_PATH}/{sign_video.path}.jpg"):
|
||||||
|
# return the thumbnail and make it cacheable for 60 days
|
||||||
|
return FileResponse(f"{settings.CACHE_PATH}/{sign_video.path}.jpg", media_type="image/jpeg", headers={"Cache-Control": "max-age=5184000"})
|
||||||
|
|
||||||
# extract the thumbnail from the video
|
# extract the thumbnail from the video
|
||||||
bytes = extract_thumbnail(f"{settings.DATA_PATH}/{sign_video.path}")
|
bytes = await extract_thumbnail(f"{settings.DATA_PATH}/{sign_video.path}")
|
||||||
|
|
||||||
|
# save the thumbnail in the cache folder
|
||||||
|
with open(f"{settings.CACHE_PATH}/{sign_video.path}.jpg", 'wb') as f:
|
||||||
|
f.write(bytes)
|
||||||
|
|
||||||
# return the thumbnail and make it cacheable for 60 days
|
# return the thumbnail and make it cacheable for 60 days
|
||||||
return StreamingResponse(io.BytesIO(bytes), media_type="image/jpeg", headers={"Cache-Control": "max-age=5184000"})
|
return StreamingResponse(io.BytesIO(bytes), media_type="image/jpeg", headers={"Cache-Control": "max-age=5184000"})
|
||||||
|
|||||||
Reference in New Issue
Block a user