@@ -49,25 +49,9 @@ app.get('/optimize/:filename', (req, res) => {
4949
5050 await doc . saveViewerOptimized ( `${ pathname } optimized_${ filename } ` , opts ) ;
5151 } ;
52-
53- PDFNet . runWithCleanup ( main )
54- . catch ( function ( error ) {
55- res . statusCode = 500 ;
56- res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
57- } )
58- . then ( function ( ) {
59- PDFNet . shutdown ( ) ;
60- const newpath = `${ pathname } optimized_${ filename } ` ;
61- fs . readFile ( newpath , function ( err , data ) {
62- if ( err ) {
63- res . statusCode = 500 ;
64- res . end ( `Error getting the file: ${ err } .` ) ;
65- } else {
66- res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
67- res . end ( data ) ;
68- }
69- } ) ;
70- } ) ;
52+
53+ const newpath = `${ pathname } optimized_${ filename } ` ;
54+ PDFNetEndpoint ( main , newpath , res ) ;
7155} ) ;
7256
7357app . get ( '/thumbnail/:filename' , ( req , res ) => {
@@ -87,27 +71,10 @@ app.get('/thumbnail/:filename', (req, res) => {
8771 const itr = await doc . getPageIterator ( 1 ) ;
8872 const currPage = await itr . current ( ) ;
8973 await pdfdraw . export ( currPage , `${ pathname } ${ filename } .png` , 'PNG' ) ;
90- ext = '.png' ;
9174 } ;
9275
93- PDFNet . runWithCleanup ( main )
94- . catch ( function ( error ) {
95- res . statusCode = 500 ;
96- res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
97- } )
98- . then ( function ( ) {
99- PDFNet . shutdown ( ) ;
100- const newpath = `${ pathname } ${ filename } .png` ;
101- fs . readFile ( newpath , function ( err , data ) {
102- if ( err ) {
103- res . statusCode = 500 ;
104- res . end ( `Error getting the file: ${ err } .` ) ;
105- } else {
106- res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
107- res . end ( data ) ;
108- }
109- } ) ;
110- } ) ;
76+ const newpath = `${ pathname } ${ filename } .png` ;
77+ PDFNetEndpoint ( main , newpath , res ) ;
11178} ) ;
11279
11380app . get ( '/convert/:filename' , ( req , res ) => {
@@ -129,24 +96,8 @@ app.get('/convert/:filename', (req, res) => {
12996 ext = '.pdf' ;
13097 } ;
13198
132- PDFNet . runWithCleanup ( main )
133- . catch ( function ( error ) {
134- res . statusCode = 500 ;
135- res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
136- } )
137- . then ( function ( ) {
138- PDFNet . shutdown ( ) ;
139- const newpath = `${ pathname } ${ filename } .pdf` ;
140- fs . readFile ( newpath , function ( err , data ) {
141- if ( err ) {
142- res . statusCode = 500 ;
143- res . end ( `Error getting the file: ${ err } .` ) ;
144- } else {
145- res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
146- res . end ( data ) ;
147- }
148- } ) ;
149- } ) ;
99+ const newpath = `${ pathname } ${ filename } .pdf` ;
100+ PDFNetEndpoint ( main , newpath , res ) ;
150101} ) ;
151102
152103app . get ( '/files/:filename' , ( req , res ) => {
@@ -163,6 +114,27 @@ app.get('/files/:filename', (req, res) => {
163114 } ) ;
164115} ) ;
165116
117+ const PDFNetEndpoint = ( main , pathname , res ) => {
118+ PDFNet . runWithCleanup ( main )
119+ . catch ( function ( error ) {
120+ res . statusCode = 500 ;
121+ res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
122+ } )
123+ . then ( function ( ) {
124+ PDFNet . shutdown ( ) ;
125+ fs . readFile ( pathname , function ( err , data ) {
126+ if ( err ) {
127+ res . statusCode = 500 ;
128+ res . end ( `Error getting the file: ${ err } .` ) ;
129+ } else {
130+ const ext = path . parse ( pathname ) . ext ;
131+ res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
132+ res . end ( data ) ;
133+ }
134+ } ) ;
135+ } ) ;
136+ } ;
137+
166138app . listen ( port , ( ) =>
167139 console . log (
168140 `nodejs-convert-file-server listening at http://localhost:${ port } ` ,
0 commit comments