@@ -4,11 +4,13 @@ const path = require('path');
44const { PDFNet } = require ( '@pdftron/pdfnet-node' ) ;
55const mimeType = require ( './modules/mimeType' ) ;
66const port = 9000 ;
7+ const filesPath = './files' ;
78
89const app = express ( ) ;
910
1011app . get ( '/files' , ( req , res ) => {
11- fs . readdir ( './files' , function ( err , files ) {
12+ const inputPath = path . resolve ( __dirname , filesPath ) ;
13+ fs . readdir ( inputPath , function ( err , files ) {
1214 if ( err ) {
1315 return console . log ( 'Unable to scan directory: ' + err ) ;
1416 }
@@ -17,18 +19,34 @@ app.get('/files', (req, res) => {
1719 } ) ;
1820} ) ;
1921
22+ app . get ( '/files/:filename' , ( req , res ) => {
23+ const inputPath = path . resolve ( __dirname , filesPath , req . params . filename ) ;
24+ fs . readFile ( inputPath , function ( err , data ) {
25+ if ( err ) {
26+ res . statusCode = 500 ;
27+ res . end ( `Error getting the file: ${ err } .` ) ;
28+ } else {
29+ const ext = path . parse ( inputPath ) . ext ;
30+ res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
31+ res . end ( data ) ;
32+ }
33+ } ) ;
34+ } ) ;
35+
2036app . get ( '/optimize/:filename' , ( req , res ) => {
21- const pathname = './files/' ;
2237 const filename = req . params . filename ;
23- const ext = path . parse ( pathname + filename ) . ext ;
38+ const ext = path . parse ( filename ) . ext ;
39+
40+ const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
41+ const outputPath = path . resolve ( __dirname , filesPath , `optimized_${ filename } ` ) ;
2442
2543 if ( ext !== '.pdf' ) {
2644 res . statusCode = 500 ;
2745 res . end ( `Only PDFs can be optimized. Cannot optimize file with extension: ${ ext } .` ) ;
2846 }
2947
3048 const main = async ( ) => {
31- const doc = await PDFNet . PDFDoc . createFromFilePath ( pathname + filename ) ;
49+ const doc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
3250 await doc . initSecurityHandler ( ) ;
3351
3452 // compress
@@ -47,39 +65,41 @@ app.get('/optimize/:filename', (req, res) => {
4765 const opts = new PDFNet . PDFDoc . ViewerOptimizedOptions ( ) ;
4866 opts . setThumbnailRenderingThreshold ( 0 ) ;
4967
50- await doc . saveViewerOptimized ( ` ${ pathname } optimized_ ${ filename } ` , opts ) ;
68+ await doc . saveViewerOptimized ( outputPath , opts ) ;
5169 } ;
5270
53- const newpath = `${ pathname } optimized_${ filename } ` ;
54- PDFNetEndpoint ( main , newpath , res ) ;
71+ PDFNetEndpoint ( main , outputPath , res ) ;
5572} ) ;
5673
5774app . get ( '/thumbnail/:filename' , ( req , res ) => {
58- const pathname = './files/' ;
5975 const filename = req . params . filename ;
60- let ext = path . parse ( pathname + filename ) . ext ;
76+ let ext = path . parse ( filename ) . ext ;
77+
78+ const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
79+ const outputPath = path . resolve ( __dirname , filesPath , `${ filename } .png` ) ;
6180
6281 if ( ext !== '.pdf' ) {
6382 res . statusCode = 500 ;
6483 res . end ( `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ ext } .` ) ;
6584 }
6685
6786 const main = async ( ) => {
68- const doc = await PDFNet . PDFDoc . createFromFilePath ( pathname + filename ) ;
87+ const doc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
6988 await doc . initSecurityHandler ( ) ;
7089 const pdfdraw = await PDFNet . PDFDraw . create ( 92 ) ;
7190 const currPage = await doc . getPage ( 1 ) ;
72- await pdfdraw . export ( currPage , ` ${ pathname } ${ filename } .png` , 'PNG' ) ;
91+ await pdfdraw . export ( currPage , outputPath , 'PNG' ) ;
7392 } ;
7493
75- const newpath = `${ pathname } ${ filename } .png` ;
76- PDFNetEndpoint ( main , newpath , res ) ;
94+ PDFNetEndpoint ( main , outputPath , res ) ;
7795} ) ;
7896
7997app . get ( '/convert/:filename' , ( req , res ) => {
80- const pathname = './files/' ;
8198 const filename = req . params . filename ;
82- let ext = path . parse ( pathname + filename ) . ext ;
99+ let ext = path . parse ( filename ) . ext ;
100+
101+ const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
102+ const outputPath = path . resolve ( __dirname , filesPath , `${ filename } .pdf` ) ;
83103
84104 if ( ext === '.pdf' ) {
85105 res . statusCode = 500 ;
@@ -89,28 +109,12 @@ app.get('/convert/:filename', (req, res) => {
89109 const main = async ( ) => {
90110 const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
91111 await pdfdoc . initSecurityHandler ( ) ;
92- const inputFile = pathname + filename ;
93- await PDFNet . Convert . toPdf ( pdfdoc , inputFile ) ;
112+ await PDFNet . Convert . toPdf ( pdfdoc , inputPath ) ;
94113 pdfdoc . save ( `${ pathname } ${ filename } .pdf` , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
95114 ext = '.pdf' ;
96115 } ;
97116
98- const newpath = `${ pathname } ${ filename } .pdf` ;
99- PDFNetEndpoint ( main , newpath , res ) ;
100- } ) ;
101-
102- app . get ( '/files/:filename' , ( req , res ) => {
103- const pathname = `./files/${ req . params . filename } ` ;
104- fs . readFile ( pathname , function ( err , data ) {
105- if ( err ) {
106- res . statusCode = 500 ;
107- res . end ( `Error getting the file: ${ err } .` ) ;
108- } else {
109- const ext = path . parse ( pathname ) . ext ;
110- res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
111- res . end ( data ) ;
112- }
113- } ) ;
117+ PDFNetEndpoint ( main , outputPath , res ) ;
114118} ) ;
115119
116120const PDFNetEndpoint = ( main , pathname , res ) => {
0 commit comments