Gzip images

This commit is contained in:
LordMathis 2019-10-21 21:20:46 +02:00
parent e20d5c9697
commit 130c3c74ac
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
1 changed files with 36 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import config from '../../config/config.json'
import fm from 'front-matter' import fm from 'front-matter'
import moment from 'moment' import moment from 'moment'
import jsonfile from 'jsonfile' import jsonfile from 'jsonfile'
import zlib from 'zlib'
export class Scanner { export class Scanner {
constructor () { constructor () {
@ -38,9 +39,37 @@ export class Scanner {
}) })
} }
processImage (filename) { copyImage (filename) {
const filePath = path.join(process.cwd(), config.contentPath, filename) return new Promise((resolve, reject) => {
fs.copyFileSync(filePath, path.join(process.cwd(), 'public/static', filename)) const inputPath = path.join(process.cwd(), config.contentPath, filename)
const outputPath = path.join(process.cwd(), 'public/static', filename)
fs.copyFile(inputPath, outputPath, (err) => {
if (err) {
reject(err)
} else {
resolve(filename)
}
})
})
}
gzipImage (filename) {
return new Promise((resolve, reject) => {
const inputPath = path.join(process.cwd(), 'public/static', filename)
const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`)
const fileContents = fs.createReadStream(inputPath);
const writeStream = fs.createWriteStream(outputPath);
const zip = zlib.createGzip();
fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => {
if (err) {
reject(err)
}
else {
resolve()
}
})
})
} }
processFile (file, data) { processFile (file, data) {
@ -112,12 +141,15 @@ export class Scanner {
fs.statSync(path.join(process.cwd(), config.contentPath, file)).isFile() fs.statSync(path.join(process.cwd(), config.contentPath, file)).isFile()
) )
) )
const images = filtered.filter( const images = filtered.filter(
(file) => ( (file) => (
path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif' path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif'
) )
) )
images.map(this.processImage) Promise.all(images.map(this.copyImage))
.then((files) => files.map(this.gzipImage))
const posts = filtered.filter( const posts = filtered.filter(
(file) => ( (file) => (
path.extname(file) == '.md' path.extname(file) == '.md'