Skip to content

Commit

Permalink
Merge eb976d6 into dd0a007
Browse files Browse the repository at this point in the history
  • Loading branch information
nonumpa committed Jul 3, 2022
2 parents dd0a007 + eb976d6 commit e1be3ca
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import redis from './lib/redisClient';
import session from 'koa-session2';
import passport from 'koa-passport';
import { loginRouter, authRouter } from './auth';
import lineContentRouter from './lineContent';

const app = new Koa();
const router = Router();
Expand All @@ -36,6 +37,11 @@ router.use('/callback', webhookRouter.routes(), webhookRouter.allowedMethods());

router.use('/login', loginRouter.routes(), loginRouter.allowedMethods());
router.use('/authcallback', authRouter.routes(), authRouter.allowedMethods());
router.use(
'/getcontent',
lineContentRouter.routes(),
lineContentRouter.allowedMethods()
);

if (process.env.NODE_ENV === 'production') {
app.use(
Expand Down
28 changes: 28 additions & 0 deletions src/lineContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Router from 'koa-router';
import { fetchFile } from './webhook/handlers/fileHandler';
import { verify, read } from 'src/lib/jwt';

const lineContentRouter = Router();

lineContentRouter.get('/', async ctx => {
const jwt = ctx.query.token;
if (!jwt || !verify(jwt)) {
const err = new Error('`token` is invalid or expired.');
err.status = 400;
err.expose = true;
throw err;
}

const parsed = read(jwt);
const response = await fetchFile(parsed.messageId);
ctx.response.set('content-length', response.headers.get('content-length'));
ctx.response.set('content-type', response.headers.get('content-type'));
ctx.response.set(
'content-disposition',
`attachment; filename=${parsed.messageId}`
);
ctx.status = 200;
ctx.body = response.body;
});

export default lineContentRouter;
5 changes: 4 additions & 1 deletion src/webhook/handlers/fileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { google } from 'googleapis';
import fs from 'fs';
import util from 'util';
import { exec as child_process_exec } from 'child_process';
import { getLineContentProxyURL } from './utils';
const exec = util.promisify(child_process_exec);

const OAuth2 = google.auth.OAuth2;
Expand Down Expand Up @@ -36,7 +37,7 @@ function initGDrive() {
drive = google.drive({ version: 'v3', auth: oauth2Client });
}

export async function downloadFile(messageId) {
export async function fetchFile(messageId) {
//get line message file
const LINE_API_URL = `https://api-data.line.me/v2/bot/message/${messageId}/content`;
const options = {
Expand Down Expand Up @@ -80,6 +81,8 @@ export async function saveImageFile(fetchResponse, fileName) {
}

export async function processImage(messageId) {
console.log(`Image url: ${getLineContentProxyURL(messageId)}`);

const filePath = tmpDir + messageId;
const command = `tesseract ${filePath}.jpg ${filePath} -l chi_tra`;
await exec(command);
Expand Down
16 changes: 16 additions & 0 deletions src/webhook/handlers/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { t, msgid, ngettext } from 'ttag';
import GraphemeSplitter from 'grapheme-splitter';
import { getArticleURL, createTypeWords } from 'src/lib/sharedUtils';
import { sign } from 'src/lib/jwt';

const splitter = new GraphemeSplitter();

Expand Down Expand Up @@ -696,3 +697,18 @@ export function createArticleSourceReply(sessionId) {
},
};
}

const LINE_CONTENT_EXP_SEC = 300; // LINE content proxy JWT is only valid for 5 min

/**
* @param {string} messageId - The line messageId
* @returns {string}
*/
export function getLineContentProxyURL(messageId) {
const jwt = sign({
messageId,
exp: Math.round(Date.now() / 1000) + LINE_CONTENT_EXP_SEC,
});

return `${process.env.RUMORS_LINE_BOT_URL}/getcontent?token=${jwt}`;
}
4 changes: 2 additions & 2 deletions src/webhook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import handleInput from './handleInput';
import { groupEventQueue, expiredGroupEventQueue } from 'src/lib/queues';
import GroupHandler from './handlers/groupHandler';
import {
downloadFile,
fetchFile,
uploadImageFile,
saveImageFile,
processImage,
Expand Down Expand Up @@ -208,7 +208,7 @@ const singleUserHandler = async (

let text = '';
try {
const res = await downloadFile(otherFields.message.id);
const res = await fetchFile(otherFields.message.id);
uploadImageFile(res.clone(), otherFields.message.id);
await saveImageFile(res, otherFields.message.id);
text = await processImage(otherFields.message.id);
Expand Down

0 comments on commit e1be3ca

Please sign in to comment.