Skip to content

Commit

Permalink
Merge pull request #50 from TickLabVN/feature/save-preview-file-in-minio
Browse files Browse the repository at this point in the history
feat(configuration printing): save file after approving configuration…
  • Loading branch information
quannhg authored Dec 6, 2023
2 parents 3ace76a + f3a0a35 commit 4edae88
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[previewMinioName]` on the table `File` will be added. If there are existing duplicate values, this will fail.
- Added the required column `previewMinioName` to the `File` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "File" ADD COLUMN "previewMinioName" TEXT NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "File_previewMinioName_key" ON "File"("previewMinioName");
2 changes: 2 additions & 0 deletions prisma/migrations/20231206165815_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- DropIndex
DROP INDEX "File_minioName_key";
11 changes: 11 additions & 0 deletions prisma/migrations/20231206170049_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[minioName]` on the table `File` will be added. If there are existing duplicate values, this will fail.
*/
-- DropIndex
DROP INDEX "File_previewMinioName_key";

-- CreateIndex
CREATE UNIQUE INDEX "File_minioName_key" ON "File"("minioName");
2 changes: 2 additions & 0 deletions prisma/migrations/20231206170138_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "File" ALTER COLUMN "previewMinioName" DROP NOT NULL;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ model File {
id String @id @default(cuid())
realName String
minioName String @unique
previewMinioName String?
printingRequest PrintingRequest @relation(fields: [printingRequestId], references: [id])
printingRequestId String
numPage Int @default(0)
Expand Down
43 changes: 28 additions & 15 deletions src/handlers/printingRequest.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,7 @@ const handleUploadingFile = async (printingRequestId: string, file: Buffer, file
* @param newConfig
* @returns The object include amount page and coins of a new file with new config
*/
const calculateNewAmountPageAndCoins = async (fileMinioName: string, newConfig: UploadConfigBodyDto) => {
const fileBuffer = await minio.getFileFromMinio(fileMinioName);

const configurationFileBuffer = await editPdf.editPdfPrinting(
fileBuffer,
newConfig.pageSide,
newConfig.pages,
newConfig.layout,
Number(newConfig.pagesPerSheet) as PagePerSheet
);

const calculateNewAmountPageAndCoins = async (configurationFileBuffer: Buffer) => {
const newAmountPages = await getNumPages(configurationFileBuffer);

const newAmountCoins = newAmountPages * (await DBConfiguration.coinPerPage());
Expand All @@ -225,6 +215,13 @@ const calculateNewAmountPageAndCoins = async (fileMinioName: string, newConfig:
};

const handleUploadingConfig = async (fileId: string, config: UploadConfigBodyDto) => {
function addPreviewToFileName(fileName: string) {
const parts = fileName.split('.');
const extension = parts.pop();
const baseName = parts.join('.');
const previewFileName = `${baseName}-preview.${extension}`;
return previewFileName;
}
try {
const file = await prisma.file.findUnique({
where: {
Expand All @@ -234,8 +231,18 @@ const handleUploadingConfig = async (fileId: string, config: UploadConfigBodyDto
if (!file) throw new Error(`File ${fileId} doesn't exist`);
const minioName = file.minioName;

const fileBuffer = await minio.getFileFromMinio(file.minioName);

const configurationFileBuffer = await editPdf.editPdfPrinting(
fileBuffer,
config.pageSide,
config.pages,
config.layout,
Number(config.pagesPerSheet) as PagePerSheet
);

const newAmountCopies = config.numOfCopies;
const { newAmountCoins, newAmountPages } = await calculateNewAmountPageAndCoins(minioName, config);
const { newAmountCoins, newAmountPages } = await calculateNewAmountPageAndCoins(configurationFileBuffer);

const oldTotalCoin = file.fileNum * file.fileCoin;
const oldTotalPage = file.fileNum * file.numPage;
Expand All @@ -248,6 +255,8 @@ const handleUploadingConfig = async (fileId: string, config: UploadConfigBodyDto
const configBuffer = Buffer.from(configJSON);
const configName = convertFileNameToConfigFileName(minioName);

const previewFileName = addPreviewToFileName(file.minioName);

await prisma.$transaction(async () => {
await prisma.file.update({
where: {
Expand All @@ -256,7 +265,8 @@ const handleUploadingConfig = async (fileId: string, config: UploadConfigBodyDto
data: {
fileNum: newAmountCopies,
fileCoin: newAmountCoins,
numPage: newAmountPages
numPage: newAmountPages,
previewMinioName: previewFileName
}
});

Expand All @@ -276,6 +286,8 @@ const handleUploadingConfig = async (fileId: string, config: UploadConfigBodyDto

await removeConfigInMinio(file);

await minio.uploadFileToMinio(previewFileName, configurationFileBuffer);

await minio.uploadFileToMinio(configName, configBuffer);
});

Expand Down Expand Up @@ -392,7 +404,7 @@ const uploadConfigToPrintingRequest: Handler<UploadConfigResultDto, { Params: Up

const printFileFromBuffer = async (printer: Printer, fileBuffer: Buffer) => {
try {
await printer.print(fileBuffer, 'AUTO', 'PDF');
await printer.print(fileBuffer, 'AUTO', 'Canon_MF3010_3');
} catch (err) {
throw err;
}
Expand All @@ -406,6 +418,7 @@ const getFilesOfPrintingRequest = async (printingRequestId: string) => {
select: {
id: true,
minioName: true,
previewMinioName: true,
realName: true,
fileCoin: true,
fileSize: true,
Expand Down Expand Up @@ -484,7 +497,7 @@ const getAllFilesPrintingRequest: Handler<AllFilesPrintingRequestResultDto, { Pa
fileName: item.realName,
fileCoin: item.fileCoin,
fileSize: item.fileSize,
fileURL: `${envs.MINIO_URL}/${envs.MINIO_BUCKET_NAME}/${item.minioName}`,
fileURL: `${envs.MINIO_URL}/${envs.MINIO_BUCKET_NAME}/${item.previewMinioName}`,
numPage: item.numPage,
numOfCopies: item.fileNum
};
Expand Down
2 changes: 1 addition & 1 deletion src/routes/apis/configuration.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const configurationPlugin = createRoutes('Configuration', [
{
method: 'GET',
url: '/',
roles: ['*'],
roles: [USER_ROLES.admin],
schema: {
summary: 'Get list configurations',
description: 'Get all current name and value of configurations',
Expand Down
17 changes: 16 additions & 1 deletion src/utils/editPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { degrees, PDFDocument } from 'pdf-lib';
// export type PageSide = 'one' | 'both';
// export type EdgeBinding = 'long' | 'short';
// export type PageSideEdge = 'one' | EdgeBinding;
// export type KeepPages = 'all' | 'odd' | 'even' | string[];
// export type KeepPages = 'all' | 'odd' | 'even' | string;
// export type Orientation = 'portrait' | 'landscape';
// export type PagePerSheet = 1 | 2 | 4 | 6 | 9 | 16;

Expand Down Expand Up @@ -259,4 +259,19 @@ const editPdfPrinting = async (
return withPageSide;
};

// const testFunction = async () => {
// const rootPath = process.cwd();
// const inputPath = path.join(rootPath, 'pdf/in/file.pdf');
// const outputPath = path.join(rootPath, 'pdf/out/file.pdf');

// const pdfBuffer = await fs.readFile(inputPath);

// const modifiedPdfBuffer = await editPdfPrinting(pdfBuffer, 'long', '1', 'portrait', 1);

// await fs.writeFile(outputPath, modifiedPdfBuffer);
// logger.info('done');
// };

// testFunction();

export const editPdf = { setPageSide, setKeepPages, convertToPortraitOrLandscape, setTwoSideShortLongEdge, editPdfPrinting };

0 comments on commit 4edae88

Please sign in to comment.