Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
enoy19 committed Aug 27, 2023
1 parent f2ca4b2 commit 312f90d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
38 changes: 22 additions & 16 deletions src/lib/server/fileUtil.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import { env } from '$env/dynamic/private';
import fs from 'fs/promises';

if (!(await fileExists(env.SECRET_DATA_PATH))) {
await fs.mkdir(env.SECRET_DATA_PATH, {
recursive: true
});
await mkdirIfNotExists('.');

export async function mkdirIfNotExists(dataPath: string) {
const path = getDataFilePathFor(dataPath);

if (!(await fileExists(path, false))) {
await fs.mkdir(path, {
recursive: true
});
}
}

export async function storeObject(object: object, path: string) {
await fs.writeFile(getDataFilePathFor(path), JSON.stringify(object, undefined, 2));
export async function storeObject(object: object, dataPath: string) {
await fs.writeFile(getDataFilePathFor(dataPath), JSON.stringify(object, undefined, 2));
}

export async function createEmptyJsonFileIfNotExists(path: string) {
if (!fileExists(path)) {
await createEmptyJsonFile(path);
export async function createEmptyJsonFileIfNotExists(dataPath: string) {
if (!fileExists(dataPath)) {
await createEmptyJsonFile(dataPath);
}
}

export async function readJson(path: string) {
export async function readJson(dataPath: string) {
try {
return JSON.parse((await fs.readFile(getDataFilePathFor(path))).toString());
return JSON.parse((await fs.readFile(getDataFilePathFor(dataPath))).toString());
} catch (e) {
console.warn(`${path} not found`);
console.warn(`${dataPath} not found`);
return {};
}
}

async function createEmptyJsonFile(path: string) {
await storeObject({}, path);
async function createEmptyJsonFile(dataPath: string) {
await storeObject({}, dataPath);
}

export async function fileExists(path: string, inDataPath = true) {
Expand All @@ -39,6 +45,6 @@ export async function fileExists(path: string, inDataPath = true) {
}
}

function getDataFilePathFor(path: string) {
return `${env.SECRET_DATA_PATH}/${path}`;
export function getDataFilePathFor(dataPath: string) {
return `${env.SECRET_DATA_PATH}/${dataPath}`;
}
4 changes: 2 additions & 2 deletions src/lib/server/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export async function imageToZpl(
return `^XA^FO0,0^GFA,${totalBytes},${totalBytes},${Math.ceil(paddedWidth / 8)},${hexString}^XZ`;
}

export async function pdfToImage(buffer: Buffer | ArrayBuffer, dpi = 300) {
export async function pdfToImage(buffer: Buffer | ArrayBuffer) {
const pdfDataBuffer = Buffer.from(buffer);
const image = await convertPdfToImage(pdfDataBuffer, 1, 10);
const image = await convertPdfToImage(pdfDataBuffer, 1);

return image;
}
Expand Down
9 changes: 2 additions & 7 deletions src/lib/server/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { dpmmToDpi } from './dpiUtils';
import { imageMagick } from './imageMagick';

export async function convertPdfToImage(
buffer: Buffer,
page: number,
dpmm: number,
) {
export async function convertPdfToImage(buffer: Buffer, page: number) {
const pageSetup = `pdf[${page - 1}]`;

return new Promise<Buffer>((resolve, reject) => {
imageMagick(buffer, pageSetup)
.in('-define', 'pdf:use-cropbox=true')
.density(dpmmToDpi(dpmm), dpmmToDpi(dpmm))
.density(254, 254)
.quality(0)
.compress('jpeg')
.stream('png', (error, stdout) => {
Expand Down

0 comments on commit 312f90d

Please sign in to comment.