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 -