Spaces:
Paused
Paused
:gem: [Feature] DataProxyAPI: cache images
Browse files- .gitignore +2 -1
- apis/data_proxy_api.py +30 -4
- configs/envs.json +2 -1
- configs/envs.py +1 -2
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
__pycache__
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
cache
|
apis/data_proxy_api.py
CHANGED
|
@@ -9,7 +9,7 @@ from tclogger import logger
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
from apis.arg_parser import ArgParser
|
| 12 |
-
from configs.envs import DATA_PROXY_APP_ENVS
|
| 13 |
|
| 14 |
|
| 15 |
class DataProxyAPI:
|
|
@@ -39,10 +39,36 @@ class DataProxyAPI:
|
|
| 39 |
allow_headers=["*"],
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def get_img(self, url: str):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def setup_routes(self):
|
| 48 |
self.app.get(
|
|
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
from apis.arg_parser import ArgParser
|
| 12 |
+
from configs.envs import DATA_PROXY_APP_ENVS, CACHE_ROOT
|
| 13 |
|
| 14 |
|
| 15 |
class DataProxyAPI:
|
|
|
|
| 39 |
allow_headers=["*"],
|
| 40 |
)
|
| 41 |
|
| 42 |
+
def get_img_path_by_url(self, url: str):
|
| 43 |
+
return CACHE_ROOT / url.split("/")[-1]
|
| 44 |
+
|
| 45 |
+
def save_img(self, img_url: str, img_bytes: bytes):
|
| 46 |
+
img_path = self.get_img_path_by_url(img_url)
|
| 47 |
+
if not img_path.parent.exists():
|
| 48 |
+
img_path.parent.mkdir(parents=True, exist_ok=True)
|
| 49 |
+
with open(img_path, "wb") as img_file:
|
| 50 |
+
img_file.write(img_bytes)
|
| 51 |
+
return img_path
|
| 52 |
+
|
| 53 |
+
def get_img_bytes_from_url(self, url: str):
|
| 54 |
+
try:
|
| 55 |
+
response = requests.get(url, headers=self.REQUESTS_HEADERS)
|
| 56 |
+
img_bytes = response.content
|
| 57 |
+
except Exception as e:
|
| 58 |
+
img_bytes = None
|
| 59 |
+
|
| 60 |
+
return img_bytes
|
| 61 |
+
|
| 62 |
def get_img(self, url: str):
|
| 63 |
+
img_path = self.get_img_path_by_url(url)
|
| 64 |
+
if img_path.exists():
|
| 65 |
+
with open(img_path, "rb") as img_file:
|
| 66 |
+
img_bytes = img_file.read()
|
| 67 |
+
else:
|
| 68 |
+
img_bytes = self.get_img_bytes_from_url(url)
|
| 69 |
+
self.save_img(url, img_bytes)
|
| 70 |
+
|
| 71 |
+
return Response(content=img_bytes, media_type="image/jpeg")
|
| 72 |
|
| 73 |
def setup_routes(self):
|
| 74 |
self.app.get(
|
configs/envs.json
CHANGED
|
@@ -4,5 +4,6 @@
|
|
| 4 |
"host": "0.0.0.0",
|
| 5 |
"port": 22001,
|
| 6 |
"version": "0.1"
|
| 7 |
-
}
|
|
|
|
| 8 |
}
|
|
|
|
| 4 |
"host": "0.0.0.0",
|
| 5 |
"port": 22001,
|
| 6 |
"version": "0.1"
|
| 7 |
+
},
|
| 8 |
+
"cache_root": "cache"
|
| 9 |
}
|
configs/envs.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
-
|
| 3 |
from tclogger import OSEnver
|
| 4 |
|
| 5 |
configs_root = Path(__file__).parents[1] / "configs"
|
| 6 |
envs_path = configs_root / "envs.json"
|
| 7 |
-
|
| 8 |
ENVS_ENVER = OSEnver(envs_path)
|
| 9 |
DATA_PROXY_APP_ENVS = ENVS_ENVER["data_proxy_app"]
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
|
|
|
| 2 |
from tclogger import OSEnver
|
| 3 |
|
| 4 |
configs_root = Path(__file__).parents[1] / "configs"
|
| 5 |
envs_path = configs_root / "envs.json"
|
|
|
|
| 6 |
ENVS_ENVER = OSEnver(envs_path)
|
| 7 |
DATA_PROXY_APP_ENVS = ENVS_ENVER["data_proxy_app"]
|
| 8 |
+
CACHE_ROOT = Path(__file__).parents[1] / ENVS_ENVER["cache_root"]
|