Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(telegram): Invalid URL when enabled files.local #278

Merged
merged 2 commits into from
May 30, 2024
Merged

fix(telegram): Invalid URL when enabled files.local #278

merged 2 commits into from
May 30, 2024

Conversation

su226
Copy link
Contributor

@su226 su226 commented May 29, 2024

I run telegram-bot-api with --local argument and enable files.local config in Koishi. When I use any API that returns a file (e.g. getUser will return user's avatar), I will get a TypeError: Invalid URL error like this.

1000|2|2024-05-29 13:20:18 [W] telegram get file error TypeError: Invalid URL: /stor/opt/bot/telegram/***/profile_photos/file_52.jpg at Proxy.resolveURL (/home/su226/.koishi/data/instances/default/node_modules/@cordisjs/plugin-http/lib/index.cjs:250:13) at [cordis.invoke] (/home/su226/.koishi/data/instances/default/node_modules/@cordisjs/plugin-http/lib/index.cjs:278:22) at applyTraceable (/home/su226/.koishi/data/instances/default/node_modules/@cordisjs/core/lib/index.cjs:263:) at Object.apply (/home/su226/.koishi/data/instances/default/node_modules/@cordisjs/core/lib/index.cjs:254:14) at Proxy.file (/home/su226/.koishi/data/instances/default/node_modules/@cordisjs/plugin-http/lib/index.cjs:404:59)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Proxy.$getFile (/home/su226/.koishi/data/instances/default/node_modules/@satorijs/adapter-telegram/lib/index.cjs:941:14) at async Proxy.$getFileFromPath (/home/su226/.koishi/data/instances/default/node_modules/@satorijs/adapter-telegram/lib/index.cjs:958:26) at async Proxy.$getFileFromId (/home/su226/.koishi/data/instances/default/node_modules/@satorijs/adapter-telegram/lib/index.cjs:949:14) at async Proxy.getUser (/home/su226/.koishi/data/instances/default/node_modules/@satorijs/adapter-telegram/lib/index.cjs:982:21)

No any \n in the log as above. *** in the path is my bot token.

The PR fixes this, tested on Windows and Linux.

@@ -144,7 +144,7 @@ export class TelegramBot<C extends Context = Context, T extends TelegramBot.Conf

async $getFile(filePath: string) {
if (this.local) {
return await this.ctx.http.file(filePath)
return await this.ctx.http.file(`file://${filePath}`)
Copy link
Contributor

@idranme idranme May 30, 2024

Choose a reason for hiding this comment

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

On Windows, should file:/// be used?
On Windows, the first character of the path is the disk drive letter, not /.

Copy link
Contributor

@shigma shigma May 30, 2024

Choose a reason for hiding this comment

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

Actuall it should be pathToFileURL. I'll fix it.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actuall it should be pathToFileURL. I'll fix it.

Is #78 still relevant? I mean if it is, maybe you should not directly use Node APIs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is #78 still relevant? I mean if it is, maybe you should not directly use Node APIs.

pathToFileURL can be ponyfilled in the browser.

https://github.com/cordiverse/ponyfills/blob/main/packages/url/src/index.ts

@shigma shigma merged commit 1835b5e into satorijs:master May 30, 2024
4 checks passed
@shigma shigma changed the title fix(telegram): Invalid URL when enabled files.local fix(telegram): Invalid URL when enabled files.local May 30, 2024
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.

3 participants