forked from ytgov/digital-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Pdf merger #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burkkyy
wants to merge
7
commits into
main
Choose a base branch
from
pdf-merger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pdf merger #2
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a9ee4b
pdf lib for pdf merging
burkkyy 88a4b1a
pdf merging class and service
burkkyy c04f9ce
enhaned pdf signer to take in a buffer
burkkyy 8cb0806
pdf converter job changes
burkkyy 115b7ba
upload buffer helper function
burkkyy ff069e2
archiver item upload job
burkkyy 2ff5023
not using this
burkkyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { isNil } from "lodash" | ||
|
|
||
| import { FileStorageService } from "@/services" | ||
| import { ArchiveItemFile } from "@/models" | ||
| import cache from "@/db/cache-client" | ||
|
|
||
| import { signPDFWithPAdES } from "@/utils/pdf-signer" | ||
| import { PDFMerger } from "@/lib/pdf-merger" | ||
| import logger from "@/utils/logger" | ||
|
|
||
| export class ArchiveItemUploadJob { | ||
| name = "arhive-item-upload" | ||
| schedule = "*/1 * * * *" | ||
|
|
||
| constructor() {} | ||
|
|
||
| async run(statDate: Date) { | ||
| logger.info("Running Archive Item Upload Job", statDate) | ||
| const toUploads = await cache.getKeysByPattern(`PENDING_FILESTORE_UPLOAD_ARCHIVE_ITEM_ID_`) | ||
| const fileStore = new FileStorageService() | ||
|
|
||
| for (const key of toUploads) { | ||
| const data = await cache.getValue(key) | ||
|
|
||
| if (isNil(data)) return | ||
|
|
||
| const archiveItemInfo = JSON.parse(data) | ||
|
|
||
| const archiveFiles = await ArchiveItemFile.findAll({ | ||
| where: { | ||
| archiveItemId: archiveItemInfo.archiveItemId, | ||
| }, | ||
| }) | ||
|
|
||
| const merger = new PDFMerger() | ||
|
|
||
| for (const archiveFile of archiveFiles) { | ||
| if (isNil(archiveFile.pdfKey)) return | ||
|
|
||
| const file = await fileStore.downloadFile(archiveFile.pdfKey) | ||
| await merger.add(file) | ||
| } | ||
|
|
||
| const mergedPDF = await merger.saveAsBuffer() | ||
| const signedMergedPdf = await signPDFWithPAdES(mergedPDF) | ||
|
|
||
| /* FILE STORE UPLOAD */ | ||
| // Not sure where it should go | ||
|
|
||
| // const folderKey = archiveItemInfo.originalKey.substring( | ||
| // 0, | ||
| // archiveItemInfo.originalKey.indexOf("/") | ||
| // ) | ||
| // const mergedAndSignedPdfKey = `${folderKey}/${fileStore.makeKey()}` | ||
|
|
||
| // const uploadResp = await fileStore.uploadBuffer(mergedAndSignedPdfKey, signedMergedPdf) | ||
|
|
||
| // if (uploadResp.errorCode) { | ||
| // throw Error("File upload error") | ||
| // } | ||
|
|
||
| // update file record? | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the file record need to be updated? Or does the filestore only care about the merged pdf? |
||
|
|
||
| cache.deleteValue(key) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { PDFMerger } from "./pdf-merger" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { isUndefined } from "lodash" | ||
| import { writeFileSync } from "fs" | ||
| import { PDFDocument } from "pdf-lib" | ||
|
|
||
| interface PDFMetadata { | ||
| producer?: string | ||
| author?: string | ||
| title?: string | ||
| creator?: string | ||
| } | ||
|
|
||
| export class PDFMerger { | ||
| private doc?: PDFDocument | ||
| private options = { | ||
| ignoreEncryption: true, | ||
| } | ||
|
|
||
| async setMetadata(metadata: PDFMetadata): Promise<void> { | ||
| if (isUndefined(this.doc)) { | ||
| this.doc = await PDFDocument.create() | ||
| this.doc.setProducer("digital-vault") | ||
| this.doc.setCreationDate(new Date()) | ||
| } | ||
|
|
||
| if (metadata.producer) this.doc.setProducer(metadata.producer) | ||
| if (metadata.author) this.doc.setAuthor(metadata.author) | ||
| if (metadata.title) this.doc.setTitle(metadata.title) | ||
| if (metadata.creator) this.doc.setCreator(metadata.creator) | ||
| } | ||
|
|
||
| async add(input: Buffer, pages?: number[]): Promise<void> { | ||
| if (isUndefined(this.doc)) { | ||
| this.doc = await PDFDocument.create() | ||
| this.doc.setProducer("digital-vault") | ||
| this.doc.setCreationDate(new Date()) | ||
| } | ||
|
|
||
| const srcDoc = await PDFDocument.load(input, this.options) | ||
| let indices = [] | ||
| if (isUndefined(pages)) { | ||
| indices = srcDoc.getPageIndices() | ||
| } else { | ||
| indices = pages.map((p) => p - 1) | ||
| } | ||
|
|
||
| const copiedPages = await this.doc.copyPages(srcDoc, indices) | ||
| copiedPages.forEach((page) => { | ||
| this.doc?.addPage(page) | ||
| }) | ||
| } | ||
|
|
||
| async saveAsBuffer(): Promise<Buffer> { | ||
| if (isUndefined(this.doc)) { | ||
| this.doc = await PDFDocument.create() | ||
| this.doc.setProducer("digital-vault") | ||
| this.doc.setCreationDate(new Date()) | ||
| } | ||
|
|
||
| const uInt8Array = await this.doc.save() | ||
| return Buffer.from(uInt8Array) | ||
| } | ||
|
|
||
| async save(fileName: string): Promise<void> { | ||
| if (isUndefined(this.doc)) { | ||
| this.doc = await PDFDocument.create() | ||
| this.doc.setProducer("digital-vault") | ||
| this.doc.setCreationDate(new Date()) | ||
| } | ||
|
|
||
| const pdf = await this.doc.save() | ||
| writeFileSync(fileName, pdf) | ||
| } | ||
| } | ||
|
|
||
| export default PDFMerger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where on the file store should the merged(and signed) pdf go?