File tree 4 files changed +45
-1
lines changed
4 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1
1
## Changelog
2
2
3
+ #### tglib v3.0.3
4
+
5
+ - Added a new high level API ` client.tg.download ` for downloading files
6
+
7
+ -----
8
+
3
9
#### tglib v3.0.2
4
10
5
11
- Fix WASM verbosity setting for TDLib 1.4.0 (provides official WASM suppor & example)
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " tglib" ,
3
- "version" : " 3.0.2 " ,
3
+ "version" : " 3.0.3 " ,
4
4
"author" : " nodegin" ,
5
5
"license" : " MIT" ,
6
6
"description" : " TDLib (Telegram Database library) bindings for Node.js" ,
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ class Client {
35
35
this . client = null
36
36
this . tg = new TG ( this )
37
37
this . hijackers = { }
38
+ this . downloading = { }
38
39
this . fetching = { }
39
40
this . callbacks = {
40
41
'td:update' : ( ) => { } ,
@@ -213,6 +214,20 @@ class Client {
213
214
return
214
215
}
215
216
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
+ }
216
231
case 'updateOption' : {
217
232
if ( update [ 'name' ] === 'my_id' && update [ 'value' ] [ '@type' ] === 'optionValueEmpty' ) {
218
233
// session has been signed out
Original file line number Diff line number Diff line change @@ -242,6 +242,29 @@ class TG {
242
242
return chat
243
243
}
244
244
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
+
245
268
/*
246
269
* Call an user
247
270
*/
You can’t perform that action at this time.
0 commit comments