|
1 | 1 | import type { Context } from 'hono'; |
2 | 2 | import { stream } from 'hono/streaming'; |
3 | | -import type { TelegramClient } from 'telegram'; |
4 | | -import { Api } from 'telegram'; |
5 | | -import type { IterDownloadFunction } from 'telegram/client/downloads.js'; |
6 | | -import { returnBigInt as bigInt } from 'telegram/Helpers.js'; |
7 | | -import { getAppropriatedPartSize } from 'telegram/Utils.js'; |
| 3 | +import type { TelegramClient } from 'teleproto'; |
| 4 | +import { Api } from 'teleproto'; |
| 5 | +import { returnBigInt as bigInt } from 'teleproto/Helpers.js'; |
8 | 6 |
|
9 | 7 | import { config } from '@/config'; |
10 | 8 | import InvalidParameterError from '@/errors/types/invalid-parameter'; |
@@ -86,30 +84,32 @@ export async function* streamThumbnail(client: TelegramClient, doc: Api.Document |
86 | 84 | } |
87 | 85 |
|
88 | 86 | export async function* streamDocument(client: TelegramClient, obj: Api.Document, thumbSize = '', offset?: bigInt.BigInteger, limit?: bigInt.BigInteger) { |
89 | | - const chunkSize = (obj.size ? getAppropriatedPartSize(obj.size) : 64) * 1024; |
90 | | - const iterFileParams: IterDownloadFunction = { |
91 | | - file: new Api.InputDocumentFileLocation({ |
| 87 | + const requestSize = 512 * 1024; // MAX_CHUNK_SIZE |
| 88 | + let skip = offset ? offset.mod(requestSize).toJSNumber() : 0; |
| 89 | + const alignedOffset = offset?.subtract(skip); |
| 90 | + // console.log('starting iterDownload'); |
| 91 | + const chunks = client.iterDownload( |
| 92 | + new Api.InputDocumentFileLocation({ |
92 | 93 | id: obj.id, |
93 | 94 | accessHash: obj.accessHash, |
94 | 95 | fileReference: obj.fileReference, |
95 | 96 | thumbSize, |
96 | 97 | }), |
97 | | - chunkSize, |
98 | | - requestSize: 512 * 1024, // MAX_CHUNK_SIZE |
99 | | - dcId: obj.dcId, |
100 | | - offset: undefined, |
101 | | - limit: undefined, |
102 | | - }; |
103 | | - if (offset) { |
104 | | - iterFileParams.offset = offset; |
105 | | - } |
106 | | - if (limit) { |
107 | | - iterFileParams.limit = limit.valueOf(); |
| 98 | + { |
| 99 | + requestSize, |
| 100 | + dcId: obj.dcId, |
| 101 | + offset: alignedOffset, |
| 102 | + limit: limit && alignedOffset ? limit.subtract(alignedOffset).add(1).valueOf() : undefined, |
| 103 | + } |
| 104 | + ); |
| 105 | + for await (const chunk of chunks) { |
| 106 | + if (skip >= chunk.length) { |
| 107 | + skip -= chunk.length; |
| 108 | + continue; |
| 109 | + } |
| 110 | + yield skip ? chunk.subarray(skip) : chunk; |
| 111 | + skip = 0; |
108 | 112 | } |
109 | | - // console.log('starting iterDownload'); |
110 | | - const stream = client.iterDownload(iterFileParams); |
111 | | - yield* stream; |
112 | | - await stream.close(); |
113 | 113 | } |
114 | 114 |
|
115 | 115 | function parseRange(range: string, length: bigInt.BigInteger) { |
|
0 commit comments