Initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# Routes Folder
|
||||
|
||||
Routes define routes within your application. Fastify provides an
|
||||
easy path to a microservice architecture, in the future you might want
|
||||
to independently deploy some of those.
|
||||
|
||||
In this folder you should define all the routes that define the endpoints
|
||||
of your web application.
|
||||
Each service is a [Fastify
|
||||
plugin](https://www.fastify.io/docs/latest/Plugins/), it is
|
||||
encapsulated (it can have its own independent plugins) and it is
|
||||
typically stored in a file; be careful to group your routes logically,
|
||||
e.g. all `/users` routes in a `users.js` file. We have added
|
||||
a `root.js` file for you with a '/' root added.
|
||||
|
||||
If a single file become too large, create a folder and add a `index.js` file there:
|
||||
this file must be a Fastify plugin, and it will be loaded automatically
|
||||
by the application. You can now add as many files as you want inside that folder.
|
||||
In this way you can create complex routes within a single monolith,
|
||||
and eventually extract them.
|
||||
|
||||
If you need to share functionality between routes, place that
|
||||
functionality into the `plugins` folder, and share it via
|
||||
[decorators](https://www.fastify.io/docs/latest/Decorators/).
|
||||
|
||||
If you're a bit confused about using `async/await` to write routes, you would
|
||||
better take a look at [Promise resolution](https://www.fastify.io/docs/latest/Routes/#promise-resolution) for more details.
|
||||
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const util = require("util");
|
||||
const path = require("path");
|
||||
const { pipeline } = require("stream");
|
||||
const pump = util.promisify(pipeline);
|
||||
const crypto = require("crypto");
|
||||
const mime = require("mime-types");
|
||||
const exec = util.promisify(require("child_process").execFile);
|
||||
|
||||
const UPLOAD_PATH = "./uploads";
|
||||
|
||||
module.exports = async function (fastify, opts) {
|
||||
fastify.register(require("fastify-multipart"));
|
||||
fastify.post("/ocr", async function (req, reply) {
|
||||
const data = await req.file();
|
||||
const uid = crypto.randomBytes(16).toString("hex");
|
||||
const filename = `${UPLOAD_PATH}/${uid}.${mime.extension(data.mimetype)}`;
|
||||
const lang = "mal+eng";
|
||||
await pump(data.file, fs.createWriteStream(filename));
|
||||
const args = [filename, "stdout", "-l", lang];
|
||||
const { stdout, stderr } = await exec("tesseract", args);
|
||||
exec("rm", ["-f", filename]);
|
||||
return {
|
||||
text: stdout,
|
||||
error: stderr,
|
||||
};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user