bharath491 commited on
Commit
ba7fd71
·
verified ·
1 Parent(s): 4932e0b

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +7 -5
server.py CHANGED
@@ -7,7 +7,7 @@ import mimetypes
7
  from pathlib import Path
8
  from aiohttp import web
9
  from dotenv import load_dotenv
10
- import hls_manager
11
 
12
  logger = logging.getLogger(__name__)
13
 
@@ -166,15 +166,17 @@ async def serve_file(request, inline=False):
166
  else:
167
  response = web.StreamResponse(status=200, headers=headers)
168
 
169
- response.content_length = end_byte - start_byte + 1
170
- await response.prepare(request)
171
-
172
  content_length = end_byte - start_byte + 1
 
173
  first_chunk_idx = start_byte // CHUNK_SIZE
174
  slice_start = start_byte % CHUNK_SIZE
175
  num_chunks = math.ceil((slice_start + content_length) / CHUNK_SIZE)
176
 
 
 
 
177
  async with request.app['pyrogram_sem']:
 
178
  await _pipe_stream(
179
  request, response, bot_app, target_media,
180
  first_chunk_idx, num_chunks, slice_start, content_length
@@ -196,4 +198,4 @@ def create_app(bot_app):
196
  app['bot'] = bot_app
197
  app.router.add_get('/', serve_homepage)
198
  app.router.add_get('/download/{token}', handle_download)
199
- return app
 
7
  from pathlib import Path
8
  from aiohttp import web
9
  from dotenv import load_dotenv
10
+
11
 
12
  logger = logging.getLogger(__name__)
13
 
 
166
  else:
167
  response = web.StreamResponse(status=200, headers=headers)
168
 
 
 
 
169
  content_length = end_byte - start_byte + 1
170
+ response.content_length = content_length
171
  first_chunk_idx = start_byte // CHUNK_SIZE
172
  slice_start = start_byte % CHUNK_SIZE
173
  num_chunks = math.ceil((slice_start + content_length) / CHUNK_SIZE)
174
 
175
+ # Acquire semaphore BEFORE preparing the response.
176
+ # If we prepare first and then block on the semaphore, the browser sees
177
+ # an accepted connection with no data flowing — it hangs forever.
178
  async with request.app['pyrogram_sem']:
179
+ await response.prepare(request)
180
  await _pipe_stream(
181
  request, response, bot_app, target_media,
182
  first_chunk_idx, num_chunks, slice_start, content_length
 
198
  app['bot'] = bot_app
199
  app.router.add_get('/', serve_homepage)
200
  app.router.add_get('/download/{token}', handle_download)
201
+ return app