From abcb593971fb19ce1334731c61325ef15459d674 Mon Sep 17 00:00:00 2001 From: Michael Hadley Date: Sat, 5 Aug 2017 10:44:40 -0500 Subject: [PATCH] Fix: allow local node paths for language --- src/node/lang.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/node/lang.js b/src/node/lang.js index 3e3d858..e452a8b 100644 --- a/src/node/lang.js +++ b/src/node/lang.js @@ -1,15 +1,22 @@ const http = require("http"), zlib = require("zlib"), fs = require("fs"), - path = require("path"); + path = require("path"), + isURL = require("is-url"); var langdata = require('../common/langdata.json') function getLanguageData(req, res, cb){ var lang = req.options.lang, langfile = lang + '.traineddata.gz'; - - fs.readFile(lang + '.traineddata', function (err, data) { + + // langPath defaults to a URL where languages can be downloaded. If a custom path is specified + // and it is a local path, use that instead + var localPath = isURL(req.workerOptions.langPath) ? + lang + '.traineddata' : + path.join(req.workerOptions.langPath, lang + '.traineddata'); + + fs.readFile(localPath, function (err, data) { if(!err) return cb(new Uint8Array(data)); http.get(req.workerOptions.langPath + langfile, stream => { @@ -34,4 +41,4 @@ function getLanguageData(req, res, cb){ } -module.exports = getLanguageData; \ No newline at end of file +module.exports = getLanguageData;