@yaebal/media-cache 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +31 -0
- package/lib/index.js.map +1 -0
- package/lib/index.test.d.ts +2 -0
- package/lib/index.test.d.ts.map +1 -0
- package/lib/index.test.js +48 -0
- package/lib/index.test.js.map +1 -0
- package/package.json +49 -0
- package/src/index.test.ts +56 -0
- package/src/index.ts +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 neverlane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @yaebal/media-cache
|
|
2
|
+
|
|
3
|
+
where to keep `key → file_id`. defaults to in-memory (lost on restart).
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @yaebal/media-cache
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Context, type MediaSource, type Message } from "@yaebal/core";
|
|
2
|
+
import { type StorageAdapter } from "@yaebal/session";
|
|
3
|
+
export interface MediaCacheOptions {
|
|
4
|
+
/** where to keep `key → file_id`. defaults to in-memory (lost on restart). */
|
|
5
|
+
storage?: StorageAdapter<string>;
|
|
6
|
+
}
|
|
7
|
+
export interface MediaCache {
|
|
8
|
+
/** send a photo, caching its `file_id` under `key` to skip future uploads. */
|
|
9
|
+
photo(ctx: Context, key: string, source: MediaSource | string, extra?: Record<string, unknown>): Promise<Message>;
|
|
10
|
+
/** send a document, caching its `file_id` under `key`. */
|
|
11
|
+
document(ctx: Context, key: string, source: MediaSource | string, extra?: Record<string, unknown>): Promise<Message>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* the first time you send a local file under a `key`, telegram returns a
|
|
15
|
+
* `file_id`; this caches it and reuses it on later sends — no re-upload.
|
|
16
|
+
* caller-supplied keys, so it's correct under concurrency (unlike a transparent
|
|
17
|
+
* hook that has to guess which upload produced which id).
|
|
18
|
+
*/
|
|
19
|
+
export declare function mediaCache(options?: MediaCacheOptions): MediaCache;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,OAAO,EAAS,MAAM,cAAc,CAAC;AACnF,OAAO,EAAiB,KAAK,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAErE,MAAM,WAAW,iBAAiB;IACjC,8EAA8E;IAC9E,OAAO,CAAC,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,UAAU;IAC1B,8EAA8E;IAC9E,KAAK,CACJ,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,WAAW,GAAG,MAAM,EAC5B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAAC,OAAO,CAAC,CAAC;IACpB,0DAA0D;IAC1D,QAAQ,CACP,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,WAAW,GAAG,MAAM,EAC5B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAAC,OAAO,CAAC,CAAC;CACpB;AASD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,UAAU,CA6BtE"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { media } from "@yaebal/core";
|
|
2
|
+
import { MemoryStorage } from "@yaebal/session";
|
|
3
|
+
function extractFileId(result, field) {
|
|
4
|
+
const value = result[field];
|
|
5
|
+
const node = Array.isArray(value) ? value[value.length - 1] : value;
|
|
6
|
+
return node?.file_id;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* the first time you send a local file under a `key`, telegram returns a
|
|
10
|
+
* `file_id`; this caches it and reuses it on later sends — no re-upload.
|
|
11
|
+
* caller-supplied keys, so it's correct under concurrency (unlike a transparent
|
|
12
|
+
* hook that has to guess which upload produced which id).
|
|
13
|
+
*/
|
|
14
|
+
export function mediaCache(options = {}) {
|
|
15
|
+
const storage = options.storage ?? new MemoryStorage();
|
|
16
|
+
const send = async (ctx, key, source, field, sender, extra) => {
|
|
17
|
+
const cached = await storage.get(key);
|
|
18
|
+
const result = await sender(cached ? media.fileId(cached) : source, extra);
|
|
19
|
+
if (!cached) {
|
|
20
|
+
const id = extractFileId(result, field);
|
|
21
|
+
if (id)
|
|
22
|
+
await storage.set(key, id);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
photo: (ctx, key, source, extra) => send(ctx, key, source, "photo", (s, e) => ctx.sendPhoto(s, e), extra),
|
|
28
|
+
document: (ctx, key, source, extra) => send(ctx, key, source, "document", (s, e) => ctx.sendDocument(s, e), extra),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgD,KAAK,EAAE,MAAM,cAAc,CAAC;AACnF,OAAO,EAAE,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AAwBrE,SAAS,aAAa,CAAC,MAAe,EAAE,KAAa;IACpD,MAAM,KAAK,GAAI,MAA6C,CAAC,KAAK,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAEpE,OAAQ,IAAyC,EAAE,OAAO,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,UAA6B,EAAE;IACzD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,IAAI,aAAa,EAAU,CAAC;IAE/D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 -0,0 +1,48 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { media } from "@yaebal/core";
|
|
4
|
+
import { mediaCache } from "./index.js";
|
|
5
|
+
test("first send uploads the source, later sends reuse the cached file_id", async () => {
|
|
6
|
+
const sent = [];
|
|
7
|
+
const ctx = {
|
|
8
|
+
sendPhoto: (src) => {
|
|
9
|
+
sent.push(src);
|
|
10
|
+
return Promise.resolve({ message_id: 1, photo: [{ file_id: "SMALL" }, { file_id: "BIG" }] });
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
const cache = mediaCache();
|
|
14
|
+
await cache.photo(ctx, "logo", media.path("./logo.png"));
|
|
15
|
+
await cache.photo(ctx, "logo", media.path("./logo.png"));
|
|
16
|
+
assert.equal(sent[0]?.kind, "path"); // first time → uploaded
|
|
17
|
+
assert.equal(sent[1]?.kind, "fileId"); // second time → cached
|
|
18
|
+
assert.equal(sent[1].fileId, "BIG"); // last/biggest size
|
|
19
|
+
});
|
|
20
|
+
test("document caches file_id from the document field", async () => {
|
|
21
|
+
const sent = [];
|
|
22
|
+
const ctx = {
|
|
23
|
+
sendDocument: (src) => {
|
|
24
|
+
sent.push(src);
|
|
25
|
+
return Promise.resolve({ message_id: 1, document: { file_id: "DOC" } });
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const cache = mediaCache();
|
|
29
|
+
await cache.document(ctx, "report", media.path("./r.pdf"));
|
|
30
|
+
await cache.document(ctx, "report", media.path("./r.pdf"));
|
|
31
|
+
assert.equal(sent[1]?.kind, "fileId");
|
|
32
|
+
assert.equal(sent[1].fileId, "DOC");
|
|
33
|
+
});
|
|
34
|
+
test("distinct keys cache independently", async () => {
|
|
35
|
+
const sent = [];
|
|
36
|
+
const ctx = {
|
|
37
|
+
sendPhoto: (src) => {
|
|
38
|
+
sent.push(src);
|
|
39
|
+
return Promise.resolve({ message_id: 1, photo: [{ file_id: "X" }] });
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const cache = mediaCache();
|
|
43
|
+
await cache.photo(ctx, "a", media.path("./a.png"));
|
|
44
|
+
await cache.photo(ctx, "b", media.path("./b.png")); // different key → uploads
|
|
45
|
+
assert.equal(sent[0]?.kind, "path");
|
|
46
|
+
assert.equal(sent[1]?.kind, "path");
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAkC,KAAK,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,IAAI,CAAC,qEAAqE,EAAE,KAAK,IAAI,EAAE;IACtF,MAAM,IAAI,GAAkB,EAAE,CAAC;IAC/B,MAAM,GAAG,GAAG;QACX,SAAS,EAAE,CAAC,GAAgB,EAAE,EAAE;YAC/B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACf,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QAC9F,CAAC;KACqB,CAAC;IAExB,MAAM,KAAK,GAAG,UAAU,EAAE,CAAC;IAC3B,MAAM,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;IACzD,MAAM,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;IAEzD,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,C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AG,GAAG;QACX,SAAS,EAAE,CAAC,GAAgB,EAAE,EAAE;YAC/B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACf,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;QACtE,CAAC;KACqB,CAAC;IAExB,MAAM,KAAK,GAAG,UAAU,EAAE,CAAC;IAC3B,MAAM,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IACnD,MAAM,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,0BAA0B;IAE9E,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACpC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yaebal/media-cache",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "yaebal media-cache — reuse a file_id instead of re-uploading the same file.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@yaebal/core": "0.0.1",
|
|
20
|
+
"@yaebal/session": "0.0.1"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "latest"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"telegram",
|
|
30
|
+
"telegram-bot",
|
|
31
|
+
"yaebal",
|
|
32
|
+
"media-cache",
|
|
33
|
+
"file-id"
|
|
34
|
+
],
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/neverlane/yaebal",
|
|
39
|
+
"directory": "packages/media-cache"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.json",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
47
|
+
"test": "node --test lib"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { type Context, type MediaSource, media } from "@yaebal/core";
|
|
4
|
+
import { mediaCache } from "./index.js";
|
|
5
|
+
|
|
6
|
+
test("first send uploads the source, later sends reuse the cached file_id", async () => {
|
|
7
|
+
const sent: MediaSource[] = [];
|
|
8
|
+
const ctx = {
|
|
9
|
+
sendPhoto: (src: MediaSource) => {
|
|
10
|
+
sent.push(src);
|
|
11
|
+
return Promise.resolve({ message_id: 1, photo: [{ file_id: "SMALL" }, { file_id: "BIG" }] });
|
|
12
|
+
},
|
|
13
|
+
} as unknown as Context;
|
|
14
|
+
|
|
15
|
+
const cache = mediaCache();
|
|
16
|
+
await cache.photo(ctx, "logo", media.path("./logo.png"));
|
|
17
|
+
await cache.photo(ctx, "logo", media.path("./logo.png"));
|
|
18
|
+
|
|
19
|
+
assert.equal(sent[0]?.kind, "path"); // first time → uploaded
|
|
20
|
+
assert.equal(sent[1]?.kind, "fileId"); // second time → cached
|
|
21
|
+
assert.equal((sent[1] as { fileId: string }).fileId, "BIG"); // last/biggest size
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("document caches file_id from the document field", async () => {
|
|
25
|
+
const sent: MediaSource[] = [];
|
|
26
|
+
const ctx = {
|
|
27
|
+
sendDocument: (src: MediaSource) => {
|
|
28
|
+
sent.push(src);
|
|
29
|
+
return Promise.resolve({ message_id: 1, document: { file_id: "DOC" } });
|
|
30
|
+
},
|
|
31
|
+
} as unknown as Context;
|
|
32
|
+
|
|
33
|
+
const cache = mediaCache();
|
|
34
|
+
await cache.document(ctx, "report", media.path("./r.pdf"));
|
|
35
|
+
await cache.document(ctx, "report", media.path("./r.pdf"));
|
|
36
|
+
|
|
37
|
+
assert.equal(sent[1]?.kind, "fileId");
|
|
38
|
+
assert.equal((sent[1] as { fileId: string }).fileId, "DOC");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("distinct keys cache independently", async () => {
|
|
42
|
+
const sent: MediaSource[] = [];
|
|
43
|
+
const ctx = {
|
|
44
|
+
sendPhoto: (src: MediaSource) => {
|
|
45
|
+
sent.push(src);
|
|
46
|
+
return Promise.resolve({ message_id: 1, photo: [{ file_id: "X" }] });
|
|
47
|
+
},
|
|
48
|
+
} as unknown as Context;
|
|
49
|
+
|
|
50
|
+
const cache = mediaCache();
|
|
51
|
+
await cache.photo(ctx, "a", media.path("./a.png"));
|
|
52
|
+
await cache.photo(ctx, "b", media.path("./b.png")); // different key → uploads
|
|
53
|
+
|
|
54
|
+
assert.equal(sent[0]?.kind, "path");
|
|
55
|
+
assert.equal(sent[1]?.kind, "path");
|
|
56
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Context, type MediaSource, type Message, media } from "@yaebal/core";
|
|
2
|
+
import { MemoryStorage, type StorageAdapter } from "@yaebal/session";
|
|
3
|
+
|
|
4
|
+
export interface MediaCacheOptions {
|
|
5
|
+
/** where to keep `key → file_id`. defaults to in-memory (lost on restart). */
|
|
6
|
+
storage?: StorageAdapter<string>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface MediaCache {
|
|
10
|
+
/** send a photo, caching its `file_id` under `key` to skip future uploads. */
|
|
11
|
+
photo(
|
|
12
|
+
ctx: Context,
|
|
13
|
+
key: string,
|
|
14
|
+
source: MediaSource | string,
|
|
15
|
+
extra?: Record<string, unknown>,
|
|
16
|
+
): Promise<Message>;
|
|
17
|
+
/** send a document, caching its `file_id` under `key`. */
|
|
18
|
+
document(
|
|
19
|
+
ctx: Context,
|
|
20
|
+
key: string,
|
|
21
|
+
source: MediaSource | string,
|
|
22
|
+
extra?: Record<string, unknown>,
|
|
23
|
+
): Promise<Message>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function extractFileId(result: Message, field: string): string | undefined {
|
|
27
|
+
const value = (result as unknown as Record<string, unknown>)[field];
|
|
28
|
+
const node = Array.isArray(value) ? value[value.length - 1] : value;
|
|
29
|
+
|
|
30
|
+
return (node as { file_id?: string } | undefined)?.file_id;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* the first time you send a local file under a `key`, telegram returns a
|
|
35
|
+
* `file_id`; this caches it and reuses it on later sends — no re-upload.
|
|
36
|
+
* caller-supplied keys, so it's correct under concurrency (unlike a transparent
|
|
37
|
+
* hook that has to guess which upload produced which id).
|
|
38
|
+
*/
|
|
39
|
+
export function mediaCache(options: MediaCacheOptions = {}): MediaCache {
|
|
40
|
+
const storage = options.storage ?? new MemoryStorage<string>();
|
|
41
|
+
|
|
42
|
+
const send = async (
|
|
43
|
+
ctx: Context,
|
|
44
|
+
key: string,
|
|
45
|
+
source: MediaSource | string,
|
|
46
|
+
field: "photo" | "document",
|
|
47
|
+
sender: (src: MediaSource | string, extra?: Record<string, unknown>) => Promise<Message>,
|
|
48
|
+
extra?: Record<string, unknown>,
|
|
49
|
+
): Promise<Message> => {
|
|
50
|
+
const cached = await storage.get(key);
|
|
51
|
+
const result = await sender(cached ? media.fileId(cached) : source, extra);
|
|
52
|
+
|
|
53
|
+
if (!cached) {
|
|
54
|
+
const id = extractFileId(result, field);
|
|
55
|
+
|
|
56
|
+
if (id) await storage.set(key, id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return result;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
photo: (ctx, key, source, extra) =>
|
|
64
|
+
send(ctx, key, source, "photo", (s, e) => ctx.sendPhoto(s, e), extra),
|
|
65
|
+
document: (ctx, key, source, extra) =>
|
|
66
|
+
send(ctx, key, source, "document", (s, e) => ctx.sendDocument(s, e), extra),
|
|
67
|
+
};
|
|
68
|
+
}
|