331 lines
9.9 KiB
TypeScript
331 lines
9.9 KiB
TypeScript
import fs from "fs";
|
|
import axios from "axios";
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Routes
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| This file is dedicated for defining HTTP routes. A single file is enough
|
|
| for majority of projects, however you can define routes in different
|
|
| files and just make sure to import them inside this file. For example
|
|
|
|
|
| Define routes in following two files
|
|
| ├── start/routes/cart.ts
|
|
| ├── start/routes/customer.ts
|
|
|
|
|
| and then import them inside `start/routes.ts` as follows
|
|
|
|
|
| import './routes/cart'
|
|
| import './routes/customer'
|
|
|
|
|
*/
|
|
|
|
import Route from "@ioc:Adonis/Core/Route";
|
|
import { runtimeCheckLogs } from "App/utils/runtimeCheckLogs";
|
|
import Env from "@ioc:Adonis/Core/Env";
|
|
import { sendMessToZulip } from "App/utils/sendMessToZulip";
|
|
import moment from "moment";
|
|
import Product from "App/Models/Product";
|
|
import { sendDeviceInfora } from "App/utils/sendDeviceInfor";
|
|
import InfoDevice from "App/Models/InfoDevice";
|
|
import LogReport from "App/Models/LogReport";
|
|
import Cache from "@ioc:Kaperskyguru/Adonis-Cache";
|
|
const util = require("util");
|
|
const exec = util.promisify(require("child_process").exec);
|
|
const { DocumentProcessorServiceClient } =
|
|
require("@google-cloud/documentai").v1;
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
runtimeCheckLogs(Env.get("FOLDER_LOGS"));
|
|
|
|
Route.post("/api/getIndexSerialNumber", "ErpsController.getIndexSerialNumber")
|
|
.middleware("checkToken")
|
|
.middleware("writeLog");
|
|
// .middleware("writeLog");
|
|
|
|
Route.post("/api/getParagraph", "ErpsController.getParagraph")
|
|
.middleware("checkToken")
|
|
.middleware("writeLog");
|
|
|
|
//Users
|
|
Route.post("/api/account/createUser", "UsersController.create").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/account/checkLogin", "UsersController.checkLogin").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
//Log
|
|
Route.get("/api/log/showLog/:name?", "LogsController.showLog").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.get("/api/getAllLogDetect", "LogsController.getAllLogDetect");
|
|
|
|
//Key-Value
|
|
Route.post("/api/getKeyValue", "ValuesController.getKeyValue");
|
|
|
|
Route.post("/api/deleteValue", "ValuesController.destroy").middleware(
|
|
"writeLog"
|
|
);
|
|
|
|
Route.post("/api/editValue", "ValuesController.edit").middleware("writeLog");
|
|
|
|
Route.post("/api/addValue", "ValuesController.create").middleware("writeLog");
|
|
|
|
Route.post("/api/backupProduct", async ({ request, response }) => {
|
|
try {
|
|
const res = await axios.post(
|
|
"https://logs.danielvu.com/api/getIndexSerialNumber",
|
|
{ from: request.all().from, to: request.all().to },
|
|
{
|
|
headers: {
|
|
Authorization: request.headers().authorization?.replace(/"/g, ""),
|
|
},
|
|
}
|
|
);
|
|
|
|
res.data.map((obj, index) => {
|
|
res.data[index] = {
|
|
PID: res.data[index].PID,
|
|
SN: res.data[index].SN,
|
|
VID: res.data[index].VID,
|
|
line: res.data[index].line.join(","),
|
|
file: res.data[index].fileName,
|
|
warehouse: res.data[index].warehouse,
|
|
};
|
|
});
|
|
const addProduct = await Product.createMany(res.data);
|
|
// console.log(addProduct)
|
|
response.status(200).send("Add " + res.data.length + " success!");
|
|
await sendMessToZulip(
|
|
"stream",
|
|
"networkToolBot",
|
|
"Log service",
|
|
"Backup product " +
|
|
request.all().from +
|
|
" to " +
|
|
request.all().to +
|
|
" success with " +
|
|
res.data.length +
|
|
" products"
|
|
);
|
|
} catch (error) {
|
|
response.status(500).send(error);
|
|
await sendMessToZulip(
|
|
"stream",
|
|
"networkToolBot",
|
|
"Log service",
|
|
"Backup product fail. Please check!"
|
|
);
|
|
}
|
|
}).middleware("writeLog");
|
|
|
|
Route.post("/api/sendMailInforDevice", async () => {
|
|
try {
|
|
sendDeviceInfora();
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
})
|
|
.middleware("checkToken")
|
|
.middleware("writeLog");
|
|
|
|
Route.post(
|
|
"/api/private-log/getFileOnFolder",
|
|
async ({ request, response }) => {
|
|
try {
|
|
let result = [];
|
|
let path = request.all().folerPath;
|
|
return new Promise((resolve, reject) => {
|
|
fs.readdir(path, (err, entries) => {
|
|
if (err) {
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
entries.forEach((entry) => {
|
|
const entryPath = path + "/" + entry;
|
|
|
|
fs.stat(entryPath, (statErr, stats) => {
|
|
if (statErr) {
|
|
reject(statErr);
|
|
return;
|
|
}
|
|
|
|
const type = stats.isFile() ? "file" : "directory";
|
|
result.push({ name: entryPath, type: type });
|
|
|
|
if (result.length === entries.length) {
|
|
resolve(result);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
);
|
|
|
|
Route.post("/api/private-log/readFile", async ({ request, response }) => {
|
|
try {
|
|
let result = [];
|
|
let path = request.all().filePath;
|
|
return await fs.readFileSync(path, "utf8");
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
});
|
|
|
|
Route.post("/api/extension/addressDetect", async ({ request, response }) => {
|
|
// TODO(developer): Uncomment these variables before running the sample.
|
|
const projectId = "532287737140";
|
|
const location = "us"; // Format is 'us' or 'eu'
|
|
const processorId = "64ad0cc100561909"; // Create processor before running sample
|
|
// const filePath = "https://int.ipsupply.com.au/upload/temp/packagepurchaseorder_17162360547876.jpeg";
|
|
const mimeType = "image/jpeg"; // Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types
|
|
const fieldMask = ["entities"]; // Optional. The fields to return in the Document object.
|
|
const processorVersionId = "8aa31873669ac16f"; // Optional. Processor version to use
|
|
const bucketName = "get-address"; // Replace with your GCS bucket name
|
|
|
|
// Set up authentication
|
|
process.env.GOOGLE_APPLICATION_CREDENTIALS =
|
|
"strategic-block-424302-v3-54b10fc7e085.json";
|
|
|
|
async function downloadFile(url, outputLocationPath) {
|
|
const writer = fs.createWriteStream(outputLocationPath);
|
|
const response = await axios({
|
|
url,
|
|
method: "GET",
|
|
responseType: "stream",
|
|
});
|
|
response.data.pipe(writer);
|
|
return new Promise((resolve, reject) => {
|
|
writer.on("finish", resolve);
|
|
writer.on("error", reject);
|
|
});
|
|
}
|
|
|
|
async function processDocument(filePath) {
|
|
const client = new DocumentProcessorServiceClient();
|
|
|
|
let name;
|
|
if (processorVersionId) {
|
|
// The full resource name of the processor version, e.g.:
|
|
// `projects/${projectId}/locations/${location}/processors/${processorId}/processorVersions/${processorVersionId}`
|
|
name = client.processorVersionPath(
|
|
projectId,
|
|
location,
|
|
processorId,
|
|
processorVersionId
|
|
);
|
|
} else {
|
|
// The full resource name of the processor, e.g.:
|
|
// `projects/${projectId}/locations/${location}/processors/${processorId}`
|
|
name = client.processorPath(projectId, location, processorId);
|
|
}
|
|
|
|
// Download the file from URL
|
|
let imageContent;
|
|
const tempFilePath = path.join(__dirname, "images/temp.jpeg");
|
|
if (filePath.includes("http")) {
|
|
console.log("Download file");
|
|
await downloadFile(filePath, tempFilePath);
|
|
imageContent = fs.readFileSync(tempFilePath);
|
|
} else {
|
|
imageContent = fs.readFileSync(filePath);
|
|
}
|
|
// Read the file into memory
|
|
// imageContent = fs.readFileSync(filePath);
|
|
|
|
// Load binary data
|
|
const rawDocument = {
|
|
content: imageContent,
|
|
mimeType: mimeType,
|
|
};
|
|
|
|
// Optional: Additional configurations for processing.
|
|
const processOptions = {
|
|
individualPageSelector: {
|
|
pages: [1],
|
|
},
|
|
};
|
|
|
|
// Configure the process request
|
|
const request = {
|
|
name: name,
|
|
rawDocument: rawDocument,
|
|
fieldMask: fieldMask, // Join fieldMask array into a comma-separated string
|
|
processOptions: processOptions,
|
|
};
|
|
|
|
const [result] = await client.processDocument(request);
|
|
|
|
// For a full list of `Document` object attributes, reference this page:
|
|
// https://cloud.google.com/document-ai/docs/reference/rest/v1/Document
|
|
const document = result.document;
|
|
|
|
// Read the text recognition output from the processor
|
|
console.log("The document contains the following text:");
|
|
return extractAndPrintData(document.entities);
|
|
}
|
|
|
|
// Function to extract and print data
|
|
function extractAndPrintData(response) {
|
|
let result = "";
|
|
response.forEach((item) => {
|
|
const type = item.type;
|
|
const mentionText = item.mentionText;
|
|
result += `${type} : ${mentionText}\n\n`;
|
|
});
|
|
return result;
|
|
}
|
|
const { filePath } = request.all();;
|
|
console.log(filePath)
|
|
try {
|
|
await processDocument(filePath).then((result)=>{
|
|
console.log(result)
|
|
response.status(200).send(JSON.stringify(result));
|
|
}).catch((e)=>console.log(e));
|
|
} catch (error) {
|
|
response.status(500).send(`Error: ${error.message}`);
|
|
}
|
|
});
|
|
|
|
Route.post("/api/find-value", async ({ request, response }) => {
|
|
try {
|
|
const { value } = request.all();
|
|
|
|
console.log(value);
|
|
const { stdout, stderr } = await exec(`grep -nrE "${value}" /home/logs`, {
|
|
maxBuffer: 1024 * 500,
|
|
});
|
|
|
|
// response.status(200).send("sdjkghs");
|
|
response.status(200).send(JSON.stringify(stdout));
|
|
} catch (error) {
|
|
console.error(error);
|
|
response.status(500).send(`Error: ${error.message}`);
|
|
}
|
|
});
|
|
|
|
Route.post("/api/test", async () => {
|
|
try {
|
|
const logs = await Cache.get("logs");
|
|
|
|
if (logs) {
|
|
return { type: "cache", data: logs };
|
|
} else {
|
|
let data = await LogReport.all();
|
|
Cache.set("logs", data, 120);
|
|
return { type: "no cache", data: data };
|
|
}
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
});
|