q6 commited on
Commit
c2af58d
·
1 Parent(s): 5f3b9b1
Files changed (1) hide show
  1. API/app.py +33 -28
API/app.py CHANGED
@@ -225,28 +225,33 @@ async def download_image(session, post_id, post_url):
225
  content = await response.read()
226
  return (post_id, content)
227
 
228
- async def generate_tar(posts, session):
229
- tar_buffer = io.BytesIO()
230
- tarf = tarfile.open(mode="w", fileobj=tar_buffer)
231
-
232
- semaphore = asyncio.Semaphore(100)
233
-
234
- async def add_to_tar(post_id, image_url):
235
- async with semaphore:
236
- url = f"{img_base}{image_url}"
237
- async with session.get(url) as response:
238
- image_data = await response.read()
239
- image_name = f"{post_id}.png"
240
- info = tarfile.TarInfo(name=image_name)
241
- info.size = len(image_data)
242
- tarf.addfile(tarinfo=info, fileobj=io.BytesIO(image_data))
243
-
244
- tasks = [add_to_tar(post_id, image_url) for post_id, image_url in posts.items()]
245
- await asyncio.gather(*tasks)
246
-
247
- tarf.close()
248
- tar_buffer.seek(0)
249
- return tar_buffer
 
 
 
 
 
250
 
251
  @app.post("/download")
252
  async def download(
@@ -254,13 +259,13 @@ async def download(
254
  ):
255
  posts = items.posts
256
  async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
257
- tar_buffer = await generate_tar(posts, session)
258
 
259
- return StreamingResponse(
260
- tar_buffer,
261
- media_type="application/x-tar",
262
- headers={"Content-Disposition": f"attachment; filename={base26_time()}.tar"}
263
- )
264
 
265
  @app.get("/")
266
  async def read_root():
 
225
  content = await response.read()
226
  return (post_id, content)
227
 
228
+ async def tar_stream_generator(posts, session):
229
+ for post_id, image_url in posts.items():
230
+ try:
231
+ # Download the image data
232
+ image_data = await download_image(session, post_id, image_url)
233
+ # Create the tar header
234
+ image_name = f"{post_id}.png"
235
+ tarinfo = tarfile.TarInfo(name=image_name)
236
+ tarinfo.size = len(image_data)
237
+ tarinfo.mtime = int(time.time())
238
+ tarinfo.mode = 0o644
239
+ tarinfo.uid = 0
240
+ tarinfo.gid = 0
241
+ header = tarinfo.tobuf()
242
+ # Yield the header
243
+ yield header
244
+ # Yield the image data
245
+ yield image_data
246
+ # Pad the image data to a multiple of 512 bytes
247
+ remainder = len(image_data) % 512
248
+ if remainder > 0:
249
+ padding = b'\0' * (512 - remainder)
250
+ yield padding
251
+ except Exception as e:
252
+ continue
253
+ # At the end, yield two 512-byte blocks of zeros
254
+ yield b'\0' * 1024
255
 
256
  @app.post("/download")
257
  async def download(
 
259
  ):
260
  posts = items.posts
261
  async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
262
+ tar_generator = tar_stream_generator(posts, session)
263
 
264
+ return StreamingResponse(
265
+ tar_generator,
266
+ media_type="application/x-tar",
267
+ headers={"Content-Disposition": f"attachment; filename={base26_time()}.tar"}
268
+ )
269
 
270
  @app.get("/")
271
  async def read_root():