add postgres db, use settings and user data, lots of cleanup and logic fixes, bug fixes, better error handling, update docs and docker
Some checks are pending
njsscan sarif / njsscan code scanning (push) Waiting to run
Update AUTHORS File / update-authors (push) Waiting to run
Some checks are pending
njsscan sarif / njsscan code scanning (push) Waiting to run
Update AUTHORS File / update-authors (push) Waiting to run
This commit is contained in:
parent
765b1144fa
commit
4d540078f5
30 changed files with 1664 additions and 727 deletions
|
@ -24,22 +24,17 @@ async function downloadModule(moduleId: string): Promise<ModuleResult | null> {
|
|||
method: 'GET',
|
||||
responseType: 'stream',
|
||||
});
|
||||
|
||||
const disposition = response.headers['content-disposition'];
|
||||
let fileName = moduleId;
|
||||
|
||||
if (disposition && disposition.includes('filename=')) {
|
||||
fileName = disposition
|
||||
.split('filename=')[1]
|
||||
.split(';')[0]
|
||||
.replace(/['"]/g, '');
|
||||
}
|
||||
|
||||
const filePath = path.resolve(__dirname, fileName);
|
||||
|
||||
const filePath = path.join(__dirname, fileName);
|
||||
const writer = fs.createWriteStream(filePath);
|
||||
response.data.pipe(writer);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
writer.on('finish', () => resolve({ filePath, fileName }));
|
||||
writer.on('error', reject);
|
||||
|
@ -49,39 +44,41 @@ async function downloadModule(moduleId: string): Promise<ModuleResult | null> {
|
|||
}
|
||||
}
|
||||
|
||||
export default (bot: Telegraf<Context>) => {
|
||||
bot.command(['modarchive', 'tma'], spamwatchMiddleware, async (ctx) => {
|
||||
const Strings = getStrings(languageCode(ctx));
|
||||
const reply_to_message_id = replyToMessageId(ctx);
|
||||
const moduleId = ctx.message?.text.split(' ')[1];
|
||||
|
||||
if (Number.isNaN(moduleId) || null) {
|
||||
return ctx.reply(Strings.maInvalidModule, {
|
||||
parse_mode: "Markdown",
|
||||
...({ reply_to_message_id })
|
||||
});
|
||||
}
|
||||
const numberRegex = /^\d+$/;
|
||||
const isNumber = numberRegex.test(moduleId);
|
||||
if (isNumber) {
|
||||
const result = await downloadModule(moduleId);
|
||||
if (result) {
|
||||
const { filePath, fileName } = result;
|
||||
const regexExtension = /\.\w+$/i;
|
||||
const hasExtension = regexExtension.test(fileName);
|
||||
if (hasExtension) {
|
||||
await ctx.replyWithDocument({ source: filePath }, {
|
||||
caption: fileName,
|
||||
...({ reply_to_message_id })
|
||||
});
|
||||
fs.unlinkSync(filePath);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
export const modarchiveHandler = async (ctx: Context) => {
|
||||
const Strings = getStrings(languageCode(ctx));
|
||||
const reply_to_message_id = replyToMessageId(ctx);
|
||||
const moduleId = ctx.message && 'text' in ctx.message && typeof ctx.message.text === 'string'
|
||||
? ctx.message.text.split(' ')[1]?.trim()
|
||||
: undefined;
|
||||
if (!moduleId || !/^\d+$/.test(moduleId)) {
|
||||
return ctx.reply(Strings.maInvalidModule, {
|
||||
parse_mode: "Markdown",
|
||||
...({ reply_to_message_id })
|
||||
...(reply_to_message_id ? { reply_parameters: { message_id: reply_to_message_id } } : {})
|
||||
});
|
||||
}
|
||||
const result = await downloadModule(moduleId);
|
||||
if (result) {
|
||||
const { filePath, fileName } = result;
|
||||
const regexExtension = /\.\w+$/i;
|
||||
const hasExtension = regexExtension.test(fileName);
|
||||
if (hasExtension) {
|
||||
try {
|
||||
await ctx.replyWithDocument({ source: filePath }, {
|
||||
caption: fileName,
|
||||
...(reply_to_message_id ? { reply_parameters: { message_id: reply_to_message_id } } : {})
|
||||
});
|
||||
} finally {
|
||||
try { fs.unlinkSync(filePath); } catch (e) { /* ignore */ }
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
return ctx.reply(Strings.maInvalidModule, {
|
||||
parse_mode: "Markdown",
|
||||
...(reply_to_message_id ? { reply_parameters: { message_id: reply_to_message_id } } : {})
|
||||
});
|
||||
};
|
||||
|
||||
export default (bot: Telegraf<Context>) => {
|
||||
bot.command(['modarchive', 'tma'], spamwatchMiddleware, modarchiveHandler);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue