1
1
import archiver , { Archiver } from 'archiver'
2
2
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'
4
8
import { TaskState } from '../generated/graphql'
5
9
import { findExportById , saveExport } from '../services/export'
6
10
import { findHighlightsByLibraryItemId } from '../services/highlights'
@@ -13,8 +17,14 @@ import { sendExportJobEmail } from '../services/send_emails'
13
17
import { findActiveUser } from '../services/user'
14
18
import { logger } from '../utils/logger'
15
19
import { highlightToMarkdown } from '../utils/parser'
16
- import { contentFilePath , createGCSFile } from '../utils/uploads'
20
+ import {
21
+ contentFilePath ,
22
+ createGCSFile ,
23
+ generateUploadFilePathName ,
24
+ } from '../utils/uploads'
17
25
import { batch } from 'googleapis/build/src/apis/batch'
26
+ import { getRepository } from '../repository'
27
+ import { UploadFile } from '../entity/upload_file'
18
28
19
29
export interface ExportJobData {
20
30
userId : string
@@ -79,6 +89,30 @@ const uploadContent = async (
79
89
} )
80
90
}
81
91
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
+
82
116
const uploadToBucket = async (
83
117
userId : string ,
84
118
items : Array < LibraryItem > ,
@@ -111,7 +145,11 @@ const uploadToBucket = async (
111
145
// Loop through the items and add files to /content and /highlights directories
112
146
for ( const item of items ) {
113
147
// 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
+ }
115
153
116
154
if ( item . highlightAnnotations ?. length ) {
117
155
const highlights = await findHighlightsByLibraryItemId ( item . id , userId )
0 commit comments