@@ -45,10 +45,7 @@ app.get('/optimize/:filename', (req, res) => {
4545 ) ;
4646
4747 if ( ext !== '.pdf' ) {
48- res . statusCode = 500 ;
49- res . end (
50- `Only PDFs can be optimized. Cannot optimize file with extension: ${ ext } .` ,
51- ) ;
48+ throw `Only PDFs can be optimized. Cannot optimize file with extension: ${ ext } .` ;
5249 }
5350
5451 const main = async ( ) => {
@@ -85,10 +82,7 @@ app.get('/thumbnail/:filename', (req, res) => {
8582 const outputPath = path . resolve ( __dirname , filesPath , `${ filename } .png` ) ;
8683
8784 if ( ext !== '.pdf' ) {
88- res . statusCode = 500 ;
89- res . end (
90- `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ ext } .` ,
91- ) ;
85+ throw `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ ext } .` ;
9286 }
9387
9488 const main = async ( ) => {
@@ -144,14 +138,12 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
144138 const main = async ( ) => {
145139 await PDFNet . initialize ( ) ;
146140 try {
147- await PDFNet . startDeallocateStack ( ) ;
148141 const pdfdoc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
149142 await pdfdoc . initSecurityHandler ( ) ;
150143 const page = await pdfdoc . getPage ( pageNumber ) ;
151144
152- if ( page . id === '0' ) {
153- console . log ( 'Page not found.' ) ;
154- return 1 ;
145+ if ( ! page ) {
146+ throw 'Page number is invalid.' ;
155147 }
156148
157149 const txt = await PDFNet . TextExtractor . create ( ) ;
@@ -163,11 +155,8 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
163155 fs . writeFile ( outputPath , text , ( err ) => {
164156 if ( err ) return console . log ( err ) ;
165157 } ) ;
166- await PDFNet . endDeallocateStack ( ) ;
167158 } catch ( err ) {
168- console . log ( err ) ;
169- console . log ( err . stack ) ;
170- return 1 ;
159+ throw err ;
171160 }
172161 } ;
173162
@@ -176,13 +165,9 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
176165
177166const PDFNetEndpoint = ( main , pathname , res ) => {
178167 PDFNet . runWithCleanup ( main )
179- . catch ( function ( error ) {
180- res . statusCode = 500 ;
181- res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
182- } )
183- . then ( function ( ) {
168+ . then ( ( ) => {
184169 PDFNet . shutdown ( ) ;
185- fs . readFile ( pathname , function ( err , data ) {
170+ fs . readFile ( pathname , ( err , data ) => {
186171 if ( err ) {
187172 res . statusCode = 500 ;
188173 res . end ( `Error getting the file: ${ err } .` ) ;
@@ -192,6 +177,10 @@ const PDFNetEndpoint = (main, pathname, res) => {
192177 res . end ( data ) ;
193178 }
194179 } ) ;
180+ } )
181+ . catch ( ( error ) => {
182+ res . statusCode = 500 ;
183+ res . end ( error ) ;
195184 } ) ;
196185} ;
197186
0 commit comments