|
|
@ -4,7 +4,6 @@ const sharp = require('sharp')
|
|
|
|
const mkdirp = require('mkdirp')
|
|
|
|
const mkdirp = require('mkdirp')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const slugify = require('slugify')
|
|
|
|
const slugify = require('slugify')
|
|
|
|
const assign = require('assign-deep')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const configStore = require('./../config.js')
|
|
|
|
const configStore = require('./../config.js')
|
|
|
|
|
|
|
|
|
|
|
@ -21,8 +20,8 @@ const configStore = require('./../config.js')
|
|
|
|
|
|
|
|
|
|
|
|
class Media {
|
|
|
|
class Media {
|
|
|
|
|
|
|
|
|
|
|
|
constructor(dirPath = NULL) {
|
|
|
|
constructor(path = NULL) {
|
|
|
|
this._path = dirPath
|
|
|
|
this._path = path
|
|
|
|
this._DIR_ASSETS = '/assets/'
|
|
|
|
this._DIR_ASSETS = '/assets/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -43,7 +42,11 @@ class Media {
|
|
|
|
const filename = slugify(path.basename(src, extension))
|
|
|
|
const filename = slugify(path.basename(src, extension))
|
|
|
|
|
|
|
|
|
|
|
|
// check for images in path
|
|
|
|
// check for images in path
|
|
|
|
sourcePath = this._getSourcePath(src)
|
|
|
|
if (this._path && fs.existsSync(configStore.get('source') + this._path + '/' + src)) {
|
|
|
|
|
|
|
|
sourcePath = configStore.get('source') + this._path + '/' + src
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sourcePath = configStore.get('source') + '/' + src
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getting sharp
|
|
|
|
// getting sharp
|
|
|
|
const process = sharp(sourcePath)
|
|
|
|
const process = sharp(sourcePath)
|
|
|
@ -88,30 +91,6 @@ class Media {
|
|
|
|
return this._reduce(results)
|
|
|
|
return this._reduce(results)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* get source path
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {String} src
|
|
|
|
|
|
|
|
* @return {String}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
_getSourcePath(src) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sourcePath = configStore.get('source') + '/' + src
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(this._path)) {
|
|
|
|
|
|
|
|
if (fs.existsSync(configStore.get('source') + this._path[0] + '/' + src)) {
|
|
|
|
|
|
|
|
sourcePath = configStore.get('source') + this._path[0] + '/' + src
|
|
|
|
|
|
|
|
} else if (fs.existsSync(configStore.get('source') + this._path[1] + '/' + src)) {
|
|
|
|
|
|
|
|
sourcePath = configStore.get('source') + this._path[1] + '/' + src
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (this._path && fs.existsSync(configStore.get('source') + this._path + '/' + src)) {
|
|
|
|
|
|
|
|
sourcePath = configStore.get('source') + this._path + '/' + src
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sourcePath
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* if only full is in results, reduce object to string
|
|
|
|
* if only full is in results, reduce object to string
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -163,34 +142,16 @@ class Media {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
_writeFile(file, process, options) {
|
|
|
|
_writeFile(file, process, options) {
|
|
|
|
|
|
|
|
|
|
|
|
// if sizes have height or width with no optional parameters then check for merge of options
|
|
|
|
// resize without options and with options
|
|
|
|
if (file.sizes &&
|
|
|
|
if (file.sizes && Object.getOwnPropertyNames(options).length === 0 && Object.getOwnPropertyNames(file.sizes).length > 0) {
|
|
|
|
(((!file.sizes.height || !file.sizes.width) && Object.getOwnPropertyNames(file.sizes).length === 1) ||
|
|
|
|
|
|
|
|
(file.sizes.height && file.sizes.width && Object.getOwnPropertyNames(file.sizes).length === 2))) {
|
|
|
|
|
|
|
|
process.resize(this._mergeOptions(file.sizes, options))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if already options in sizes ignore options
|
|
|
|
|
|
|
|
} else if (file.sizes) {
|
|
|
|
|
|
|
|
process.resize(file.sizes)
|
|
|
|
process.resize(file.sizes)
|
|
|
|
|
|
|
|
} else if (file.sizes && Object.getOwnPropertyNames(options).length > 0 && Object.getOwnPropertyNames(file.sizes).length > 0) {
|
|
|
|
|
|
|
|
process.resize(file.sizes, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.toFile(configStore.get('destination') + file.path)
|
|
|
|
process.toFile(configStore.get('destination') + file.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* if options are exists merge them with sizes
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {object} sizes
|
|
|
|
|
|
|
|
* @param {Object} options
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
_mergeOptions(sizes, options) {
|
|
|
|
|
|
|
|
if (Object.getOwnPropertyNames(options).length > 0) {
|
|
|
|
|
|
|
|
sizes = assign(options, sizes)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sizes
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* generate destination path from hash of file
|
|
|
|
* generate destination path from hash of file
|
|
|
|
*
|
|
|
|
*
|
|
|
|