Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

HTTP API for Telegram File Download - Complete ✅

Changes in This Update

Fixed Album Rule (IS-ALBUM / NEW-FOR-ALBUM) Handling:

  • Properly detects and handles NeedNewForAlbum() rule (when dirPath is "NEW-FOR-ALBUM")
  • Groups files by album ID (GetGroupedID()) when NEW-FOR-ALBUM rule applies
  • Creates subdirectory named after first file (without extension) for album files
  • All files in the same album go into that subdirectory
  • Single files or non-grouped files with NEW-FOR-ALBUM rule are treated as regular files

Improved Rule Application:

  • Uses ruleutil.MatchedDirPath type properly for directory path handling
  • Correctly checks dirPath.NeedNewForAlbum() instead of string comparison
  • Maintains separate tracking for album files vs regular files

Album Grouping Logic (matching bot handler behavior):

  1. Apply rules to each file to determine storage and directory path
  2. If dirPath is NEW-FOR-ALBUM and file is in an album group:
    • Collect all files from the same album (grouped by groupId)
    • Create subdirectory using first file's name (without extension)
    • Place all album files in that subdirectory
  3. If dirPath is NEW-FOR-ALBUM but file is not in a group:
    • Log warning and treat as regular file
  4. Regular files (not NEW-FOR-ALBUM) are processed immediately

Example Album Handling:

  • User has rule: IS-ALBUM files go to "NEW-FOR-ALBUM"
  • API receives media group with 3 photos: photo1.jpg, photo2.jpg, photo3.jpg
  • Result: All files go to /downloads/photo1/ directory:
    • /downloads/photo1/photo1.jpg
    • /downloads/photo1/photo2.jpg
    • /downloads/photo1/photo3.jpg

Consistency with Bot Handlers:
Now matches CreateAndAddBatchTGFileTaskWithEdit logic exactly:

  • ✅ Checks dirPath.NeedNewForAlbum()
  • ✅ Groups files by groupId for albums
  • ✅ Creates subdirectory from first file's basename
  • ✅ Uses first file's storage for all album files
  • ✅ Falls back to base directory for non-grouped files
Original prompt

This section details on the original issue you should resolve

<issue_title>能否增加一个api接口下载文件</issue_title>
<issue_description>### ⭐️ Feature description

能否增加一个通过http api接口传入tg链接 自动下载文件 完成后回调的功能

🌈 Your view

No response

🧐 Code example

```</issue_description>

<agent_instructions>Use standard libs to implement this,. The design should be elegant and standardized, conforming to RESTful best practices.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@krau</author><body>
说下你对该接口的具体想法 (例如, 路由路径, 请求和响应格式, 任务回调机制, 能力边界等等)</body></comment_new>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 6 commits January 19, 2026 04:42
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
…es and handle errors properly

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
…text-aware logging

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API endpoint for downloading files via HTTP Add HTTP API for programmatic file downloads from Telegram links Jan 19, 2026
Copilot AI requested a review from krau January 19, 2026 04:54
Copy link
Owner

@krau krau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that your code is not formatted.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taskStatus should also contain progress

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added progress tracking in commit 20a5e31. TaskStatusResponse now includes downloaded, total, and progress_pct fields that are updated in real-time during file downloads.

…s tracking

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

Note that your code is not formatted.

Code formatted with go fmt in commit 20a5e31.

Copilot AI requested a review from krau January 19, 2026 05:28
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need a trusted IP mechanism, which will increase the difficulty of use. We only need a token authentication mechanism.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed IP whitelist mechanism in commit 173a5e3. Authentication is now simplified to token-only, making the API easier to use while maintaining security through Bearer token authentication.

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copilot AI requested a review from krau January 19, 2026 05:43
Copy link
Owner

@krau krau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Startup should be refused and a warning should be displayed when the API token is configured to be empty,

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

Startup should be refused and a warning should be displayed when the API token is configured to be empty,

Added validation in commit 3a6402a. The API server now refuses to start if enabled without a token, displaying: "API is enabled but token is not configured. Please set 'api.token' in your configuration file for security"

Copilot AI requested a review from krau January 19, 2026 05:50
Copy link
Owner

@krau krau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because OnProgress is called frequently, the current implementation causes serious performance and deadlock issues.

Furthermore, the filename handling is inconsistent with the implementation within the bot handler. Please refer to the original code.

Finally, we also need to handle media group messages, still referring to the original code.

… handling

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

Because OnProgress is called frequently, the current implementation causes serious performance and deadlock issues.

Furthermore, the filename handling is inconsistent with the implementation within...

Fixed in commit 6896bdc:

  1. Performance/Deadlock: OnProgress now uses atomic operations (atomic.Int64 and atomic float64 via math.Float64bits) instead of mutex locks
  2. Filename Handling: Now uses tgutil.GenFileNameFromMessage with proper user database retrieval, matching bot handler implementation
  3. Media Group Support: Added support via GetGroupedMessages, returns multiple task_ids for grouped messages, supports ?single query parameter

Copilot AI requested a review from krau January 19, 2026 06:16
Copy link
Owner

@krau krau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task creation logic remains inconsistent with the original, and storage rules have not been considered. You should carefully review the original code to ensure that tasks created via the API and tasks created directly using the bot behave exactly the same.

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

The task creation logic remains inconsistent with the original, and storage rules have not been considered. You should carefully review the original code to ensure that tasks created via the API and t...

Fixed in commit e1b4087. API task creation now applies storage rules exactly like bot handlers:

  • Checks user.ApplyRule and user.Rules
  • Applies ruleutil.ApplyRule() to each file
  • Rules can override storage name and directory path based on filename/message regex patterns
  • Falls back to requested storage/path if rule doesn't match or rule-specified storage unavailable

Copilot AI requested a review from krau January 19, 2026 07:18
Copy link
Owner

@krau krau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still inconsistent. IS-ALBUM rule and filenames are not being handled correctly.

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

Still inconsistent. IS-ALBUM rule and filenames are not being handled correctly.

Fixed in commit ef1ecf9:

  • IS-ALBUM rule: Now properly checks dirPath.NeedNewForAlbum() instead of string comparison
  • Album grouping: Files with NEW-FOR-ALBUM rule are grouped by groupId, creating subdirectory from first file's basename (without extension)
  • Filename handling: Uses ruleutil.MatchedDirPath type properly, matches bot handler logic exactly from CreateAndAddBatchTGFileTaskWithEdit

Copilot AI requested a review from krau January 19, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

能否增加一个api接口下载文件

2 participants