2020-07-23 10:03:34 -04:00
|
|
|
import mimetypes
|
|
|
|
import os
|
|
|
|
|
|
|
|
import aiofiles
|
|
|
|
|
|
|
|
|
|
|
|
class Cache():
|
|
|
|
if not os.path.exists("ubot/cache"):
|
|
|
|
os.mkdir("ubot/cache")
|
|
|
|
|
|
|
|
def __init__(self, aioclient):
|
|
|
|
self.aioclient = aioclient
|
|
|
|
|
|
|
|
async def cache_file(self, url: str, filename: str):
|
|
|
|
async with self.aioclient.get(url) as response:
|
|
|
|
file_extension = mimetypes.guess_extension(response.headers["content-type"])
|
|
|
|
|
|
|
|
async with aiofiles.open(f"ubot/cache/{filename}{file_extension}", mode="wb") as cache_file:
|
|
|
|
while True:
|
|
|
|
chunk = await response.content.read(4096)
|
|
|
|
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
|
|
|
|
await cache_file.write(chunk)
|
|
|
|
|
|
|
|
return f"ubot/cache/{filename}{file_extension}"
|
|
|
|
|
|
|
|
def remove_cache(self, filename: str):
|
|
|
|
os.remove(filename)
|
|
|
|
|
2020-07-26 11:50:27 -04:00
|
|
|
async def is_cache_required(self, url: str) -> (bool, int):
|
2020-07-23 10:03:34 -04:00
|
|
|
async with self.aioclient.get(url) as response:
|
2020-07-26 11:50:27 -04:00
|
|
|
size = int(response.headers["content-length"])
|
|
|
|
return size >= 20000000, size
|