2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

chore: Remove thumbnail middleware

This commit is contained in:
Faris Ansari 2019-12-07 00:02:55 +05:30
parent 48d12d1a93
commit 859939e763
2 changed files with 2 additions and 67 deletions

View File

@ -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'));

View File

@ -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()
}
})
})
}
}
}
}