mirror of
https://github.com/frappe/books.git
synced 2024-12-24 11:55:46 +00:00
Cleanup styles and path name
This commit is contained in:
parent
7f3fb7713a
commit
8765c60ef3
@ -1,6 +1,7 @@
|
|||||||
const mkdirp = require('mkdirp');
|
const mkdirp = require('mkdirp');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const getDirName = require('path').dirname;
|
const path = require('path');
|
||||||
|
const getDirName = path.dirname;
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const sharp = require('sharp');
|
const sharp = require('sharp');
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
readFile(filepath) {
|
readFile(filepath) {
|
||||||
return fs.readFileSync(filepath, 'utf-8');
|
return fs.readFileSync(filepath, 'utf-8');
|
||||||
},
|
},
|
||||||
|
|
||||||
getTmpDir() {
|
getTmpDir() {
|
||||||
@ -27,38 +28,45 @@ module.exports = {
|
|||||||
|
|
||||||
thumbnailMiddleware(staticPath) {
|
thumbnailMiddleware(staticPath) {
|
||||||
|
|
||||||
return function(req, res, next){
|
return function (req, res, next) {
|
||||||
|
|
||||||
const file = (req.url).toString().replace(/\?.*/, "");
|
const filename = req.path.split(path.sep).slice(-1)[0]
|
||||||
const dimension = req.query.size || ""
|
const dimension = req.query.size || null
|
||||||
const staticFile = staticPath + file
|
const staticFile = path.join(staticPath, filename)
|
||||||
|
|
||||||
fs.exists(staticFile, (exists) => {
|
fs.exists(staticFile, (exists) => {
|
||||||
if(!exists){
|
if (!exists) {
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.stat(staticFile, (err, stats) => {
|
fs.stat(staticFile, (err, stats) => {
|
||||||
if(err){
|
if (err) {
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if url is static file
|
//Check if url is static file
|
||||||
if(stats.isFile()){
|
if (stats.isFile()) {
|
||||||
|
|
||||||
//Check if url has dimension parameters
|
// Check if url has dimension parameters
|
||||||
if(dimension != ""){
|
if (dimension) {
|
||||||
const width = parseInt(dimension.split('x')[0])
|
let [width, height] = dimension.split('x');
|
||||||
const height = parseInt(dimension.split('x')[1])
|
width = +width;
|
||||||
const destination = staticPath + '/.thumbnails/' + width + 'x' + height + file.replace('/', '')
|
height = +height;
|
||||||
|
const thumbnailPath = path.join(staticPath, 'thumbnails');
|
||||||
|
const destination = path.join(thumbnailPath, `${width}x${height}-${filename}`)
|
||||||
|
|
||||||
//Check if thumbnail already present
|
// create thumbnails folder if not exists
|
||||||
|
if (!fs.existsSync(thumbnailPath)) {
|
||||||
|
fs.mkdirSync(thumbnailPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if thumbnail already present
|
||||||
fs.existsSync(destination, (exists) => {
|
fs.existsSync(destination, (exists) => {
|
||||||
if(exists)
|
if (exists)
|
||||||
return res.sendFile(destination)
|
return res.sendFile(destination)
|
||||||
})
|
})
|
||||||
|
|
||||||
//Resize image
|
// Resize image
|
||||||
sharp(staticFile)
|
sharp(staticFile)
|
||||||
.resize(width, height)
|
.resize(width, height)
|
||||||
.toFile(destination)
|
.toFile(destination)
|
||||||
@ -73,7 +81,7 @@ module.exports = {
|
|||||||
return res.sendFile(staticFile)
|
return res.sendFile(staticFile)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log('File is not static.')
|
// File is not static
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user