/
channel.ts
166 lines (150 loc) · 5.58 KB
/
channel.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// @ts-nocheck
import wait from '@/utils/wait';
import { config } from '@/config';
const { client, decodeMedia, getFilename, getMediaLink, streamDocument, streamThumbnail } = require('./client');
const bigInt = require('telegram/Helpers').returnBigInt;
const HTMLParser = require('telegram/extensions/html').HTMLParser;
function parseRange(range, length) {
if (!range) {
return [];
}
const [typ, segstr] = range.split('=');
if (typ !== 'bytes') {
throw `unsupported range: ${typ}`;
}
const segs = segstr.split(',').map((s) => s.trim());
const parsedSegs = [];
for (const seg of segs) {
const range = seg
.split('-', 2)
.filter((v) => !!v)
.map(bigInt);
if (range.length < 2) {
if (seg.startsWith('-')) {
range.unshift(0);
} else {
range.push(length);
}
}
parsedSegs.push(range);
}
return parsedSegs;
}
async function getMedia(ctx) {
const media = await decodeMedia(ctx.req.param('username'), ctx.req.param('media'));
if (!media) {
ctx.status = 500;
return ctx.res.end();
}
if (ctx.res.closed) {
// console.log(`prematurely closed ${ctx.req.param('media')}`);
return;
}
if (media.document) {
ctx.status = 200;
let stream;
if ('thumb' in ctx.req.query()) {
try {
stream = streamThumbnail(media);
ctx.set('Content-Type', 'image/jpeg');
} catch {
ctx.status = 404;
return ctx.res.end();
}
} else {
ctx.set('Content-Type', media.document.mimeType);
ctx.set('Accept-Ranges', 'bytes');
const range = parseRange(ctx.get('Range'), media.document.size - 1);
if (range.length > 1) {
ctx.status = 416; // range not satisfiable
return ctx.res.end();
}
if (range.length === 1) {
// console.log(`${ctx.method} ${ctx.req.url} Range: ${ctx.get('Range')}`);
ctx.status = 206; // partial content
const [offset, limit] = range[0];
ctx.set('Content-Length', limit - offset + 1);
ctx.set('Content-Range', `bytes ${offset}-${limit}/${media.document.size}`);
const stream = streamDocument(media.document, '', offset, limit);
for await (const chunk of stream) {
ctx.res.write(chunk);
if (ctx.res.closed) {
break;
}
}
return ctx.res.end();
}
ctx.set('Content-Length', media.document.size);
if (media.document.mimeType.startsWith('application/')) {
ctx.set('Content-Disposition', `attachment; filename="${encodeURIComponent(getFilename(media))}"`);
}
stream = streamDocument(media.document);
}
// const addr = JSON.stringify(ctx.res.socket.address());
// console.log(`streaming ${ctx.req.param('media')} to ${addr}`);
for await (const chunk of stream) {
if (ctx.res.closed) {
// console.log(`closed ${addr}`);
break;
}
// console.log(`writing ${chunk.length / 1024} to ${addr}`);
ctx.res.write(chunk);
}
if ('close' in stream) {
stream.close();
}
} else if (media.photo) {
ctx.status = 200;
ctx.set('Content-Type', 'image/jpeg');
const buf = await client.downloadMedia(media);
ctx.res.write(buf);
} else {
ctx.status = 415;
ctx.write(media.className);
}
return ctx.res.end();
}
export default async (ctx) => {
if (!config.telegram.session) {
return [];
}
if (!client.connected) {
await wait(1000);
}
const item = [];
const chat = await client.getInputEntity(ctx.req.param('username'));
const channelInfo = await client.getEntity(chat);
let attachments = [];
const messages = await client.getMessages(chat, { limit: 50 });
for (const message of messages) {
if (message.media) {
// messages that have no text are shown as if they're one post
// because in TG only 1 attachment per message is possible
attachments.push(getMediaLink(ctx, chat, ctx.req.param('username'), message));
}
if (message.text !== '') {
let description = attachments.join('\n');
attachments = []; // emitting these, buffer other ones
if (message.text) {
description += `<p>${HTMLParser.unparse(message.message, message.entities).replaceAll('\n', '<br/>')}</p>`;
}
const title = message.text ? message.text.substring(0, 80) + (message.text.length > 80 ? '...' : '') : new Date(message.date * 1000).toUTCString();
item.push({
title,
description,
pubDate: new Date(message.date * 1000).toUTCString(),
link: `https://t.me/s/${channelInfo.username}/${message.id}`,
author: `${channelInfo.title} (@${channelInfo.username})`,
});
}
}
ctx.set('data', {
title: channelInfo.title,
language: null,
link: `https://t.me/${channelInfo.username}`,
item,
allowEmpty: ctx.req.param('id') === 'allow_empty',
description: `@${channelInfo.username} on Telegram`,
});
};
module.exports.getMedia = getMedia;