diff --git a/.gitignore b/.gitignore index c5bfa1a..31468fb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ tests/ logo/ app.json Procfile -.vscode \ No newline at end of file +.vscode +.gitignore \ No newline at end of file diff --git a/README.md b/README.md index 515eb74..018ad3f 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,9 @@ pip3 install -U -r requirements.txt | `DEBUG` (optional) | Give `true` to set logging level to debug, info by default. | `BLOCK_DOWNLOADS` (optional) | Enable downloads or not. If any value is provided, downloads will be disabled. | `RESULTS_PER_PAGE` (optional) | Number of results to be returned per page defaults to 20. -| `USERNAME` (optional) | Username for authentication, defaults to `''`. +| `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`. | `PASSWORD` (optional) | Username for authentication, defaults to `''`. +| `SHORT_URL_LEN` (optional) | Url length for aliases | `SESSION_COOKIE_LIFETIME` (optional) | Number of minutes, for which authenticated session is valid for, after which user has to login again. defaults to 60. | `SECRET_KEY` (optional) | Long string for signing the session cookies, required if authentication is enabled. diff --git a/app/config.py b/app/config.py index b39a496..67e9a76 100644 --- a/app/config.py +++ b/app/config.py @@ -44,10 +44,12 @@ host = os.environ.get("HOST", "0.0.0.0") debug = bool(os.environ.get("DEBUG")) block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS")) results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20")) -logo_folder = Path("/Temp/logo/" if platform.system() == "Windows" else "/tmp/logo") -logo_folder.mkdir(exist_ok=True) -username = os.environ.get("USERNAME", "") +logo_folder = Path("./Temp/logo/" if platform.system() == "Windows" else "/tmp/logo") +if not logo_folder.exists(): + logo_folder.mkdir(parents=True) +username = os.environ.get("TGINDEX_USERNAME", "") password = os.environ.get("PASSWORD", "") +SHORT_URL_LEN = int(os.environ.get("SHORT_URL_LEN", 3)) authenticated = username and password SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60") try: diff --git a/app/routes.py b/app/routes.py index 7ab9e2c..e0b028d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,10 +5,8 @@ from telethon.tl.types import Channel, Chat, User from .config import index_settings - log = logging.getLogger(__name__) - async def setup_routes(app, handler): h = handler client = h.client @@ -24,6 +22,15 @@ async def setup_routes(app, handler): web.post("/login", h.login_post, name="login_handle"), web.get("/logout", h.logout_get, name="logout"), ] + def get_common_routes(p): + return [ + web.get(p, h.index), + web.get(p + r"/logo", h.logo), + web.get(p + r"/{id:\d+}/view", h.info), + web.get(p + r"/{id:\d+}/download", h.download_get), + web.head(p + r"/{id:\d+}/download", h.download_head), + web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), + ] if index_all: # print(await client.get_dialogs()) # dialogs = await client.get_dialogs() @@ -48,14 +55,7 @@ async def setup_routes(app, handler): alias_id = h.generate_alias_id(chat) p = "/{chat:" + alias_id + "}" routes.extend( - [ - web.get(p, h.index), - web.get(p + r"/logo", h.logo), - web.get(p + r"/{id:\d+}/view", h.info), - web.get(p + r"/{id:\d+}/download", h.download_get), - web.head(p + r"/{id:\d+}/download", h.download_head), - web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), - ] + get_common_routes(p) ) log.debug(f"Index added for {chat.id} at /{alias_id}") @@ -65,14 +65,7 @@ async def setup_routes(app, handler): alias_id = h.generate_alias_id(chat) p = "/{chat:" + alias_id + "}" routes.extend( - [ - web.get(p, h.index), - web.get(p + r"/logo", h.logo), - web.get(p + r"/{id:\d+}/view", h.info), - web.get(p + r"/{id:\d+}/download", h.download_get), - web.head(p + r"/{id:\d+}/download", h.download_head), - web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), - ] + get_common_routes(p) # returns list() of common routes ) log.debug(f"Index added for {chat.id} at /{alias_id}") routes.append(web.view(r"/{wildcard:.*}", h.wildcard)) diff --git a/app/templates/home.html b/app/templates/home.html index 0ae5aee..ef5bb33 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -4,11 +4,11 @@ Available Sources -
+
{% for chat in chats %} + class="mx-auto flex flex-col justify-items-center w-2/3 min-h-full md:w-2/5 lg:w-1/6 rounded my-2 p-2 shadow-md hover:shadow-lg hover:border hover:border-blue-300 hover:bg-blue-100">
{{chat.name}}
diff --git a/app/views/__init__.py b/app/views/__init__.py index 24a7ba0..a8ed5c3 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -1,6 +1,10 @@ +import base64 +import hashlib import random import string +from ..config import SHORT_URL_LEN + from .home_view import HomeView from .wildcard_view import WildcardView from .download import Download @@ -13,6 +17,7 @@ from .logout_view import LogoutView from .middlewhere import middleware_factory + class Views( HomeView, Download, @@ -33,12 +38,15 @@ class Views( chat_id = chat.id title = chat.title while True: - alias_id = "".join( - [ - random.choice(string.ascii_letters + string.digits) - for _ in range(len(str(chat_id))) - ] - ) + # alias_id = "".join( + # [ + # random.choice(string.ascii_letters + string.digits) + # for _ in range(len(str(chat_id))) + # ] + # ) + orig_id = f"{title}{chat_id}" # the original id + alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode() + if alias_id in self.chat_ids: continue diff --git a/run-dev.py b/run-dev.py new file mode 100644 index 0000000..e1d8cad --- /dev/null +++ b/run-dev.py @@ -0,0 +1,29 @@ +import os +from dotenv import load_dotenv + +load_dotenv() # take environment variables from .env. + +# Code of your application, which uses environment variables (e.g. from `os.environ` or +# `os.getenv`) as if they came from the actual environment. + +# os.system("alias python3=python") + +def runSetup(): + def alert(missing="API_ID , API_HASH"): + print(f"\nCopy your {missing} and save it into Secrets(Environment variables) Sidebar!\n") + + api_id = os.getenv("API_ID") + if api_id == None: + alert() + return + + session_string = os.getenv("SESSION_STRING") + if session_string == None: + os.system("python app/generate_session_string.py") + alert(missing = "SESSION_STRING") + return + + os.system("python -m app") + +if __name__ == '__main__': + runSetup() \ No newline at end of file