getapi commited on
Commit
51463c5
1 Parent(s): 9aa69e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -108,11 +108,11 @@ async def optimize_pngs(image_paths: list[str | Path] | str | Path) -> None:
108
  await gather(*tasks)
109
 
110
 
111
- async def upload_image_to_imgbb(file_path: Path) -> str | None:
112
  url = f'https://api.imgbb.com/1/upload?key={choice(tokens)}'
113
  try:
114
  with file_path.open('rb') as file:
115
- files = {'image': (file_path.name, file, 'image/png')}
116
  data = {}
117
  async with AsyncClient() as client:
118
  response = await client.post(url, files=files, data=data, timeout=30)
@@ -124,9 +124,9 @@ async def upload_image_to_imgbb(file_path: Path) -> str | None:
124
  return None
125
 
126
 
127
- async def upload_image(file_path: Path | str) -> str | None:
128
  file_path = Path(file_path)
129
- return await upload_image_to_imgbb(file_path)
130
 
131
 
132
  async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
@@ -138,7 +138,7 @@ async def optimize_and_upload(images_urls: list[str] | str, convert: bool = Fals
138
  new_images_urls = []
139
  images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
140
  for image_path in images_paths:
141
- new_url = await upload_image(image_path)
142
  if new_url:
143
  new_images_urls.append(new_url)
144
  logger.info(f'загружено изображение {image_path} в {new_url}')
@@ -178,7 +178,7 @@ async def optimize_images_endpoint(image_urls: ImageURLs):
178
  @app.post('/jpegs_by_urls/')
179
  async def optimize_images_endpoint(image_urls: ImageURLs):
180
  try:
181
- optimized_urls = await optimize_and_upload([str(url) for url in image_urls.urls])
182
  return {"optimized_urls": optimized_urls}
183
  except Exception as e:
184
  raise HTTPException(status_code=500, detail=str(e))
 
108
  await gather(*tasks)
109
 
110
 
111
+ async def upload_image_to_imgbb(file_path: Path, file_type: str = 'png') -> str | None:
112
  url = f'https://api.imgbb.com/1/upload?key={choice(tokens)}'
113
  try:
114
  with file_path.open('rb') as file:
115
+ files = {'image': (file_path.name, file, f'image/{file_type}')}
116
  data = {}
117
  async with AsyncClient() as client:
118
  response = await client.post(url, files=files, data=data, timeout=30)
 
124
  return None
125
 
126
 
127
+ async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | None:
128
  file_path = Path(file_path)
129
+ return await upload_image_to_imgbb(file_path, file_type)
130
 
131
 
132
  async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
 
138
  new_images_urls = []
139
  images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
140
  for image_path in images_paths:
141
+ new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
142
  if new_url:
143
  new_images_urls.append(new_url)
144
  logger.info(f'загружено изображение {image_path} в {new_url}')
 
178
  @app.post('/jpegs_by_urls/')
179
  async def optimize_images_endpoint(image_urls: ImageURLs):
180
  try:
181
+ optimized_urls = await optimize_and_upload([str(url) for url in image_urls.urls], convert=True)
182
  return {"optimized_urls": optimized_urls}
183
  except Exception as e:
184
  raise HTTPException(status_code=500, detail=str(e))