@@ -7,6 +7,8 @@ const _ = require("lodash");
77const winston = require ( "winston" ) ;
88const logger = winston . loggers . get ( "defaultLogger" ) ;
99const { logUserData } = require ( "./../../utils/helper" ) ;
10+ const { customFetch } = require ( "../../utils/helper" ) ;
11+ const { Readable } = require ( "stream" ) ;
1012
1113var JSZip = require ( "jszip" ) ;
1214PDLQueue . on ( "active" , ( job , jobPromise ) => {
@@ -57,12 +59,41 @@ async function getZipAndBytelength(no_of_pages, id, title, job) {
5759 return [ zip , byteLength ] ;
5860}
5961
60- function setHeaders ( metadata , byteLength , title ) {
62+ async function getPdfAndBytelength ( pdfUrl , job ) {
63+ try {
64+ const response = await customFetch (
65+ pdfUrl ,
66+ "GET" ,
67+ new Headers ( {
68+ "Content-Type" : "application/pdf" ,
69+ } ) ,
70+ "pdf"
71+ ) ;
72+ if ( response . status === 200 ) {
73+ job . progress ( 30 ) ;
74+ const buffer = await response . buffer ( ) ;
75+ job . progress ( 60 ) ;
76+ return {
77+ pdfBuffer : buffer ,
78+ byteLength : buffer . byteLength ,
79+ } ;
80+ } else {
81+ throw new Error (
82+ `Failed to download PDF. Status Code: ${ response . status } `
83+ ) ;
84+ }
85+ } catch ( error ) {
86+ console . error ( "Error:" , error ) ;
87+ return null ;
88+ }
89+ }
90+
91+ function setHeaders ( metadata , byteLength , title , contentType ) {
6192 let headers = { } ;
6293 headers [
6394 "Authorization"
6495 ] = `LOW ${ process . env . access_key } :${ process . env . secret_key } ` ;
65- headers [ "Content-type" ] = " application/zip" ;
96+ headers [ "Content-type" ] = ` application/${ contentType } ` ;
6697 headers [ "Content-length" ] = byteLength ;
6798 headers [ "X-Amz-Auto-Make-Bucket" ] = 1 ;
6899 headers [ "X-Archive-meta-collection" ] = "opensource" ;
@@ -83,11 +114,16 @@ function setHeaders(metadata, byteLength, title) {
83114 return headers ;
84115}
85116
86- async function uploadToIA ( zip , metadata , byteLength , email , job ) {
117+ async function uploadZipToIA ( zip , metadata , byteLength , email , job ) {
87118 const bucketTitle = metadata . IAIdentifier ;
88119 const IAuri = `http://s3.us.archive.org/${ bucketTitle } /${ bucketTitle } _images.zip` ;
89120 metadata = _ . omit ( metadata , "coverImage" ) ;
90- let headers = setHeaders ( metadata , byteLength , metadata . title ) ;
121+ let headers = setHeaders (
122+ metadata ,
123+ byteLength ,
124+ metadata . title ,
125+ job . data . details . contentType
126+ ) ;
91127 await zip . generateNodeStream ( { type : "nodebuffer" , streamFiles : true } ) . pipe (
92128 request (
93129 {
@@ -112,27 +148,78 @@ async function uploadToIA(zip, metadata, byteLength, email, job) {
112148 ) ;
113149}
114150
115- PDLQueue . process ( async ( job , done ) => {
116- const jobLogs = job . data . details ;
117- const trueURI = `http://archive.org/details/${ job . data . details . IAIdentifier } ` ;
118- jobLogs [ "trueURI" ] = trueURI ;
119- jobLogs [ "userName" ] = job . data . details . userName ;
120- job . log ( JSON . stringify ( jobLogs ) ) ;
121- logUserData ( jobLogs [ "userName" ] , "Panjab Digital Library" ) ;
122- const [ zip , byteLength ] = await getZipAndBytelength (
123- job . data . details . Pages ,
124- job . data . details . bookID ,
125- job . data . details . title ,
126- job
127- ) ;
128- job . progress ( 90 ) ;
129- await uploadToIA (
130- zip ,
131- job . data . details ,
151+ async function uploadPdfToIA ( pdfBuffer , metadata , byteLength , email , job ) {
152+ const bucketTitle = metadata . IAIdentifier ;
153+ const IAuri = `http://s3.us.archive.org/${ bucketTitle } /${ bucketTitle } .pdf` ;
154+ let headers = setHeaders (
155+ metadata ,
132156 byteLength ,
133- job . data . details . email ,
134- job
157+ metadata . title ,
158+ job . data . details . contentType
135159 ) ;
136- job . progress ( 100 ) ;
137- done ( null , true ) ;
160+ const options = {
161+ method : "PUT" ,
162+ uri : IAuri ,
163+ headers : headers ,
164+ } ;
165+ const readableStream = Readable . from ( pdfBuffer ) ;
166+ readableStream . pipe (
167+ request ( options , async ( error , response , body ) => {
168+ if ( response . statusCode === 200 ) {
169+ // EmailProducer(email, metadata.title, IAuri, true);
170+ } else {
171+ logger . log ( {
172+ level : "error" ,
173+ message : `IA Failure PDL ${ body } ` ,
174+ } ) ;
175+ }
176+ } )
177+ ) ;
178+ }
179+
180+ PDLQueue . process ( async ( job , done ) => {
181+ try {
182+ const jobLogs = job . data . details ;
183+ const trueURI = `http://archive.org/details/${ job . data . details . IAIdentifier } ` ;
184+ jobLogs [ "trueURI" ] = trueURI ;
185+ jobLogs [ "userName" ] = job . data . details . userName ;
186+ job . log ( JSON . stringify ( jobLogs ) ) ;
187+ logUserData ( jobLogs [ "userName" ] , "Panjab Digital Library" ) ;
188+
189+ if ( job . data . details . pdfUrl ) {
190+ const { pdfBuffer, byteLength } = await getPdfAndBytelength (
191+ job . data . details . pdfUrl ,
192+ job
193+ ) ;
194+ await uploadPdfToIA (
195+ pdfBuffer ,
196+ job . data . details ,
197+ byteLength ,
198+ job . data . details . email ,
199+ job
200+ ) ;
201+ job . progress ( 100 ) ;
202+ done ( null , true ) ;
203+ } else {
204+ const [ zip , byteLength ] = await getZipAndBytelength (
205+ job . data . details . Pages ,
206+ job . data . details . bookID ,
207+ job . data . details . title ,
208+ job
209+ ) ;
210+ job . progress ( 90 ) ;
211+ await uploadZipToIA (
212+ zip ,
213+ job . data . details ,
214+ byteLength ,
215+ job . data . details . email ,
216+ job
217+ ) ;
218+ job . progress ( 100 ) ;
219+ done ( null , true ) ;
220+ }
221+ } catch ( error ) {
222+ console . error ( "Error processing job:" , error ) ;
223+ done ( new Error ( error ) ) ;
224+ }
138225} ) ;
0 commit comments