const express = require('express');
const bodyParser = require('body-parser');
const { fetchEventSource } = require('./modules/FetchEventSource')require('request');
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const ROOT_DIR = path.resolve(__dirname, '..'); // 프로젝트 Root 경로
const FILE_UPLOAD_DIR = path.resolve(ROOT_DIR, 'tmp/files'); // OCR을 하기 위한 원본 파일이 저장될 위치
const OCR_RESULT_DIR = path.resolve(ROOT_DIR, 'tmp/work'); // OCR 처리된 이미지를 저장할 위치
const OCR_API_URL = ''; // OCR API URL
const OCR_API_KEY = ''; // OCR API Key
const OCR_OPTIONS = {
// type: 'upload', // 3.0.2403, 2.18.2403 이후 requestOCR 함수에서 fid여부에 따라 type이 변경
coord: 'origin',
skew: 'image',
boxes_type: 'all',
save_mask: 'true',
textout: 'true',
recog_form: 'true',
extract_table: 'true'
};
const app = express();
const router = express.Router();
const uploader = multer({ dest: FILE_UPLOAD_DIR });
router.post('/request', (requploader.single('file'), async (request, resresponse) => {
const filepage = _.get(req, ['file']);
const filePath = _.get(file, 'path');
(request.body || {}).page || 0;
const file = request.file;
const filePath = file.path;
const page = _request.get(req.body, ['page'])body.page || 0;
const originalNamefid = _.get(file, 'originalname');
request.body.fid;
let filePathWithExt;
if (!fid) { // 3.0.2403, 2.18.2403 이후
const originalName = file.originalname;
const mimeType = _.get(file, 'mimetype');
file.mimetype;
const extension = path.extname(originalName).split('.')[1] || mimeType.split('/')[1];
const filePathWithExt = filePath + '.' + extension;
fs.renameSync(filePath, filePathWithExt);
}
try {
const ocrResult = await requestOCR(filePathWithExt, page, fid);
const imagePath = await downloadImage(ocrResult.result.masked_image);
res
response.end(JSON.stringify({ result: ocrResult.result, imagePath }));
} catch (error) {
res
response.status(error.status).send(error.message);
}
}
}
});
/**
* OCR을 하기위해 서버로 요청합니다.
* @param {string} imgPath
* @param {number} page
* @param {string} fid
* @returns
*/
function requestOCR('/request', (imgPath, pageimgPath, page, fid) {
return new Promise((resolve, reject) => {
const apiKeytypeOption = OCR_API_KEY; // 발급 받은 OCR API Key를 입력합니다.
const options = {
{};
if (!fid) { // 3.0.2403, 2.18.2403 이후
typeOption.type = 'upload';
typeOption.image = fs.createReadStream(imgPath);
} else {
typeOption.type = 'page';
typeOption.fid = fid;
}
const options = {
url: `${OCR_SDK_URL}/ocr`,
formData: {
api_key: OCR_API_KEY,
image: fs.createReadStream(imgPath),
page_index: page,
...typeOption,
...OCROption
}
}
};
request.post(options).on('response', response => {
const statusCode = _.get(response, 'statusCode')response.statusCode;
const statusMessage = _.get(response, 'statusMessage');response.statusMessage;
if (statusCode === 200) {
console.log('OCR 성공');
let body = [];
response.on('error', (err) => {
throw err;
}).on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
// 전송 완료 const data = resolve(JSON.parse(Buffer.concat(body).toString()));
// 클라이언트에게 응답
return res.status(statusCode).json(data});
}); else {
} else { console.log('OCR 실패', statusCode);
const error = new Error(statusMessage);
error.status = statusCode;
reject(error);
// 클라이언트에게 오류 응답 }
return res.status(statusCode).json({ error: statusMessage});
});
}
/**
* OCR 결과 이미지를 }다운로드 합니다.
* @param });
}
{string} fileName
* @returns
*/
function downloadImage(fileName) {
return new Promise((resolve, reject) => {
const options = {
url: `${OCR_SDKAPI_URL}/out/${fileName}`,
formData: {
api_key: OCR_API_KEY,
}
};
const imagePath = path.resolve(WORKOCR_RESULT_DIR, fileName);
request.post(options).on('response', response => {
const statusCode = _.get(response, 'statusCode')response.statusCode;
const statusMessage = _.get(response, 'statusMessage')response.statusMessage;
if (statusCode === 200) {
console.log('OCR image download 성공');
let imageData = Buffer.from([]);
response.on('data', (chunk) => {
imageData = Buffer.concat([imageData, chunk]);
}).on('end', () => {
const relativeImagePath = '/' + path.relative(ROOT_DIR, imagePath).replace(/\\/g, '/');
fs.writeFileSync(imagePath, imageData); // 이미지 저장
resolve(relativeImagePath);
});
} else {
console.log('OCR image download 실패', statusCode);
const error = new Error(statusMessage);
error.status = statusCode;
reject(error);
}
});
});
}
app.use(bodyParser.json());
app.use('/', router);
app.listen(8080);
|