...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const express = require('express'); const bodyParser = require('body-parser'); const request = 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_SDKAPI_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', uploader.single('file'), async (request, response) => { const page = (request.body || {}).page || 0; const file = request.file; const filePath = file.path; const page = request.body.page || 0; const fid = request.body.fid; let filePathWithExt; if (!fid) { // 3.0.2403, 2.18.2403 이후 const originalName = file.originalname; const 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); response.end(JSON.stringify({ result: ocrResult.result, imagePath })); } catch (error) { response.status(error.status).send(error.message); } }); /** * OCR을 하기위해 서버로 요청합니다. * @param {string} imgPath * @param {number} page * @param {string} fid * @returns */ function requestOCR(imgPath, page, fid) { return new Promise((resolve, reject) => { const typeOption = {}; 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, imagepage_index: fs.createReadStream(imgPath)page, page_index: page...typeOption, ...OCR_OPTIONSOCROption } }; request.post(options).on('response', response => { const statusCode = response.statusCode; const 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', () => { resolve(JSON.parse(Buffer.concat(body).toString())); // 여기서 헤더, 메서드, url, 바디를 가지게 되었고 // 이 요청에 응답하는 데 필요한 어떤 일이라도 할 수 있게 되었습니다. }); } else { console.log('OCR 실패', statusCode); const error = new Error(statusMessage); error.status = statusCode; reject(error); } }); }); } /** * 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(OCR_RESULT_DIR, fileName); request.post(options).on('response', response => { const statusCode = response.statusCode; const 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); |
...