download(urlPath, filePath) { return new Promise((resolve, reject) => { const parsed = url.parse(urlPath); let relativePath = filePath; if (!relativePath) { const downloadDir = path.parse(absolutePath).dir; Downloader.ensureDir(downloadDir); const proto = parsed.protocol === 'https:' ? https : http;
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 } } }); } }); }); }
function walkAsync(directory, excludedDirectories, fileCallback, errback) { if (excludedDirectories.has(path.parse(directory).base)) { return; } fs.readdir(directory, function (err, names) { if (err) { errback(err); return; } names.forEach(function (name) { var filepath = path.join(directory, name); fs.lstat(filepath, function (err, stats) { if (err) { process.nextTick(errback, err); return; } if (stats.isSymbolicLink()) { return; } else if (stats.isDirectory()) { process.nextTick(walkAsync, filepath, excludedDirectories, fileCallback, errback); } else if (stats.isFile()) { process.nextTick(fileCallback, filepath); } }); }); }); }
process(src, filename) { const assetFilename = JSON.stringify(path.basename(filename)); if (filename.match(/\.svg$/)) { // Based on how SVGR generates a component name: // https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6 const pascalCaseFilename = camelcase(path.parse(filename).name, { pascalCase: true, }); const componentName = `Svg${pascalCaseFilename}`; return `const React = require('react'); module.exports = { __esModule: true, default: ${assetFilename}, ReactComponent: React.forwardRef(function ${componentName}(props, ref) { return { $$typeof: Symbol.for('react.element'), type: 'svg', ref: ref, key: null, props: Object.assign({}, props, { children: ${assetFilename} }) }; }), };`; } return `module.exports = ${assetFilename};`; }
file.name = `photo_${id}${path.parse(file.name).ext}`;
const pathInfo = path.parse(req.url.substr(1));
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); });
app.get('/watermark/:filename-:watermark', (req, res) => { const filename = req.params.filename; const watermark = req.params.watermark; 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('/textExtract/:filename-:pagenumber', (req, res) => { const filename = req.params.filename; let pageNumber = Number(req.params.pagenumber); let ext = path.parse(filename).ext;
app.get('/optimize/:filename', (req, res) => { const filename = req.params.filename; const ext = path.parse(filename).ext;
process(src, filename) { const assetFilename = JSON.stringify(path.basename(filename)); if (filename.match(/\.svg$/)) { // Based on how SVGR generates a component name: // https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6 const pascalCaseFileName = camelcase(path.parse(filename).name, { pascalCase: true, }); const componentName = `Svg${pascalCaseFileName}`; return `const React = require('react'); module.exports = { __esModule: true, default: ${assetFilename}, ReactComponent: React.forwardRef(function ${componentName}(props, ref) { return { $$typeof: Symbol.for('react.element'), type: 'svg', ref: ref, key: null, props: Object.assign({}, props, { children: ${assetFilename} }) }; }), };`; } return `module.exports = ${assetFilename};`; }