From 859939e76318f88b9e7aedd6dd0696203c0a272f Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Sat, 7 Dec 2019 00:02:55 +0530 Subject: [PATCH] chore: Remove thumbnail middleware --- server/index.js | 3 +-- server/utils.js | 66 +------------------------------------------------ 2 files changed, 2 insertions(+), 67 deletions(-) diff --git a/server/index.js b/server/index.js index dfef1719..1be0e3e5 100644 --- a/server/index.js +++ b/server/index.js @@ -18,7 +18,6 @@ const auth = require('./../auth/auth')(); const morgan = require('morgan'); const { addWebpackMiddleware } = require('../webpack/serve'); const { getAppConfig, resolveAppDir } = require('../webpack/utils'); -const { thumbnailMiddleware } = require('./utils'); frappe.conf = getAppConfig(); @@ -42,7 +41,7 @@ module.exports = { app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(frappe.conf.distPath)); - app.use('/static', thumbnailMiddleware(resolveAppDir(frappe.conf.staticPath))); + app.use('/static', express.static(resolveAppDir(frappe.conf.staticPath))) app.use(morgan('tiny')); diff --git a/server/utils.js b/server/utils.js index bcf5d69a..2d493c80 100644 --- a/server/utils.js +++ b/server/utils.js @@ -3,7 +3,6 @@ const fs = require('fs'); const path = require('path'); const getDirName = path.dirname; const os = require('os'); -const sharp = require('sharp'); module.exports = { writeFile(fullpath, contents) { @@ -24,68 +23,5 @@ module.exports = { getTmpDir() { return os.tmpdir(); - }, - - thumbnailMiddleware(staticPath) { - - return function (req, res, next) { - - const filename = req.path.split(path.sep).slice(-1)[0] - const dimension = req.query.size || null - const staticFile = path.join(staticPath, filename) - - fs.exists(staticFile, (exists) => { - if (!exists) { - return next() - } - - fs.stat(staticFile, (err, stats) => { - if (err) { - throw err - } - - //Check if url is static file - if (stats.isFile()) { - - // Check if url has dimension parameters - if (dimension) { - let [width, height] = dimension.split('x'); - width = +width; - height = +height; - const thumbnailPath = path.join(staticPath, 'thumbnails'); - const destination = path.join(thumbnailPath, `${width}x${height}-${filename}`) - - // create thumbnails folder if not exists - if (!fs.existsSync(thumbnailPath)) { - fs.mkdirSync(thumbnailPath); - } - - // Check if thumbnail already present - fs.existsSync(destination, (exists) => { - if (exists) - return res.sendFile(destination) - }) - - // Resize image - sharp(staticFile) - .resize(width, height) - .toFile(destination) - .then(() => { - return res.sendFile(destination) - }) - .catch(err => { - console.error(err) - }) - - } else { - return res.sendFile(staticFile) - } - } else { - // File is not static - return next() - } - }) - }) - } } -} \ No newline at end of file +}