Skip to content

Commit 4cd5f2f

Browse files
authored
Merge pull request #4490 from omnivore-app/jacksonh/export-pdf-content
Add PDFs to export content
2 parents 9808d75 + ddc9808 commit 4cd5f2f

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

packages/api/src/jobs/export.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import archiver, { Archiver } from 'archiver'
22
import { v4 as uuidv4 } from 'uuid'
3-
import { LibraryItem, LibraryItemState } from '../entity/library_item'
3+
import {
4+
ContentReaderType,
5+
LibraryItem,
6+
LibraryItemState,
7+
} from '../entity/library_item'
48
import { TaskState } from '../generated/graphql'
59
import { findExportById, saveExport } from '../services/export'
610
import { findHighlightsByLibraryItemId } from '../services/highlights'
@@ -13,8 +17,14 @@ import { sendExportJobEmail } from '../services/send_emails'
1317
import { findActiveUser } from '../services/user'
1418
import { logger } from '../utils/logger'
1519
import { highlightToMarkdown } from '../utils/parser'
16-
import { contentFilePath, createGCSFile } from '../utils/uploads'
20+
import {
21+
contentFilePath,
22+
createGCSFile,
23+
generateUploadFilePathName,
24+
} from '../utils/uploads'
1725
import { batch } from 'googleapis/build/src/apis/batch'
26+
import { getRepository } from '../repository'
27+
import { UploadFile } from '../entity/upload_file'
1828

1929
export interface ExportJobData {
2030
userId: string
@@ -79,6 +89,30 @@ const uploadContent = async (
7989
})
8090
}
8191

92+
const uploadPdfContent = async (
93+
libraryItem: LibraryItem,
94+
archive: Archiver
95+
) => {
96+
const upload = await getRepository(UploadFile).findOneBy({
97+
id: libraryItem.uploadFileId,
98+
})
99+
if (!upload || !upload.fileName) {
100+
console.log(`upload does not have a filename: ${upload}`)
101+
return
102+
}
103+
104+
const filePath = generateUploadFilePathName(upload.id, upload.fileName)
105+
const file = createGCSFile(filePath)
106+
const [exists] = await file.exists()
107+
if (exists) {
108+
console.log(`adding PDF file: ${filePath}`)
109+
// append the existing file to the archive
110+
archive.append(file.createReadStream(), {
111+
name: `content/${libraryItem.slug}.pdf`,
112+
})
113+
}
114+
}
115+
82116
const uploadToBucket = async (
83117
userId: string,
84118
items: Array<LibraryItem>,
@@ -111,7 +145,11 @@ const uploadToBucket = async (
111145
// Loop through the items and add files to /content and /highlights directories
112146
for (const item of items) {
113147
// Add content files to /content
114-
await uploadContent(userId, item, archive)
148+
if (item.uploadFileId) {
149+
await uploadPdfContent(item, archive)
150+
} else {
151+
await uploadContent(userId, item, archive)
152+
}
115153

116154
if (item.highlightAnnotations?.length) {
117155
const highlights = await findHighlightsByLibraryItemId(item.id, userId)

0 commit comments

Comments
 (0)