mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat: migrate the printing from Attachment to Document model
This commit is contained in:
@@ -5,7 +5,11 @@ import { PageProperties, PdfFormat } from '@/lib/Chromiumly/_types';
|
|||||||
import { UrlConverter } from '@/lib/Chromiumly/UrlConvert';
|
import { UrlConverter } from '@/lib/Chromiumly/UrlConvert';
|
||||||
import HasTenancyService from '../Tenancy/TenancyService';
|
import HasTenancyService from '../Tenancy/TenancyService';
|
||||||
import { Chromiumly } from '@/lib/Chromiumly/Chromiumly';
|
import { Chromiumly } from '@/lib/Chromiumly/Chromiumly';
|
||||||
import { PDF_FILE_EXPIRE_IN, getPdfFilesStorageDir } from './utils';
|
import {
|
||||||
|
PDF_FILE_EXPIRE_IN,
|
||||||
|
getPdfFilePath,
|
||||||
|
getPdfFilesStorageDir,
|
||||||
|
} from './utils';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class ChromiumlyHtmlConvert {
|
export class ChromiumlyHtmlConvert {
|
||||||
@@ -22,22 +26,16 @@ export class ChromiumlyHtmlConvert {
|
|||||||
tenantId: number,
|
tenantId: number,
|
||||||
content: string
|
content: string
|
||||||
): Promise<[string, () => Promise<void>]> {
|
): Promise<[string, () => Promise<void>]> {
|
||||||
const { Attachment } = this.tenancy.models(tenantId);
|
const { Document } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
const filename = `document-${Date.now()}.html`;
|
const filename = `document-print-${Date.now()}.html`;
|
||||||
const storageDir = getPdfFilesStorageDir(filename);
|
const filePath = getPdfFilePath(filename);
|
||||||
const filePath = path.join(global.__storage_dir, storageDir);
|
|
||||||
|
|
||||||
await fs.writeFile(filePath, content);
|
await fs.writeFile(filePath, content);
|
||||||
await Attachment.query().insert({
|
await Document.query().insert({ key: filename, mimeType: 'text/html' });
|
||||||
key: filename,
|
|
||||||
path: storageDir,
|
|
||||||
expire_in: PDF_FILE_EXPIRE_IN, // ms
|
|
||||||
extension: 'html',
|
|
||||||
});
|
|
||||||
const cleanup = async () => {
|
const cleanup = async () => {
|
||||||
await fs.unlink(filePath);
|
await fs.unlink(filePath);
|
||||||
await Attachment.query().where('key', filename).delete();
|
await Document.query().where('key', filename).delete();
|
||||||
};
|
};
|
||||||
return [filename, cleanup];
|
return [filename, cleanup];
|
||||||
}
|
}
|
||||||
@@ -60,6 +58,7 @@ export class ChromiumlyHtmlConvert {
|
|||||||
html
|
html
|
||||||
);
|
);
|
||||||
const fileDir = getPdfFilesStorageDir(filename);
|
const fileDir = getPdfFilesStorageDir(filename);
|
||||||
|
|
||||||
const url = path.join(Chromiumly.GOTENBERG_DOCS_ENDPOINT, fileDir);
|
const url = path.join(Chromiumly.GOTENBERG_DOCS_ENDPOINT, fileDir);
|
||||||
const urlConverter = new UrlConverter();
|
const urlConverter = new UrlConverter();
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,10 @@ export const PDF_FILE_EXPIRE_IN = 40; // ms
|
|||||||
|
|
||||||
export const getPdfFilesStorageDir = (filename: string) => {
|
export const getPdfFilesStorageDir = (filename: string) => {
|
||||||
return path.join(PDF_FILE_SUB_DIR, filename);
|
return path.join(PDF_FILE_SUB_DIR, filename);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export const getPdfFilePath = (filename: string) => {
|
||||||
|
const storageDir = getPdfFilesStorageDir(filename);
|
||||||
|
|
||||||
|
return path.join(global.__storage_dir, storageDir);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user