parent
5c29b92a6e
commit
c42119dee5
After Width: | Height: | Size: 28 KiB |
@ -1,15 +0,0 @@
|
||||
---
|
||||
title: "Cliche Vegan Messenger"
|
||||
date_published: "2022-01-10 10:00"
|
||||
view: "post.njk"
|
||||
tags:
|
||||
- tumeric
|
||||
- heard
|
||||
- bag
|
||||
media:
|
||||
teaser: 'jep.jpg'
|
||||
meta:
|
||||
description: "DSA yes plz hot chicken green juice"
|
||||
robots: "index, follow"
|
||||
---
|
||||
## TEST
|
@ -1,7 +0,0 @@
|
||||
---
|
||||
title: "Blog"
|
||||
view: "blog.njk"
|
||||
meta:
|
||||
description: "DSA yes plz hot chicken green juice"
|
||||
robots: "index, follow"
|
||||
---
|
@ -0,0 +1,36 @@
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class ConfigStore {
|
||||
|
||||
constructor() {
|
||||
if (!ConfigStore.instance) {
|
||||
ConfigStore.instance = this;
|
||||
this._data = {}
|
||||
}
|
||||
|
||||
return ConfigStore.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
set(key, value) {
|
||||
this._data[key] = value
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
get(key) {
|
||||
return this._data[key]
|
||||
}
|
||||
}
|
||||
|
||||
// create instance
|
||||
const instance = new ConfigStore();
|
||||
|
||||
export default instance
|
@ -0,0 +1,101 @@
|
||||
import path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
import sharp from 'sharp'
|
||||
import mkdirp from 'mkdirp'
|
||||
import crypto from 'crypto'
|
||||
import slugify from 'slugify'
|
||||
|
||||
import configStore from './config.js'
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class Media {
|
||||
|
||||
constructor() {
|
||||
this._DIR_ASSETS = '/assets/'
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} srcPath
|
||||
* @param {object} sizes
|
||||
* @param {Object} [options={}]
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
async resize(src, sizes, options = {}) {
|
||||
|
||||
this._extension = path.extname(src)
|
||||
this._filename = slugify(path.basename(src, this._extension))
|
||||
|
||||
this._process = await sharp(configStore.get('source') + '/' + src)
|
||||
|
||||
// resize without options and with options
|
||||
if (Object.getOwnPropertyNames(options).length === 0) {
|
||||
await this._process
|
||||
.resize(sizes)
|
||||
} else {
|
||||
this._process
|
||||
.resize(sizes, options)
|
||||
}
|
||||
|
||||
// optimize
|
||||
this._optimize()
|
||||
|
||||
const fileBuffer = await this._process
|
||||
.toBuffer()
|
||||
|
||||
const relativeDestinationPath = this._DIR_ASSETS + this._resolveRelativeDestinationPath(fileBuffer)
|
||||
|
||||
// create directories and write file
|
||||
mkdirp.sync(configStore.get('destination') + relativeDestinationPath)
|
||||
fs.writeFileSync(configStore.get('destination') + relativeDestinationPath + '/' + this._filename + this._extension, fileBuffer)
|
||||
|
||||
return relativeDestinationPath + '/' + this._filename + this._extension
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO much nicer to add a hook system so behavior can be change
|
||||
*
|
||||
*
|
||||
* @param {string} extension
|
||||
*
|
||||
*/
|
||||
_optimize() {
|
||||
if (this._extension === '.gif') {
|
||||
this._process
|
||||
.gif({
|
||||
reoptimise: true
|
||||
})
|
||||
} else {
|
||||
|
||||
// change extension
|
||||
this._extension = '.webp'
|
||||
this._process
|
||||
|
||||
.webp({
|
||||
lossless: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* resolve path to write file, hash will be get from fileBuffer and
|
||||
*
|
||||
*
|
||||
* @param {object} fileBuffer
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
_resolveRelativeDestinationPath(fileBuffer) {
|
||||
const hash = crypto.createHash('sha1')
|
||||
hash.update(fileBuffer)
|
||||
|
||||
return hash.digest('hex').match(new RegExp('.{1,8}', 'g')).join('/')
|
||||
}
|
||||
}
|
||||
|
||||
export default Media
|
Loading…
Reference in new issue