app.get('/files/:filename', (req, res) => { const inputPath = path.resolve(__dirname, filesPath, req.params.filename); fs.readFile(inputPath, function (err, data) { if (err) { res.statusCode = 500; res.end(`Error getting the file: ${err}.`); } else { const ext = path.parse(inputPath).ext; res.setHeader('Content-type', mimeType[ext] || 'text/plain'); res.end(data); } }); });
app.get('/convert/:filename', (req, res) => { const filename = req.params.filename; let ext = path.parse(filename).ext; const inputPath = path.resolve(__dirname, filesPath, filename); const outputPath = path.resolve(__dirname, filesPath, `${filename}.pdf`); if (ext === '.pdf') { res.statusCode = 500; res.end(`File is already PDF.`); } const main = async () => { const pdfdoc = await PDFNet.PDFDoc.create(); await pdfdoc.initSecurityHandler(); await PDFNet.Convert.toPdf(pdfdoc, inputPath); pdfdoc.save( outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized, ); }; PDFNetEndpoint(main, outputPath, res); });
function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) { original = new RegExp(RegExp.quote(original), 'g'); replacement = RegExp.quoteReplacement(replacement); var updateFile = !DRY_RUN ? function (filepath) { if (allowedExtensions.has(path.parse(filepath).ext)) { sed('-i', original, replacement, filepath); } } : function (filepath) { if (allowedExtensions.has(path.parse(filepath).ext)) { console.log('FILE: ' + filepath); } else { console.log('EXCLUDED:' + filepath); } }; walkAsync(directory, excludedDirectories, updateFile, function (err) { console.error('ERROR while traversing directory!:'); console.error(err); process.exit(1); }); }
function downloadFile(file, fileName) { console.log('Download started for ' + fileName); let sourcePath = path.parse(fileName); return new Promise((resolve, reject) => { let tempDestination = '/tmp/' + fileName; file.download({ destination: tempDestination }).then((error) => { console.log('Download is done ' + error); if (error.length > 0) { reject(error); } else { resolve({ source: { name: sourcePath.name, ext: sourcePath.ext }, destination: { temp: { video: tempDestination } } }); } }); }); }
file.name = `photo_${id}${path.parse(file.name).ext}`;
const cacheAssetPath = path.join(this.cachePath, pathInfo.dir, `${pathInfo.name}${pathInfo.ext}`); promisify(fs.writeFile)(cacheAssetPath, response.body).catch(err => { console.log('Write asset cache error:', err);
app.get('/watermark/:filename-:watermark', (req, res) => { const filename = req.params.filename; const watermark = req.params.watermark; let ext = path.parse(filename).ext;
app.get('/thumbnail/:filename', (req, res) => { const filename = req.params.filename; let ext = path.parse(filename).ext; const inputPath = path.resolve(__dirname, filesPath, filename); const outputPath = path.resolve(__dirname, filesPath, `${filename}.png`); if (ext !== '.pdf') { throw `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ext}.`; } const main = async () => { const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath); await doc.initSecurityHandler(); const pdfdraw = await PDFNet.PDFDraw.create(92); const currPage = await doc.getPage(1); await pdfdraw.export(currPage, outputPath, 'PNG'); }; PDFNetEndpoint(main, outputPath, res); });
if(parsed_path.ext == '.html')
app.get('/textExtract/:filename-:pagenumber', (req, res) => { const filename = req.params.filename; let pageNumber = Number(req.params.pagenumber); let ext = path.parse(filename).ext;
const PDFNetEndpoint = (main, pathname, res) => { PDFNet.runWithCleanup(main) // you can add the key to PDFNet.runWithCleanup(main, process.env.PDFTRONKEY) .then(() => { PDFNet.shutdown(); fs.readFile(pathname, (err, data) => { if (err) { res.statusCode = 500; res.end(`Error getting the file: ${err}.`); } else { const ext = path.parse(pathname).ext; res.setHeader('Content-type', mimeType[ext] || 'text/plain'); res.end(data); } }); }) .catch((error) => { res.statusCode = 500; res.end(error); }); }
app.get('/optimize/:filename', (req, res) => { const filename = req.params.filename; const ext = path.parse(filename).ext;