@@ -20,6 +20,12 @@ app.get('/files', (req, res) => {
2020app . get ( '/optimize/:filename' , ( req , res ) => {
2121 const pathname = './files/' ;
2222 const filename = req . params . filename ;
23+ const ext = path . parse ( pathname + filename ) . ext ;
24+
25+ if ( ext !== '.pdf' ) {
26+ res . statusCode = 500 ;
27+ res . end ( `Only PDFs can be optimized. Cannot optimize file with extension: ${ ext } .` ) ;
28+ }
2329
2430 const main = async ( ) => {
2531 const doc = await PDFNet . PDFDoc . createFromFilePath ( pathname + filename ) ;
@@ -49,10 +55,8 @@ app.get('/optimize/:filename', (req, res) => {
4955 await doc . saveViewerOptimized ( `${ pathname } optimized_${ filename } ` , opts ) ;
5056 } ;
5157
52- // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
5358 PDFNet . runWithCleanup ( main )
5459 . catch ( function ( error ) {
55- console . log ( 'Error: ' + JSON . stringify ( error ) ) ;
5660 res . statusCode = 500 ;
5761 res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
5862 } )
@@ -64,8 +68,46 @@ app.get('/optimize/:filename', (req, res) => {
6468 res . statusCode = 500 ;
6569 res . end ( `Error getting the file: ${ err } .` ) ;
6670 } else {
67- const ext = path . parse ( newpath ) . ext ;
68- // if the file is found, set Content-type and send data
71+ res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
72+ res . end ( data ) ;
73+ }
74+ } ) ;
75+ } ) ;
76+ } ) ;
77+
78+ app . get ( '/thumbnail/:filename' , ( req , res ) => {
79+ const pathname = './files/' ;
80+ const filename = req . params . filename ;
81+ let ext = path . parse ( pathname + filename ) . ext ;
82+
83+ if ( ext !== '.pdf' ) {
84+ res . statusCode = 500 ;
85+ res . end ( `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ ext } .` ) ;
86+ }
87+
88+ const main = async ( ) => {
89+ const doc = await PDFNet . PDFDoc . createFromFilePath ( pathname + filename ) ;
90+ await doc . initSecurityHandler ( ) ;
91+ const pdfdraw = await PDFNet . PDFDraw . create ( 92 ) ;
92+ const itr = await doc . getPageIterator ( 1 ) ;
93+ const currPage = await itr . current ( ) ;
94+ await pdfdraw . export ( currPage , `${ pathname } ${ filename } .png` , 'PNG' ) ;
95+ ext = '.png' ;
96+ } ;
97+
98+ PDFNet . runWithCleanup ( main )
99+ . catch ( function ( error ) {
100+ res . statusCode = 500 ;
101+ res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
102+ } )
103+ . then ( function ( ) {
104+ PDFNet . shutdown ( ) ;
105+ const newpath = `${ pathname } ${ filename } .png` ;
106+ fs . readFile ( newpath , function ( err , data ) {
107+ if ( err ) {
108+ res . statusCode = 500 ;
109+ res . end ( `Error getting the file: ${ err } .` ) ;
110+ } else {
69111 res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
70112 res . end ( data ) ;
71113 }
@@ -81,7 +123,6 @@ app.get('/files/:filename', (req, res) => {
81123 res . end ( `Error getting the file: ${ err } .` ) ;
82124 } else {
83125 const ext = path . parse ( pathname ) . ext ;
84- // if the file is found, set Content-type and send data
85126 res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
86127 res . end ( data ) ;
87128 }
0 commit comments