hrms-report-template/libs/convert-libs.ts

114 lines
No EOL
4 KiB
TypeScript

import express from 'express'
export const convertTemplateRoute = express.Router();
import {mimeToExtension} from './report-template'
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';
/** javascript-obfuscator:disable
* @swagger
* tags:
* name: office-convert
* description: ใช้แปลงไฟล์จากเอกสารที่ libreoffice แปลงได้ เช่น docx เป็น pdf
*/
/** javascript-obfuscator:disable
* @swagger
* /api/v1/report-template/convert:
* post:
* summary: แปลงฟอร์แม็ตเอกสารเช่นจาก docx เป็น pdf ให้ตั้งค่า Media type เป็นชนิดไฟล์ที่ต้องการ
* tags: [office-convert]
* parameters:
* - name: report-name
* in: header
* description: ชื่อไฟล์ที่ต้องการหลังแปลง
* required: true
* schema:
* type: string
* example: report
* requestBody:
* required: true
* content:
* application/octet-stream:
* schema:
* type: string
* format: binary
* responses:
* 201:
* description: file was converted.
* content:
* application/pdf:
* schema:
* type: string
* format: binary
* application/vnd.oasis.opendocument.text:
* schema:
* type: string
* format: binary
* application/vnd.openxmlformats-officedocument.wordprocessingml.document:
* schema:
* type: string
* format: binary
* application/msword:
* schema:
* type: string
* format: binary
* image/png:
* schema:
* type: string
* format: binary
* image/jpeg:
* schema:
* type: string
* format: binary
* 400:
* description: Invalid format
* 500:
* description: Server error
*
*/
convertTemplateRoute.post("/", async function (req, res) {
try {
if (!req.headers['content-type'] ||
!req.headers['accept'] ||
!req.headers['report-name'] ||
req.headers['content-type'] !== "application/octet-stream") {
res.statusCode = 400;
res.statusMessage = 'Require header: content-type(application/octet-stream) accept, report-name';
res.end(res.statusMessage);
console.log(req.headers['content-type'],req.headers['accept'],req.headers['report-name'])
return
}
let outputMediaType = mimeToExtension(req.headers['accept']);
let reportName = req.headers['report-name']
console.log('convert output: ' + outputMediaType);
const libreOfficeFileConverter = new LibreOfficeFileConverter({
childProcessOptions: {
timeout: 60 * 1000,
},
});
//const buffer = await libreOfficeFileConverter.convertBuffer(req.body, outputMediaType);
const buffer = await libreOfficeFileConverter.convert({
buffer:Buffer.from(req.body),
format: outputMediaType,
input: "buffer",
output: "buffer"
})
res.statusCode = 201;
res.setHeader('Content-Type', req.headers['accept']);
res.setHeader('Content-Disposition', `attachment;filename=${reportName}.${outputMediaType}`);
res.setHeader('Content-Length', buffer.length);
res.end(buffer);
} catch (ex) {
if(ex instanceof SyntaxError){
res.statusCode = 400
res.statusMessage = ex.message
res.end(res.statusMessage)
console.error("report-template/convert: ", ex)
}else{
res.statusCode = 500
res.statusMessage = "Internal Server Error during POST report-template/convert"
res.end(res.statusMessage)
console.error("report-template/html: ", ex)
}
}
})