Commit
·
12e4bed
1
Parent(s):
eb81f74
Code Updates & Optimisations :Server Upgrade Test
Browse files- FileStream/server/API/__init__.py +38 -0
- FileStream/server/API/downloads.py +37 -0
- FileStream/server/{routes_api.py → API/listings.py} +8 -72
- FileStream/server/API/uploads.py +52 -0
- FileStream/server/APP/__init__.py +17 -0
- FileStream/server/{render_template.py → APP/render_template.py} +1 -1
- FileStream/server/{routes_app.py → APP/routes_app.py} +0 -8
- FileStream/server/Exceptions/__init__.py +2 -0
- FileStream/server/{exceptions.py → Exceptions/exceptions.py} +0 -0
- FileStream/server/Functions/__init__.py +1 -0
- FileStream/server/Functions/downloader.py +2 -3
- FileStream/server/__init__.py +2 -2
- FileStream/server/routes_main.py +1 -4
FileStream/server/API/__init__.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import aiohttp_cors
|
2 |
+
from aiohttp import web
|
3 |
+
|
4 |
+
|
5 |
+
from .listings import (
|
6 |
+
list_all_files_db,
|
7 |
+
list_all_files,
|
8 |
+
list_all_files_tmdb,
|
9 |
+
list_10_all_files_db,
|
10 |
+
list_all_tmdb_tv_from_db,
|
11 |
+
list_all_tmdb_movies_from_db,
|
12 |
+
)
|
13 |
+
from .downloads import stream_handler
|
14 |
+
from .uploads import upload_file
|
15 |
+
|
16 |
+
async def handle_v2(request):
|
17 |
+
return web.Response(text="Hello from app api!")
|
18 |
+
|
19 |
+
# Web server setup with optimized CORS handling
|
20 |
+
api = web.Application()
|
21 |
+
cors = aiohttp_cors.setup(api, defaults={"*": aiohttp_cors.ResourceOptions(
|
22 |
+
allow_credentials=False,
|
23 |
+
expose_headers="*",
|
24 |
+
allow_headers="*",
|
25 |
+
allow_methods="*"
|
26 |
+
)})
|
27 |
+
|
28 |
+
cors.add(api.router.add_get('/', handle_v2))
|
29 |
+
|
30 |
+
api.router.add_get('/files', list_all_files_db)
|
31 |
+
api.router.add_get('/files/mix', list_all_files)
|
32 |
+
api.router.add_get('/tmdb/mix', list_all_files_tmdb)
|
33 |
+
api.router.add_get('/10/files', list_10_all_files_db)
|
34 |
+
api.router.add_get('/tmdb/tv', list_all_tmdb_tv_from_db)
|
35 |
+
api.router.add_get('/tmdb/movies', list_all_tmdb_movies_from_db)
|
36 |
+
|
37 |
+
api.router.add_get('/upload', upload_file)
|
38 |
+
api.router.add_get('/dl/{path}', stream_handler)
|
FileStream/server/API/downloads.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import logging
|
5 |
+
import asyncio
|
6 |
+
import traceback
|
7 |
+
import aiohttp_cors
|
8 |
+
from aiohttp import web
|
9 |
+
from pyrogram import raw
|
10 |
+
from bson import ObjectId
|
11 |
+
from bson.json_util import dumps
|
12 |
+
from aiohttp.http_exceptions import BadStatusLine
|
13 |
+
|
14 |
+
#---------------------Local Imports----------------------------------#
|
15 |
+
from FileStream.bot import req_client
|
16 |
+
from ..Functions import media_streamer
|
17 |
+
from FileStream.Database import Database
|
18 |
+
from FileStream.config import Telegram, Server
|
19 |
+
from FileStream.server.Exceptions import FIleNotFound, InvalidHash
|
20 |
+
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies
|
21 |
+
|
22 |
+
|
23 |
+
# Stream file handler with optimized error handling
|
24 |
+
async def stream_handler(request: web.Request):
|
25 |
+
try:
|
26 |
+
path = request.match_info["path"]
|
27 |
+
return await media_streamer(request, path, "FAST")
|
28 |
+
except InvalidHash as e:
|
29 |
+
raise web.HTTPForbidden(text=e.message)
|
30 |
+
except FIleNotFound as e:
|
31 |
+
raise web.HTTPNotFound(text=e.message)
|
32 |
+
except (AttributeError, BadStatusLine, ConnectionResetError):
|
33 |
+
pass # Handle expected errors silently
|
34 |
+
except Exception as e:
|
35 |
+
logging.error(f"Error while streaming file: {str(e)}")
|
36 |
+
traceback.print_exc()
|
37 |
+
raise web.HTTPInternalServerError(text=str(e))
|
FileStream/server/{routes_api.py → API/listings.py}
RENAMED
@@ -12,10 +12,10 @@ from aiohttp.http_exceptions import BadStatusLine
|
|
12 |
|
13 |
#---------------------Local Imports----------------------------------#
|
14 |
from FileStream.bot import req_client
|
|
|
15 |
from FileStream.Database import Database
|
16 |
from FileStream.config import Telegram, Server
|
17 |
-
from .
|
18 |
-
from FileStream.server.exceptions import FIleNotFound, InvalidHash
|
19 |
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies
|
20 |
|
21 |
|
@@ -26,8 +26,7 @@ CORS_HEADERS = {
|
|
26 |
"Access-Control-Allow-Headers": "*"
|
27 |
}
|
28 |
|
29 |
-
|
30 |
-
return web.Response(text="Hello from app api!")
|
31 |
|
32 |
# API endpoint to list 10 files (pagination optimization)
|
33 |
async def list_10_all_files_db(request):
|
@@ -107,71 +106,8 @@ async def list_all_files(request):
|
|
107 |
} for row in files]
|
108 |
return web.json_response(json.loads(dumps(resp)), headers=CORS_HEADERS)
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
file_details = dict(
|
116 |
-
user_id="thebinary1",
|
117 |
-
dropzone_id=str(data["dzuuid"]),
|
118 |
-
file=dict(
|
119 |
-
file_id=str(data["dzuuid"]),
|
120 |
-
file_unique_id=str(data["dzuuid"]),
|
121 |
-
file_name=str(data.get('file').filename),
|
122 |
-
file_size=int(data["dztotalfilesize"]),
|
123 |
-
mime_type=mime_identifier(str(data.get('file').filename)),
|
124 |
-
part_size=int(data["dzchunksize"]),
|
125 |
-
file_part=int(data["dzchunkindex"]),
|
126 |
-
total_parts=int(data["dztotalchunkcount"])
|
127 |
-
),
|
128 |
-
time=Time_ISTKolNow(),
|
129 |
-
user_type="WEB",
|
130 |
-
privacy_type="PRIVATE"
|
131 |
-
)
|
132 |
-
|
133 |
-
client_req = await req_client() # Ensure client request is fast and optimized
|
134 |
-
tg_connect = TeleUploader(client_req["client"])
|
135 |
-
main = await tg_connect.upload_web_file(file_details, chunk)
|
136 |
-
|
137 |
-
return web.json_response({
|
138 |
-
"status": main.get("status"),
|
139 |
-
"message": main.get("message")
|
140 |
-
})
|
141 |
-
|
142 |
-
# Stream file handler with optimized error handling
|
143 |
-
async def stream_handler(request: web.Request):
|
144 |
-
try:
|
145 |
-
path = request.match_info["path"]
|
146 |
-
return await media_streamer(request, path, "FAST")
|
147 |
-
except InvalidHash as e:
|
148 |
-
raise web.HTTPForbidden(text=e.message)
|
149 |
-
except FIleNotFound as e:
|
150 |
-
raise web.HTTPNotFound(text=e.message)
|
151 |
-
except (AttributeError, BadStatusLine, ConnectionResetError):
|
152 |
-
pass # Handle expected errors silently
|
153 |
-
except Exception as e:
|
154 |
-
logging.error(f"Error while streaming file: {str(e)}")
|
155 |
-
traceback.print_exc()
|
156 |
-
raise web.HTTPInternalServerError(text=str(e))
|
157 |
-
|
158 |
-
# Web server setup with optimized CORS handling
|
159 |
-
api = web.Application()
|
160 |
-
cors = aiohttp_cors.setup(api, defaults={"*": aiohttp_cors.ResourceOptions(
|
161 |
-
allow_credentials=False,
|
162 |
-
expose_headers="*",
|
163 |
-
allow_headers="*",
|
164 |
-
allow_methods="*"
|
165 |
-
)})
|
166 |
-
|
167 |
-
cors.add(api.router.add_get('/', handle_v2))
|
168 |
-
|
169 |
-
api.router.add_get('/files', list_all_files_db)
|
170 |
-
api.router.add_get('/files/mix', list_all_files)
|
171 |
-
api.router.add_get('/tmdb/mix', list_all_files_tmdb)
|
172 |
-
api.router.add_get('/10/files', list_10_all_files_db)
|
173 |
-
api.router.add_get('/tmdb/tv', list_all_tmdb_tv_from_db)
|
174 |
-
api.router.add_get('/tmdb/movies', list_all_tmdb_movies_from_db)
|
175 |
-
|
176 |
-
api.router.add_get('/upload', upload_file)
|
177 |
-
api.router.add_get('/dl/{path}', stream_handler)
|
|
|
12 |
|
13 |
#---------------------Local Imports----------------------------------#
|
14 |
from FileStream.bot import req_client
|
15 |
+
from ..Functions import media_streamer
|
16 |
from FileStream.Database import Database
|
17 |
from FileStream.config import Telegram, Server
|
18 |
+
from FileStream.server.Exceptions import FIleNotFound, InvalidHash
|
|
|
19 |
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies
|
20 |
|
21 |
|
|
|
26 |
"Access-Control-Allow-Headers": "*"
|
27 |
}
|
28 |
|
29 |
+
|
|
|
30 |
|
31 |
# API endpoint to list 10 files (pagination optimization)
|
32 |
async def list_10_all_files_db(request):
|
|
|
106 |
} for row in files]
|
107 |
return web.json_response(json.loads(dumps(resp)), headers=CORS_HEADERS)
|
108 |
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FileStream/server/API/uploads.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import logging
|
5 |
+
import asyncio
|
6 |
+
import traceback
|
7 |
+
import aiohttp_cors
|
8 |
+
from aiohttp import web
|
9 |
+
from pyrogram import raw
|
10 |
+
from bson import ObjectId
|
11 |
+
from bson.json_util import dumps
|
12 |
+
from aiohttp.http_exceptions import BadStatusLine
|
13 |
+
|
14 |
+
#---------------------Local Imports----------------------------------#
|
15 |
+
from FileStream.bot import req_client
|
16 |
+
from ..Functions import media_streamer
|
17 |
+
from FileStream.Database import Database
|
18 |
+
from FileStream.config import Telegram, Server
|
19 |
+
from FileStream.server.Exceptions import FIleNotFound, InvalidHash
|
20 |
+
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies
|
21 |
+
|
22 |
+
# Upload endpoint with optimization (not reading entire file into memory)
|
23 |
+
async def upload_file(request: web.Request):
|
24 |
+
data = await request.post()
|
25 |
+
file = data.get('file').file
|
26 |
+
chunk = file.read() # Read the file in chunks to avoid memory overload
|
27 |
+
file_details = dict(
|
28 |
+
user_id="thebinary1",
|
29 |
+
dropzone_id=str(data["dzuuid"]),
|
30 |
+
file=dict(
|
31 |
+
file_id=str(data["dzuuid"]),
|
32 |
+
file_unique_id=str(data["dzuuid"]),
|
33 |
+
file_name=str(data.get('file').filename),
|
34 |
+
file_size=int(data["dztotalfilesize"]),
|
35 |
+
mime_type=mime_identifier(str(data.get('file').filename)),
|
36 |
+
part_size=int(data["dzchunksize"]),
|
37 |
+
file_part=int(data["dzchunkindex"]),
|
38 |
+
total_parts=int(data["dztotalchunkcount"])
|
39 |
+
),
|
40 |
+
time=Time_ISTKolNow(),
|
41 |
+
user_type="WEB",
|
42 |
+
privacy_type="PRIVATE"
|
43 |
+
)
|
44 |
+
|
45 |
+
client_req = await req_client() # Ensure client request is fast and optimized
|
46 |
+
tg_connect = TeleUploader(client_req["client"])
|
47 |
+
main = await tg_connect.upload_web_file(file_details, chunk)
|
48 |
+
|
49 |
+
return web.json_response({
|
50 |
+
"status": main.get("status"),
|
51 |
+
"message": main.get("message")
|
52 |
+
})
|
FileStream/server/APP/__init__.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from aiohttp import web
|
2 |
+
from aiohttp.http_exceptions import BadStatusLine
|
3 |
+
|
4 |
+
from .Functions.downloader import media_streamer
|
5 |
+
from ...Exceptions import FIleNotFound, InvalidHash
|
6 |
+
from .render_template import render_page, render_upload
|
7 |
+
from .routes_app import stream_handler,upload_handler
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
async def handle_v2(request):
|
12 |
+
return web.Response(text="Hello from app v2!")
|
13 |
+
|
14 |
+
sub_app = web.Application()
|
15 |
+
sub_app.router.add_get('/', handle_v2)
|
16 |
+
sub_app.router.add_get('/watch/{path}', stream_handler)
|
17 |
+
sub_app.router.add_get('/up', upload_handler)
|
FileStream/server/{render_template.py → APP/render_template.py}
RENAMED
@@ -1,5 +1,5 @@
|
|
1 |
-
import aiohttp
|
2 |
import jinja2
|
|
|
3 |
import urllib.parse
|
4 |
from FileStream.config import Telegram, Server
|
5 |
from FileStream.Database import Database
|
|
|
|
|
1 |
import jinja2
|
2 |
+
import aiohttp
|
3 |
import urllib.parse
|
4 |
from FileStream.config import Telegram, Server
|
5 |
from FileStream.Database import Database
|
FileStream/server/{routes_app.py → APP/routes_app.py}
RENAMED
@@ -6,10 +6,6 @@ from FileStream.server.exceptions import FIleNotFound, InvalidHash
|
|
6 |
from FileStream.server.render_template import render_page, render_upload
|
7 |
|
8 |
|
9 |
-
async def handle_v2(request):
|
10 |
-
return web.Response(text="Hello from app v2!")
|
11 |
-
|
12 |
-
|
13 |
#@sub_app.get("/watch/{path}", allow_head=True)
|
14 |
async def stream_handler(request: web.Request):
|
15 |
try:
|
@@ -35,7 +31,3 @@ async def upload_handler(request: web.Request):
|
|
35 |
except (AttributeError, BadStatusLine, ConnectionResetError):
|
36 |
pass
|
37 |
|
38 |
-
sub_app = web.Application()
|
39 |
-
sub_app.router.add_get('/', handle_v2)
|
40 |
-
sub_app.router.add_get('/watch/{path}', stream_handler)
|
41 |
-
sub_app.router.add_get('/up', upload_handler)
|
|
|
6 |
from FileStream.server.render_template import render_page, render_upload
|
7 |
|
8 |
|
|
|
|
|
|
|
|
|
9 |
#@sub_app.get("/watch/{path}", allow_head=True)
|
10 |
async def stream_handler(request: web.Request):
|
11 |
try:
|
|
|
31 |
except (AttributeError, BadStatusLine, ConnectionResetError):
|
32 |
pass
|
33 |
|
|
|
|
|
|
|
|
FileStream/server/Exceptions/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from .exceptions import FIleNotFound, InvalidHash
|
FileStream/server/{exceptions.py → Exceptions/exceptions.py}
RENAMED
File without changes
|
FileStream/server/Functions/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .downloader import media_streamer
|
FileStream/server/Functions/downloader.py
CHANGED
@@ -15,8 +15,7 @@ from FileStream.bot import req_client, FileStream
|
|
15 |
from FileStream import utils, StartTime, __version__
|
16 |
from FileStream.Tools import mime_identifier, Time_ISTKolNow
|
17 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
18 |
-
from FileStream.server.
|
19 |
-
from FileStream.server.render_template import render_page, render_upload
|
20 |
from FileStream.utils.FileProcessors.custom_ul import TeleUploader
|
21 |
|
22 |
|
@@ -36,7 +35,7 @@ async def media_streamer(request: web.Request, db_id: str, speed: str):
|
|
36 |
logging.debug(f"Creating new ByteStreamer object for client {client['index']}")
|
37 |
tg_connect = utils.ByteStreamer(client['client'])
|
38 |
ACTIVE_CLIENTS[client['client']] = tg_connect
|
39 |
-
|
40 |
else:
|
41 |
tg_connect.update_last_activity()
|
42 |
logging.debug(f"Using cached ByteStreamer object for client {client['index']}")
|
|
|
15 |
from FileStream import utils, StartTime, __version__
|
16 |
from FileStream.Tools import mime_identifier, Time_ISTKolNow
|
17 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
18 |
+
from FileStream.server.Exceptions import FIleNotFound, InvalidHash
|
|
|
19 |
from FileStream.utils.FileProcessors.custom_ul import TeleUploader
|
20 |
|
21 |
|
|
|
35 |
logging.debug(f"Creating new ByteStreamer object for client {client['index']}")
|
36 |
tg_connect = utils.ByteStreamer(client['client'])
|
37 |
ACTIVE_CLIENTS[client['client']] = tg_connect
|
38 |
+
|
39 |
else:
|
40 |
tg_connect.update_last_activity()
|
41 |
logging.debug(f"Using cached ByteStreamer object for client {client['index']}")
|
FileStream/server/__init__.py
CHANGED
@@ -3,9 +3,9 @@ import logging
|
|
3 |
from aiohttp import web
|
4 |
|
5 |
#-----------------------Local Imports-------------------------#
|
6 |
-
from .
|
7 |
from .routes_main import routes
|
8 |
-
from .
|
9 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
10 |
|
11 |
|
|
|
3 |
from aiohttp import web
|
4 |
|
5 |
#-----------------------Local Imports-------------------------#
|
6 |
+
from .API import api
|
7 |
from .routes_main import routes
|
8 |
+
from .APP import sub_app
|
9 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
10 |
|
11 |
|
FileStream/server/routes_main.py
CHANGED
@@ -9,14 +9,11 @@ from pyrogram import raw
|
|
9 |
from aiohttp.http_exceptions import BadStatusLine
|
10 |
|
11 |
#---------------------Local Upload---------------------#
|
12 |
-
from FileStream.config import Telegram
|
13 |
from FileStream.bot import FileStream
|
14 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS
|
15 |
-
from FileStream.server.exceptions import FIleNotFound, InvalidHash
|
16 |
from FileStream import utils, StartTime, __version__
|
17 |
-
from FileStream.utils.FileProcessors.custom_ul import TeleUploader
|
18 |
from FileStream.Tools import mime_identifier, Time_ISTKolNow
|
19 |
-
|
20 |
|
21 |
tasks = []
|
22 |
routes = web.RouteTableDef()
|
|
|
9 |
from aiohttp.http_exceptions import BadStatusLine
|
10 |
|
11 |
#---------------------Local Upload---------------------#
|
|
|
12 |
from FileStream.bot import FileStream
|
13 |
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS
|
|
|
14 |
from FileStream import utils, StartTime, __version__
|
|
|
15 |
from FileStream.Tools import mime_identifier, Time_ISTKolNow
|
16 |
+
|
17 |
|
18 |
tasks = []
|
19 |
routes = web.RouteTableDef()
|