| @@ -59,7 +59,6 @@ | |||
| 59 | "express": "^4.21.0", | 59 | "express": "^4.21.0", |
| 60 | "form-data": "^4.0.4", | 60 | "form-data": "^4.0.4", |
| 61 | "fuse.js": "^7.1.0", | 61 | "fuse.js": "^7.1.0", |
| 62 | "google-translate-api-browser": "^3.0.1", | ||
| 63 | "google-translate-api-x": "^10.7.2", | 62 | "google-translate-api-x": "^10.7.2", |
| 64 | "handlebars": "^4.7.8", | 63 | "handlebars": "^4.7.8", |
| 65 | "helmet": "^8.1.0", | 64 | "helmet": "^8.1.0", |
| @@ -5390,12 +5389,6 @@ | |||
| 5390 | "url": "https://github.com/sponsors/sindresorhus" | 5389 | "url": "https://github.com/sponsors/sindresorhus" |
| 5391 | } | 5390 | } |
| 5392 | }, | 5391 | }, |
| 5393 | "node_modules/google-translate-api-browser": { | ||
| 5394 | "version": "3.0.1", | ||
| 5395 | "resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz", | ||
| 5396 | "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==", | ||
| 5397 | "license": "MIT" | ||
| 5398 | }, | ||
| 5399 | "node_modules/google-translate-api-x": { | 5392 | "node_modules/google-translate-api-x": { |
| 5400 | "version": "10.7.2", | 5393 | "version": "10.7.2", |
| 5401 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.2.tgz", | 5394 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.2.tgz", |
| @@ -49,7 +49,6 @@ | |||
| 49 | "express": "^4.21.0", | 49 | "express": "^4.21.0", |
| 50 | "form-data": "^4.0.4", | 50 | "form-data": "^4.0.4", |
| 51 | "fuse.js": "^7.1.0", | 51 | "fuse.js": "^7.1.0", |
| 52 | "google-translate-api-browser": "^3.0.1", | ||
| 53 | "google-translate-api-x": "^10.7.2", | 52 | "google-translate-api-x": "^10.7.2", |
| 54 | "handlebars": "^4.7.8", | 53 | "handlebars": "^4.7.8", |
| 55 | "helmet": "^8.1.0", | 54 | "helmet": "^8.1.0", |
| @@ -1,10 +1,8 @@ | |||
| 1 | import { createRequire } from 'node:module'; | ||
| 2 | |||
| 3 | import fetch from 'node-fetch'; | 1 | import fetch from 'node-fetch'; |
| 4 | import express from 'express'; | 2 | import express from 'express'; |
| 5 | import { translate as bingTranslate } from 'bing-translate-api'; | 3 | import { translate as bingTranslate } from 'bing-translate-api'; |
| 6 | import iconv from 'iconv-lite'; | ||
| 7 | import urlJoin from 'url-join'; | 4 | import urlJoin from 'url-join'; |
| 5 | import { Translator } from 'google-translate-api-x'; | ||
| 8 | 6 | ||
| 9 | import { readSecret, SECRET_KEYS } from './secrets.js'; | 7 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| 10 | import { getConfigValue, uuidv4 } from '../util.js'; | 8 | import { getConfigValue, uuidv4 } from '../util.js'; |
| @@ -15,30 +13,6 @@ const LINGVA_DEFAULT = 'https://lingva.ml/api/v1'; | |||
| 15 | 13 | ||
| 16 | export const router = express.Router(); | 14 | export const router = express.Router(); |
| 17 | 15 | ||
| 18 | /** | ||
| 19 | * Get the Google Translate API client. | ||
| 20 | * @returns {import('google-translate-api-browser')} Google Translate API client | ||
| 21 | */ | ||
| 22 | function getGoogleTranslateClient() { | ||
| 23 | const require = createRequire(import.meta.url); | ||
| 24 | const googleTranslateApi = require('google-translate-api-browser'); | ||
| 25 | return googleTranslateApi; | ||
| 26 | } | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Tries to decode an ArrayBuffer to a string using iconv-lite for UTF-8. | ||
| 30 | * @param {ArrayBuffer} buffer ArrayBuffer | ||
| 31 | * @returns {string} Decoded string | ||
| 32 | */ | ||
| 33 | function decodeBuffer(buffer) { | ||
| 34 | try { | ||
| 35 | return iconv.decode(Buffer.from(buffer), 'utf-8'); | ||
| 36 | } catch (error) { | ||
| 37 | console.error('Failed to decode buffer:', error); | ||
| 38 | return Buffer.from(buffer).toString('utf-8'); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | router.post('/libre', async (request, response) => { | 16 | router.post('/libre', async (request, response) => { |
| 43 | try { | 17 | try { |
| 44 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); | 18 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); |
| @@ -101,8 +75,8 @@ router.post('/libre', async (request, response) => { | |||
| 101 | 75 | ||
| 102 | router.post('/google', async (request, response) => { | 76 | router.post('/google', async (request, response) => { |
| 103 | try { | 77 | try { |
| 104 | const text = request.body.text; | 78 | const text = String(request.body.text ?? ''); |
| 105 | const lang = request.body.lang; | 79 | const lang = String(request.body.lang ?? ''); |
| 106 | 80 | ||
| 107 | if (!text || !lang) { | 81 | if (!text || !lang) { |
| 108 | return response.sendStatus(400); | 82 | return response.sendStatus(400); |
| @@ -110,18 +84,8 @@ router.post('/google', async (request, response) => { | |||
| 110 | 84 | ||
| 111 | console.debug('Input text: ' + text); | 85 | console.debug('Input text: ' + text); |
| 112 | 86 | ||
| 113 | const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient(); | 87 | const translator = new Translator({ to: lang }); |
| 114 | const requestUrl = generateRequestUrl(text, { to: lang }); | 88 | const translatedText = await translator.translate(text).then(result => result.text); |
| 115 | const result = await fetch(requestUrl); | ||
| 116 | |||
| 117 | if (!result.ok) { | ||
| 118 | console.warn('Google Translate error: ', result.statusText); | ||
| 119 | return response.sendStatus(500); | ||
| 120 | } | ||
| 121 | |||
| 122 | const buffer = await result.arrayBuffer(); | ||
| 123 | const translateResponse = normaliseResponse(JSON.parse(decodeBuffer(buffer))); | ||
| 124 | const translatedText = translateResponse.text; | ||
| 125 | 89 | ||
| 126 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | 90 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 127 | console.debug('Translated text: ' + translatedText); | 91 | console.debug('Translated text: ' + translatedText); |