File size: 12,807 Bytes
18b952c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""
Telegram Multi-Part File Streamer - Main Application
High-performance file upload and streaming service with zero-disk buffering
"""

import asyncio
import logging
from typing import AsyncGenerator, Optional
from contextlib import asynccontextmanager

from fastapi import FastAPI, Request, HTTPException, Response
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

from session_manager import SessionManager
from database import Database, FileMetadata
from utils import (
    calculate_part_and_offset,
    generate_unique_id,
    CHUNK_SIZE,
    MAX_PART_SIZE
)

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


# Global instances
session_manager: Optional[SessionManager] = None
database: Optional[Database] = None


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan manager"""
    global session_manager, database
    
    logger.info("Initializing application...")
    
    # Initialize database
    database = Database()
    await database.connect()
    
    # Initialize session manager
    session_manager = SessionManager()
    await session_manager.initialize()
    
    logger.info("Application initialized successfully")
    
    yield
    
    # Cleanup
    logger.info("Shutting down application...")
    await session_manager.cleanup()
    await database.disconnect()
    logger.info("Application shutdown complete")


# Initialize FastAPI app
app = FastAPI(
    title="Telegram Multi-Part File Streamer",
    description="High-performance file upload and streaming service",
    version="1.0.0",
    lifespan=lifespan
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
async def root():
    """Health check endpoint"""
    return {
        "status": "online",
        "service": "Telegram Multi-Part File Streamer",
        "version": "1.0.0"
    }


@app.get("/health")
async def health_check():
    """Detailed health check"""
    session_count = len(session_manager.sessions) if session_manager else 0
    db_connected = database.is_connected() if database else False
    
    return {
        "status": "healthy" if (session_count > 0 and db_connected) else "degraded",
        "sessions": session_count,
        "database": "connected" if db_connected else "disconnected"
    }


@app.post("/upload")
async def upload_file(request: Request, filename: Optional[str] = None):
    """
    High-speed zero-disk file upload endpoint
    Streams data directly from HTTP to Telegram with auto-splitting
    """
    if not session_manager or not database:
        raise HTTPException(status_code=503, detail="Service not initialized")
    
    logger.info(f"Upload request received: filename={filename}")
    
    unique_id = generate_unique_id()
    file_parts = []
    total_size = 0
    part_number = 0
    
    try:
        # Create async generator from request stream
        async def request_stream() -> AsyncGenerator[bytes, None]:
            async for chunk in request.stream():
                yield chunk
        
        # Buffer for part assembly
        part_buffer = bytearray()
        
        async for chunk in request_stream():
            part_buffer.extend(chunk)
            
            # Check if we need to upload this part
            while len(part_buffer) >= MAX_PART_SIZE:
                part_number += 1
                part_data = bytes(part_buffer[:MAX_PART_SIZE])
                part_buffer = part_buffer[MAX_PART_SIZE:]
                
                logger.info(f"Uploading part {part_number} ({len(part_data)} bytes)")
                
                # Upload part to Telegram
                file_id = await session_manager.upload_part(
                    part_data,
                    f"{filename or unique_id}_part_{part_number}"
                )
                
                file_parts.append({
                    "part_number": part_number,
                    "file_id": file_id,
                    "size": len(part_data)
                })
                
                total_size += len(part_data)
                
                logger.info(
                    f"Part {part_number} uploaded successfully. "
                    f"Total size: {total_size / (1024**3):.2f} GB"
                )
        
        # Upload remaining data as final part
        if len(part_buffer) > 0:
            part_number += 1
            part_data = bytes(part_buffer)
            
            logger.info(f"Uploading final part {part_number} ({len(part_data)} bytes)")
            
            file_id = await session_manager.upload_part(
                part_data,
                f"{filename or unique_id}_part_{part_number}"
            )
            
            file_parts.append({
                "part_number": part_number,
                "file_id": file_id,
                "size": len(part_data)
            })
            
            total_size += len(part_data)
        
        # Store metadata in database
        metadata = FileMetadata(
            unique_id=unique_id,
            filename=filename or f"file_{unique_id}",
            total_size=total_size,
            parts=file_parts,
            part_count=part_number
        )
        
        await database.save_file_metadata(metadata)
        
        logger.info(
            f"Upload completed: unique_id={unique_id}, "
            f"parts={part_number}, total_size={total_size / (1024**3):.2f} GB"
        )
        
        return {
            "success": True,
            "unique_id": unique_id,
            "filename": metadata.filename,
            "total_size": total_size,
            "parts": part_number,
            "download_url": f"/dl/{unique_id}"
        }
    
    except Exception as e:
        logger.error(f"Upload failed: {str(e)}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")


@app.get("/dl/{unique_id}")
async def stream_file(unique_id: str, request: Request):
    """
    High-speed streaming endpoint with full range request support
    Supports multi-part concatenation and parallel connections
    """
    if not session_manager or not database:
        raise HTTPException(status_code=503, detail="Service not initialized")
    
    # Fetch file metadata
    metadata = await database.get_file_metadata(unique_id)
    if not metadata:
        raise HTTPException(status_code=404, detail="File not found")
    
    # Parse range header
    range_header = request.headers.get("range")
    start = 0
    end = metadata.total_size - 1
    status_code = 200
    
    if range_header:
        # Parse range: bytes=start-end
        range_str = range_header.replace("bytes=", "")
        range_parts = range_str.split("-")
        
        if range_parts[0]:
            start = int(range_parts[0])
        if range_parts[1]:
            end = int(range_parts[1])
        
        status_code = 206  # Partial Content
    
    # Validate range
    if start < 0 or end >= metadata.total_size or start > end:
        raise HTTPException(status_code=416, detail="Range not satisfiable")
    
    logger.info(
        f"Streaming request: unique_id={unique_id}, "
        f"range={start}-{end}, size={end - start + 1}"
    )
    
    # Create streaming response
    content_length = end - start + 1
    
    headers = {
        "Content-Type": "application/octet-stream",
        "Content-Length": str(content_length),
        "Accept-Ranges": "bytes",
        "Content-Disposition": f'attachment; filename="{metadata.filename}"',
    }
    
    if status_code == 206:
        headers["Content-Range"] = f"bytes {start}-{end}/{metadata.total_size}"
    
    async def stream_generator() -> AsyncGenerator[bytes, None]:
        """Generate stream from Telegram parts"""
        bytes_sent = 0
        current_position = 0
        
        for part in metadata.parts:
            part_start = current_position
            part_end = current_position + part["size"] - 1
            
            # Check if this part overlaps with requested range
            if part_end < start:
                current_position += part["size"]
                continue
            
            if part_start > end:
                break
            
            # Calculate offset within this part
            offset_in_part = max(0, start - part_start)
            bytes_to_read = min(
                part["size"] - offset_in_part,
                content_length - bytes_sent
            )
            
            logger.debug(
                f"Streaming part {part['part_number']}: "
                f"offset={offset_in_part}, bytes={bytes_to_read}"
            )
            
            # Stream this part with retry logic
            retry_count = 0
            max_retries = 3
            
            while retry_count < max_retries:
                try:
                    async for chunk in session_manager.stream_part(
                        part["file_id"],
                        offset=offset_in_part,
                        limit=bytes_to_read
                    ):
                        chunk_size = len(chunk)
                        
                        # Ensure we don't send more than requested
                        if bytes_sent + chunk_size > content_length:
                            chunk = chunk[:content_length - bytes_sent]
                            chunk_size = len(chunk)
                        
                        yield chunk
                        bytes_sent += chunk_size
                        
                        if bytes_sent >= content_length:
                            return
                    
                    break  # Success
                
                except Exception as e:
                    retry_count += 1
                    if retry_count >= max_retries:
                        logger.error(
                            f"Failed to stream part {part['part_number']}: {str(e)}"
                        )
                        raise
                    
                    wait_time = 2 ** retry_count
                    logger.warning(
                        f"Retry {retry_count}/{max_retries} for part "
                        f"{part['part_number']} after {wait_time}s"
                    )
                    await asyncio.sleep(wait_time)
            
            current_position += part["size"]
    
    return StreamingResponse(
        stream_generator(),
        status_code=status_code,
        headers=headers,
        media_type="application/octet-stream"
    )


@app.get("/info/{unique_id}")
async def get_file_info(unique_id: str):
    """Get file metadata and information"""
    if not database:
        raise HTTPException(status_code=503, detail="Service not initialized")
    
    metadata = await database.get_file_metadata(unique_id)
    if not metadata:
        raise HTTPException(status_code=404, detail="File not found")
    
    return {
        "unique_id": metadata.unique_id,
        "filename": metadata.filename,
        "total_size": metadata.total_size,
        "total_size_gb": f"{metadata.total_size / (1024**3):.2f}",
        "parts": metadata.part_count,
        "uploaded_at": metadata.uploaded_at,
        "download_url": f"/dl/{unique_id}"
    }


@app.delete("/delete/{unique_id}")
async def delete_file(unique_id: str):
    """Delete file and all its parts"""
    if not session_manager or not database:
        raise HTTPException(status_code=503, detail="Service not initialized")
    
    # Get metadata
    metadata = await database.get_file_metadata(unique_id)
    if not metadata:
        raise HTTPException(status_code=404, detail="File not found")
    
    # Delete from Telegram (best effort)
    deleted_parts = 0
    for part in metadata.parts:
        try:
            await session_manager.delete_part(part["file_id"])
            deleted_parts += 1
        except Exception as e:
            logger.warning(f"Failed to delete part {part['part_number']}: {str(e)}")
    
    # Delete from database
    await database.delete_file_metadata(unique_id)
    
    logger.info(f"Deleted file: unique_id={unique_id}, parts={deleted_parts}")
    
    return {
        "success": True,
        "unique_id": unique_id,
        "deleted_parts": deleted_parts,
        "total_parts": metadata.part_count
    }


if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        workers=1,  # Single worker for shared session state
        log_level="info"
    )