-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.ts
104 lines (102 loc) · 3.54 KB
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {
copyFile,
createTempFile,
downloadFile,
fetchFile,
File,
isAbsolutePath,
readFile,
} from "./deps.deno.ts";
export interface FileX {
/** Computes a URL from the `file_path` property of this file object. The
* URL can be used to download the file contents.
*
* If you are using a local Bot API server, then this method will return the
* file path that identifies the local file on your system.
*
* If the `file_path` of this file object is `undefined`, this method will
* throw an error.
*
* Note that this method is installed by grammY on [the File
* object](https://core.telegram.org/bots/api#file).
*/
getUrl(): string;
/**
* This method will download the file from the Telegram servers and store it
* under the given file path on your system. It returns the absolute path to
* the created file, so this may be the same value as the argument to the
* function.
*
* If you omit the path argument to this function, then a temporary file
* will be created for you. This path will still be returned, hence giving
* you access to the downloaded file.
*
* If you are using a local Bot API server, then the local file will be
* copied over to the specified path, or to a new temporary location.
*
* If the `file_path` of this file object is `undefined`, this method will
* throw an error.
*
* Note that this method is installed by grammY on [the File
* object](https://core.telegram.org/bots/api#file).
*
* @param path Optional path to store the file (default: temporary file)
* @returns An absolute file path to the downloaded/copied file
*/
download(path?: string): Promise<string>;
/**
* This method will fetch the file URL and return an async iterator which
* yields every time a new chunk of data is read.
*
* If the `file_path` of this file object is `undefined`, this method will
* throw an error.
*
* @example
* ```ts
* bot.on([":video", ":animation"], async (ctx) => {
* // Prepare file for download
* const file = await ctx.getFile();
* // Print the size of each chunk
* for await (const chunk of file) {
* console.log(`Read ${chunk.length} bytes`);
* }
* });
* ```
*
* @returns Async iterator for the received data
*/
[Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
}
export function getFileMethods(
linkBuilder: (path: string) => string | URL,
) {
const methods: FileX = {
getUrl(this: File) {
const path = this.file_path;
if (path === undefined) {
const id = this.file_id;
throw new Error(`File path is not available for file '${id}'`);
}
if (isAbsolutePath(path)) return path;
const link = linkBuilder(path);
if (link instanceof URL) return link.href;
return link;
},
async download(path?: string) {
const url = this.getUrl();
path ??= await createTempFile();
if (isAbsolutePath(url)) await copyFile(url, path);
else await downloadFile(url, path);
return path;
},
async *[Symbol.asyncIterator]() {
const url = this.getUrl();
if (isAbsolutePath(url)) {
yield* readFile(url);
} else {
yield* await fetchFile(url);
}
},
};
return methods;
}