format with prettier
This commit is contained in:
parent
f7178d212a
commit
3c35c1e552
10 changed files with 540 additions and 466 deletions
|
|
@ -1,14 +1,14 @@
|
|||
import express from 'express'
|
||||
export const convertTemplateRoute = express.Router();
|
||||
import {mimeToExtension} from './report-template'
|
||||
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';
|
||||
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
|
||||
|
|
@ -19,7 +19,7 @@ import { LibreOfficeFileConverter } from 'libreoffice-file-converter';
|
|||
* parameters:
|
||||
* - name: report-name
|
||||
* in: header
|
||||
* description: ชื่อไฟล์ที่ต้องการหลังแปลง
|
||||
* description: ชื่อไฟล์ที่ต้องการหลังแปลง
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
|
|
@ -66,49 +66,59 @@ import { LibreOfficeFileConverter } from 'libreoffice-file-converter';
|
|||
*
|
||||
*/
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,30 +1,32 @@
|
|||
// https://swagger.io/docs/specification/about/
|
||||
import swaggerJsdoc from "swagger-jsdoc"
|
||||
import fs from 'fs'
|
||||
import fs from "fs"
|
||||
const swaggerOptions = {
|
||||
definition: {
|
||||
openapi: "3.1.0",
|
||||
info: {
|
||||
title: "Report Server",
|
||||
version: "0.8.1",
|
||||
description:
|
||||
"Technical preview releases - Report Server <br/>Advance create and convert document API for microservice era. ",
|
||||
license: {
|
||||
name: "by oom@Frappet",
|
||||
url: "https://frappet.com",
|
||||
},
|
||||
},
|
||||
servers: [
|
||||
{ url: "https://report-server.frappet.synology.me" },
|
||||
{ url: "https://bma-ehr.frappet.synology.me/" },
|
||||
{ url: "http://localhost:3001" },
|
||||
],
|
||||
definition: {
|
||||
openapi: "3.1.0",
|
||||
info: {
|
||||
title: "Report Server",
|
||||
version: "0.8.1",
|
||||
description:
|
||||
"Technical preview releases - Report Server <br/>Advance create and convert document API for microservice era. ",
|
||||
license: {
|
||||
name: "by oom@Frappet",
|
||||
url: "https://frappet.com",
|
||||
},
|
||||
},
|
||||
apis: ["./libs/*.ts"],
|
||||
};
|
||||
servers: [
|
||||
{ url: "https://report-server.frappet.synology.me" },
|
||||
{ url: "https://bma-ehr.frappet.synology.me/" },
|
||||
{ url: "http://localhost:3001" },
|
||||
],
|
||||
},
|
||||
apis: ["./libs/*.ts"],
|
||||
}
|
||||
export function createSpec() {
|
||||
const swaggerSpecs = swaggerJsdoc(swaggerOptions);
|
||||
fs.promises.writeFile("libs/swagger-specs.json", JSON.stringify(swaggerSpecs, null, 2))
|
||||
const swaggerSpecs = swaggerJsdoc(swaggerOptions)
|
||||
fs.promises.writeFile(
|
||||
"libs/swagger-specs.json",
|
||||
JSON.stringify(swaggerSpecs, null, 2)
|
||||
)
|
||||
}
|
||||
createSpec()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
import { PDFOptions } from 'puppeteer'
|
||||
/**
|
||||
import { PDFOptions } from "puppeteer"
|
||||
/**
|
||||
* @prop {string} template template ID
|
||||
* @prop {string} reportName outputname
|
||||
* @prop {htmlTemplateOption} htmlOption? support only html-template
|
||||
* @prop {object} data json data for apply template
|
||||
*/
|
||||
export interface templateOption {
|
||||
template: string
|
||||
reportName: string
|
||||
htmlOption?:htmlTemplateOption
|
||||
data: object
|
||||
template: string
|
||||
reportName: string
|
||||
htmlOption?: htmlTemplateOption
|
||||
data: object
|
||||
}
|
||||
/**
|
||||
/**
|
||||
* @prop {number} navigationTimeout page.setDefaultNavigationTimeout(navigationTimeout)
|
||||
* @prop {number} querySelector Element of page
|
||||
* @prop {number} waitUntil 'networkidle0' or 'networkidle0'
|
||||
|
|
@ -21,48 +21,48 @@ export interface templateOption {
|
|||
* @prop {PDFOptions} pdfOption PdfOptions of Puppeteer
|
||||
*/
|
||||
export interface htmlTemplateOption {
|
||||
navigationTimeout?:number
|
||||
querySelector?: string
|
||||
waitUntil?:string
|
||||
preloadWait?:number
|
||||
preloadScroll?:number
|
||||
preloadLoop?:number
|
||||
pdfOption?: PDFOptions
|
||||
navigationTimeout?: number
|
||||
querySelector?: string
|
||||
waitUntil?: string
|
||||
preloadWait?: number
|
||||
preloadScroll?: number
|
||||
preloadLoop?: number
|
||||
pdfOption?: PDFOptions
|
||||
}
|
||||
|
||||
|
||||
|
||||
export interface IDictionary<TValue> {
|
||||
[key: string]: TValue
|
||||
[key: string]: TValue
|
||||
}
|
||||
export function mimeToExtension(mime: string): string {
|
||||
const mimeList: IDictionary<string> = {
|
||||
"application/pdf": "pdf",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
||||
"application/msword": "doc",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
||||
"application/vnd.ms-excel": "xls",
|
||||
"application/vnd.ms-powerpoint": "ppt",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
||||
"application/vnd.oasis.opendocument.text": "odt",
|
||||
"application/vnd.oasis.opendocument.spreadsheet": "ods",
|
||||
"application/vnd.oasis.opendocument.presentation": "odp",
|
||||
"text/html": "html",
|
||||
"application/json": "json",
|
||||
"text/csv": "csv",
|
||||
"text/markdown": "md",
|
||||
"text/plain": "txt",
|
||||
"application/rtf": "rtf",
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpeg",
|
||||
}
|
||||
if (mimeList[mime]) {
|
||||
return mimeList[mime]
|
||||
} else if (mime.match(/^application\/x\./)) {
|
||||
return mime.substr(mime.indexOf(".") + 1)
|
||||
} else {
|
||||
return mime
|
||||
}
|
||||
const mimeList: IDictionary<string> = {
|
||||
"application/pdf": "pdf",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
"docx",
|
||||
"application/msword": "doc",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
||||
"application/vnd.ms-excel": "xls",
|
||||
"application/vnd.ms-powerpoint": "ppt",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
||||
"pptx",
|
||||
"application/vnd.oasis.opendocument.text": "odt",
|
||||
"application/vnd.oasis.opendocument.spreadsheet": "ods",
|
||||
"application/vnd.oasis.opendocument.presentation": "odp",
|
||||
"text/html": "html",
|
||||
"application/json": "json",
|
||||
"text/csv": "csv",
|
||||
"text/markdown": "md",
|
||||
"text/plain": "txt",
|
||||
"application/rtf": "rtf",
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpeg",
|
||||
}
|
||||
if (mimeList[mime]) {
|
||||
return mimeList[mime]
|
||||
} else if (mime.match(/^application\/x\./)) {
|
||||
return mime.substr(mime.indexOf(".") + 1)
|
||||
} else {
|
||||
return mime
|
||||
}
|
||||
}
|
||||
/** javascript-obfuscator:disable
|
||||
* @swagger
|
||||
|
|
|
|||
|
|
@ -17,34 +17,40 @@ const TEMPLATE_FOLDER_NAME = "templates/xlsx"
|
|||
* @param {Number} tab tab page of spread sheet , default = 1
|
||||
* @return {Promise<Uint8Array>} output buffer after apply template.
|
||||
*/
|
||||
export async function xlsxTemplateX(t: Buffer|String, tdata: templateOption, outputMediaType: string = "xlsx", tab: number = 1): Promise<Uint8Array> {
|
||||
try {
|
||||
const templateBuff = Buffer.isBuffer(t)?t: await fs.promises.readFile(String(t))
|
||||
const template = new ExcelTemplate()
|
||||
await template.load(templateBuff)
|
||||
await template.process(tab, tdata.data)
|
||||
const buffer = await template.build({ type: "uint8array" }) as Uint8Array
|
||||
if (outputMediaType === "xlsx") return buffer
|
||||
export async function xlsxTemplateX(
|
||||
t: Buffer | String,
|
||||
tdata: templateOption,
|
||||
outputMediaType: string = "xlsx",
|
||||
tab: number = 1
|
||||
): Promise<Uint8Array> {
|
||||
try {
|
||||
const templateBuff = Buffer.isBuffer(t)
|
||||
? t
|
||||
: await fs.promises.readFile(String(t))
|
||||
const template = new ExcelTemplate()
|
||||
await template.load(templateBuff)
|
||||
await template.process(tab, tdata.data)
|
||||
const buffer = (await template.build({ type: "uint8array" })) as Uint8Array
|
||||
if (outputMediaType === "xlsx") return buffer
|
||||
|
||||
const libreOfficeFileConverter = new LibreOfficeFileConverter({
|
||||
childProcessOptions: {
|
||||
timeout: 60 * 1000,
|
||||
},
|
||||
})
|
||||
//const lbuffer = await libreOfficeFileConverter.convertBuffer(Buffer.from(buffer as Uint8Array), outputMediaType)
|
||||
const libreOfficeFileConverter = new LibreOfficeFileConverter({
|
||||
childProcessOptions: {
|
||||
timeout: 60 * 1000,
|
||||
},
|
||||
})
|
||||
//const lbuffer = await libreOfficeFileConverter.convertBuffer(Buffer.from(buffer as Uint8Array), outputMediaType)
|
||||
|
||||
const lbuffer = await libreOfficeFileConverter.convert({
|
||||
buffer:Buffer.from(buffer),
|
||||
format: outputMediaType,
|
||||
input: "buffer",
|
||||
output: "buffer"
|
||||
})
|
||||
const lbuffer = await libreOfficeFileConverter.convert({
|
||||
buffer: Buffer.from(buffer),
|
||||
format: outputMediaType,
|
||||
input: "buffer",
|
||||
output: "buffer",
|
||||
})
|
||||
|
||||
|
||||
return lbuffer
|
||||
} catch (e) {
|
||||
throw e
|
||||
}
|
||||
return lbuffer
|
||||
} catch (e) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/** javascript-obfuscator:disable
|
||||
|
|
@ -67,16 +73,16 @@ export async function xlsxTemplateX(t: Buffer|String, tdata: templateOption, out
|
|||
* description: Server error
|
||||
*/
|
||||
xlsxTemplateRoute.get("/", async function (req, res) {
|
||||
try {
|
||||
const fileList = await fs.promises.readdir(`./${TEMPLATE_FOLDER_NAME}`)
|
||||
const templateList = fileList.map(f => f.split(".xlsx")[0])
|
||||
res.send(templateList)
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during get xlsx template list"
|
||||
res.end(res.statusMessage)
|
||||
console.error("Error during get template list: ", ex)
|
||||
}
|
||||
try {
|
||||
const fileList = await fs.promises.readdir(`./${TEMPLATE_FOLDER_NAME}`)
|
||||
const templateList = fileList.map((f) => f.split(".xlsx")[0])
|
||||
res.send(templateList)
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during get xlsx template list"
|
||||
res.end(res.statusMessage)
|
||||
console.error("Error during get template list: ", ex)
|
||||
}
|
||||
})
|
||||
|
||||
/** javascript-obfuscator:disable
|
||||
|
|
@ -136,36 +142,45 @@ xlsxTemplateRoute.get("/", async function (req, res) {
|
|||
*
|
||||
*/
|
||||
xlsxTemplateRoute.post("/", async function (req, res) {
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"]) throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"])
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let template = null
|
||||
// Save the converted file to disk
|
||||
if (req.query["folder"]) {
|
||||
template = await fs.promises.readFile(`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${req.body.template}.xlsx`)
|
||||
} else {
|
||||
template = await fs.promises.readFile(`./${TEMPLATE_FOLDER_NAME}/${req.body.template}.xlsx`)
|
||||
}
|
||||
let buffer = await xlsxTemplateX(template, req.body, outputMediaType)
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader("Content-Disposition", `attachment;filename=${req.body.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/xlsx: ", ex)
|
||||
}else{
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during POST report-template/xlsx"
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/xlsx: ", ex)
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"])
|
||||
throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"])
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let template = null
|
||||
// Save the converted file to disk
|
||||
if (req.query["folder"]) {
|
||||
template = await fs.promises.readFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${req.body.template}.xlsx`
|
||||
)
|
||||
} else {
|
||||
template = await fs.promises.readFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${req.body.template}.xlsx`
|
||||
)
|
||||
}
|
||||
let buffer = await xlsxTemplateX(template, req.body, outputMediaType)
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader(
|
||||
"Content-Disposition",
|
||||
`attachment;filename=${req.body.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/xlsx: ", ex)
|
||||
} else {
|
||||
res.statusCode = 500
|
||||
res.statusMessage =
|
||||
"Internal Server Error during POST report-template/xlsx"
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/xlsx: ", ex)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
/** javascript-obfuscator:disable
|
||||
|
|
@ -211,39 +226,57 @@ xlsxTemplateRoute.post("/", async function (req, res) {
|
|||
*
|
||||
*/
|
||||
xlsxTemplateRoute.post("/upload", async function (req, res) {
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"] || !req.query["report_name"] || req.headers["content-type"] !== "application/octet-stream") {
|
||||
res.statusCode = 400
|
||||
res.statusMessage = "Require header: content-type(application/octet-stream) accept"
|
||||
res.end(res.statusMessage)
|
||||
console.log(req.headers["content-type"], req.headers["accept"], req.query["report_name"])
|
||||
return
|
||||
}
|
||||
// Determine the output media type and report name from headers
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let reportName = req.query["report_name"]
|
||||
console.log("convert output: " + outputMediaType)
|
||||
try {
|
||||
if (
|
||||
!req.headers["content-type"] ||
|
||||
!req.headers["accept"] ||
|
||||
!req.query["report_name"] ||
|
||||
req.headers["content-type"] !== "application/octet-stream"
|
||||
) {
|
||||
res.statusCode = 400
|
||||
res.statusMessage =
|
||||
"Require header: content-type(application/octet-stream) accept"
|
||||
res.end(res.statusMessage)
|
||||
console.log(
|
||||
req.headers["content-type"],
|
||||
req.headers["accept"],
|
||||
req.query["report_name"]
|
||||
)
|
||||
return
|
||||
}
|
||||
// Determine the output media type and report name from headers
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let reportName = req.query["report_name"]
|
||||
console.log("convert output: " + outputMediaType)
|
||||
|
||||
// Save the converted file to disk
|
||||
if (req.query["folder"]) {
|
||||
// Ensure the template folder exists
|
||||
await fs.promises.mkdir(`TEMPLATE_FOLDER_NAME/${req.query["folder"]}`, { recursive: true })
|
||||
await fs.promises.writeFile(`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${reportName}.xlsx`, req.body)
|
||||
} else {
|
||||
// Ensure the template folder exists
|
||||
await fs.promises.mkdir(TEMPLATE_FOLDER_NAME, { recursive: true })
|
||||
await fs.promises.writeFile(`./${TEMPLATE_FOLDER_NAME}/${reportName}.xlsx`, req.body)
|
||||
}
|
||||
// Save the converted file to disk
|
||||
if (req.query["folder"]) {
|
||||
// Ensure the template folder exists
|
||||
await fs.promises.mkdir(`TEMPLATE_FOLDER_NAME/${req.query["folder"]}`, {
|
||||
recursive: true,
|
||||
})
|
||||
await fs.promises.writeFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${reportName}.xlsx`,
|
||||
req.body
|
||||
)
|
||||
} else {
|
||||
// Ensure the template folder exists
|
||||
await fs.promises.mkdir(TEMPLATE_FOLDER_NAME, { recursive: true })
|
||||
await fs.promises.writeFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${reportName}.xlsx`,
|
||||
req.body
|
||||
)
|
||||
}
|
||||
|
||||
// Send a response to the client
|
||||
res.statusCode = 201
|
||||
res.json({ message: "File converted and saved successfully" })
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error"
|
||||
res.end(res.statusMessage)
|
||||
console.error(`Error during convert with soffice:`, ex)
|
||||
}
|
||||
// Send a response to the client
|
||||
res.statusCode = 201
|
||||
res.json({ message: "File converted and saved successfully" })
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error"
|
||||
res.end(res.statusMessage)
|
||||
console.error(`Error during convert with soffice:`, ex)
|
||||
}
|
||||
})
|
||||
|
||||
/** javascript-obfuscator:disable
|
||||
|
|
@ -281,27 +314,39 @@ xlsxTemplateRoute.post("/upload", async function (req, res) {
|
|||
*
|
||||
*/
|
||||
xlsxTemplateRoute.post("/download", async function (req, res) {
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"] || !req.body.template) throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"])
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
console.log("content-type: ", inputType)
|
||||
console.log("accept: ", outputMediaType)
|
||||
let buffer = null
|
||||
if (req.query["folder"]) {
|
||||
buffer = await fs.promises.readFile(`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${req.body.template}.xlsx`)
|
||||
} else {
|
||||
buffer = await fs.promises.readFile(`./${TEMPLATE_FOLDER_NAME}/${req.body.template}.xlsx`)
|
||||
}
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader("Content-Disposition", `attachment;filename=${req.body.template}.${outputMediaType}`)
|
||||
res.setHeader("Content-Length", buffer.length)
|
||||
res.end(buffer)
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during get xlsx template list"
|
||||
res.end(res.statusMessage)
|
||||
console.error("Error during apply template: ", ex)
|
||||
}
|
||||
try {
|
||||
if (
|
||||
!req.headers["content-type"] ||
|
||||
!req.headers["accept"] ||
|
||||
!req.body.template
|
||||
)
|
||||
throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"])
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
console.log("content-type: ", inputType)
|
||||
console.log("accept: ", outputMediaType)
|
||||
let buffer = null
|
||||
if (req.query["folder"]) {
|
||||
buffer = await fs.promises.readFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${req.query["folder"]}/${req.body.template}.xlsx`
|
||||
)
|
||||
} else {
|
||||
buffer = await fs.promises.readFile(
|
||||
`./${TEMPLATE_FOLDER_NAME}/${req.body.template}.xlsx`
|
||||
)
|
||||
}
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader(
|
||||
"Content-Disposition",
|
||||
`attachment;filename=${req.body.template}.${outputMediaType}`
|
||||
)
|
||||
res.setHeader("Content-Length", buffer.length)
|
||||
res.end(buffer)
|
||||
} catch (ex) {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during get xlsx template list"
|
||||
res.end(res.statusMessage)
|
||||
console.error("Error during apply template: ", ex)
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue