teldrive icon indicating copy to clipboard operation
teldrive copied to clipboard

[Bug]: File Upload Fails (500 Error) Due to Telegram Session File Not Being Saved Persistently

Open Abhaikumar007 opened this issue 8 months ago • 1 comments

Teldrive fails during file uploads with a 500 Internal Server Error on the POST /api/uploads/{upload_id} endpoint. This occurs even after a seemingly successful interactive Telegram login (providing phone number and code).

Repoduction

The root cause appears to be that Teldrive (or the underlying gotd library) is failing to create or save the Telegram session file to disk, despite logging messages (td Data saved) suggesting it has. Consequently, subsequent operations or restarts lack a valid Telegram session, leading to authentication failures when attempting API actions like uploads.

Steps to Reproduce:

Configure config.toml with valid Supabase DB details, JWT settings, Telegram app-id, app-hash, and explicitly set session_file path (e.g., session_file = "C:/Users/User/.teldrive/telegram.session" or even a simpler path like C:/Users/User/tg_session.dat). Ensure the target directory exists.

Delete any pre-existing session file at the specified path and potentially clear the .teldrive folder (containing setup.db) to ensure a clean state.

Run Teldrive using teldrive run --config path/to/config.toml --log-level debug --tg-enable-logging.

Perform the initial interactive Telegram login when prompted via console/Web UI (enter phone number, then the code received via Telegram).

Observe the logs indicating successful authentication (e.g., auth.authorization, User Session { ... }, td Data saved { ... }).

Crucially: Check the filesystem at the path specified by session_file. Observe that the session file has NOT been created.

Attempt to upload any file using the Teldrive Web UI.

Observe the upload failing in the UI.

Check the Teldrive logs and see the ERROR {"status": 500, "method": "POST", "path": "/api/uploads/...", "query": "...channelId=0", ...} message, followed by a 400 error for POST /api/files.

Stop and restart Teldrive. Observe that it requires interactive login again, confirming the session was not saved.

Expected behavior

After a successful interactive Telegram login, the session file should be created and saved to the path specified in config.toml's session_file directive (or a default location if not specified).

Subsequent runs of Teldrive should load the session from this file and not require interactive login.

File uploads should initiate correctly, making API calls with the proper channelId derived from the authenticated session and upload_chat setting.

Actual Behavior:

The Telegram session file is never created on the filesystem, regardless of the session_file path specified or running with Administrator privileges.

The td Data saved log message appears after login but seems unrelated to actual disk persistence in this case.

File uploads consistently fail with a 500 error on POST /api/uploads/..., with logs showing channelId=0.

Every restart of Teldrive requires a full interactive Telegram login.

Earlier attempts (before explicit session_file was tried) sometimes showed AUTH_KEY_UNREGISTERED errors when Teldrive potentially loaded an old/invalid session from an unexpected location, but the core issue now appears to be the failure to save a new session.

Version

teldrive 1.6.14

  • commit: f130ad4
  • os/type: windows
  • os/arch: amd64
  • go/version: go1.24.2

Which Platform are you using?

Windows

Environment:

OS: Windows
Database: Supabase (Postgres)

Relevant config.toml:

[db]
data-source = 'postgres://postgres:***PASSWORD***@db.xxxxxxxx.supabase.co:5432/postgres'
prepare-stmt = false

[jwt]
allowed-users = ["abhaikumar007"]
secret = "***SECRET***"
[tg]
app-id = 17896107
app-hash = "***APP_HASH***" # Or confirm this is not considered secret
upload_chat = 1334024702 # Tried "me" and numerical User ID, same upload failure
session_file = "C:/Users/HP/tg_session.dat" # Tried various paths here, including inside .teldrive
[tg.uploads]
encryption-key = "***KEY***"

Logs:

C:\Users\HP>teldrive run --config C:\Users\HP\config.toml --log-level debug --tg-enable-logging
2025/04/30 23:06:51 goose: no migrations to run. current version: 20250408170839
30/04/2025 11:06 PM     INFO    Server started at http://localhost:8080
30/04/2025 11:06 PM     INFO            {"status": 200, "method": "GET", "path": "/api/auth/session", "query": "", "ip": "[::1]:57016", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.5671122, "time": "2025-04-30T17:36:57Z"}
30/04/2025 11:06 PM     INFO            {"status": 200, "method": "GET", "path": "/api/files", "query": "page=1&order=asc&sort=name&path=%2F", "ip": "[::1]:57016", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.3293126, "time": "2025-04-30T17:36:57Z"}
30/04/2025 11:07 PM     INFO            {"status": 200, "method": "GET", "path": "/api/files", "query": "path=%2F&name=%40Cloud%20%20(282).txt&operation=find", "ip": "[::1]:57016", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.5568261, "time": "2025-04-30T17:37:08Z"}
30/04/2025 11:07 PM     INFO            {"status": 200, "method": "GET", "path": "/api/uploads/eff04f422ac86e8944bccdfd55734222", "query": "", "ip": "[::1]:57016", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.2759212, "time": "2025-04-30T17:37:08Z"}
30/04/2025 11:07 PM     ERROR           {"status": 500, "method": "POST", "path": "/api/uploads/eff04f422ac86e8944bccdfd55734222", "query": "partName=%40Cloud++%28282%29.txt&fileName=%40Cloud++%28282%29.txt&partNo=1&encrypted=false&channelId=0", "ip": "[::1]:57016", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.2782339, "time": "2025-04-30T17:37:09Z"}
30/04/2025 11:07 PM     ERROR           {"status": 500, "method": "POST", "path": "/api/uploads/eff04f422ac86e8944bccdfd55734222", "query": "partName=%40Cloud++%28282%29.txt&fileName=%40Cloud++%28282%29.txt&partNo=1&encrypted=false&channelId=0", "ip": "[::1]:57025", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.2808714, "time": "2025-04-30T17:37:09Z"}
30/04/2025 11:07 PM     INFO            {"status": 200, "method": "GET", "path": "/api/files", "query": "path=%2F&name=iptvcodes.hopto.org.txt&operation=find", "ip": "[::1]:57033", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.5613084, "time": "2025-04-30T17:37:28Z"}
30/04/2025 11:07 PM     INFO            {"status": 200, "method": "GET", "path": "/api/uploads/6aa505600a13b6daf244f0c28d3d1e56", "query": "", "ip": "[::1]:57033", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.2835487, "time": "2025-04-30T17:37:29Z"}
30/04/2025 11:07 PM     ERROR           {"status": 500, "method": "POST", "path": "/api/uploads/6aa505600a13b6daf244f0c28d3d1e56", "query": "partName=iptvcodes.hopto.org.txt&fileName=iptvcodes.hopto.org.txt&partNo=1&encrypted=false&channelId=0", "ip": "[::1]:57033", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.2865181, "time": "2025-04-30T17:37:29Z"}
30/04/2025 11:07 PM     ERROR           {"status": 400, "method": "POST", "path": "/api/files", "query": "", "ip": "[::1]:57033", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.0023511, "time": "2025-04-30T17:37:29Z"}
30/04/2025 11:07 PM     INFO            {"status": 200, "method": "GET", "path": "/api/files", "query": "page=1&order=desc&sort=updatedAt&operation=find&shared=true", "ip": "[::1]:57033", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", "latency": 0.5772957, "time": "2025-04-30T17:37:34Z"}

Troubleshooting Steps Taken:

  • Verified app-id/hash are correct via Telegram Core website.

  • Confirmed database connection is working (migrations run, initial UI loads).

  • Tried running Teldrive from different directories.

  • Tried running Teldrive with Administrator privileges.

  • Explicitly set session_file path in config.toml to various locations (user home, .teldrive subfolder, different file names).

  • Verified hidden files and system files are visible in Explorer; session file is definitely not present after login.

-Checked Antivirus logs; no obvious blocks found related to Teldrive or session files.

  • Confirmed another unrelated Telegram application using gotd or a similar library can successfully create its .session file in the same user directory.

  • Set upload_chat to both "me" and the numerical User ID; uploads fail identically likely due to the missing/unloaded session.

Abhaikumar007 avatar Apr 30 '25 17:04 Abhaikumar007

because maybe you didn't create a channel in telegram and you didn't sync, from settings in teldrive, this channel.

check it

RobertusIT avatar May 24 '25 09:05 RobertusIT