Skip to content

Commit 76cc7c8

Browse files
author
John Doe
committed
add file download
1 parent 8e6ad21 commit 76cc7c8

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Changelog
22

3+
#### tglib v3.0.3
4+
5+
- Added a new high level API `client.tg.download` for downloading files
6+
7+
-----
8+
39
#### tglib v3.0.2
410

511
- Fix WASM verbosity setting for TDLib 1.4.0 (provides official WASM suppor & example)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tglib",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"author": "nodegin",
55
"license": "MIT",
66
"description": "TDLib (Telegram Database library) bindings for Node.js",

src/ClientBase.js

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Client {
3535
this.client = null
3636
this.tg = new TG(this)
3737
this.hijackers = {}
38+
this.downloading = {}
3839
this.fetching = {}
3940
this.callbacks = {
4041
'td:update': () => {},
@@ -213,6 +214,20 @@ class Client {
213214
return
214215
}
215216
switch (update['@type']) {
217+
case 'updateFile': {
218+
const fileId = update['file']['id']
219+
if (!this.downloading[fileId]) {
220+
// default handle
221+
this.callbacks['td:update'].call(null, update)
222+
return
223+
}
224+
if (!update['file']['local']['path'].length) {
225+
// not yet downloaded
226+
return
227+
}
228+
// resolve downloaded
229+
this.downloading[fileId](update)
230+
}
216231
case 'updateOption': {
217232
if (update['name'] === 'my_id' && update['value']['@type'] === 'optionValueEmpty') {
218233
// session has been signed out

src/TG.js

+23
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,29 @@ class TG {
242242
return chat
243243
}
244244

245+
/*
246+
* Download a file
247+
*/
248+
async download(remoteFileId) {
249+
const { id } = await this.client.fetch({
250+
'@type': 'getRemoteFile',
251+
'remote_file_id': remoteFileId,
252+
})
253+
let file = await this.client.fetch({
254+
'@type': 'downloadFile',
255+
'file_id': id,
256+
'priority': 1,
257+
})
258+
if (!file['local']['path'].length) {
259+
const downloadPromise = new Promise((resolve) => {
260+
this.client.downloading[id] = resolve
261+
})
262+
file = await downloadPromise
263+
}
264+
return file
265+
}
266+
267+
245268
/*
246269
* Call an user
247270
*/

0 commit comments

Comments
 (0)