Added resize lib
This commit is contained in:
parent
b3dc18e57d
commit
405d908a09
5 changed files with 725 additions and 35 deletions
21
libs/aws.js
21
libs/aws.js
|
@ -53,7 +53,9 @@ class Aws {
|
|||
const file = await imagemin([newFile], '/tmp', {
|
||||
plugins: [
|
||||
imageminJpegtran(),
|
||||
imageminPngquant({ quality: '65-80' })
|
||||
imageminPngquant({
|
||||
quality: '65-80'
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
|
@ -69,10 +71,10 @@ class Aws {
|
|||
}
|
||||
|
||||
/**
|
||||
* Upload file on s3
|
||||
* @param {Object} params {path: String, filename: String}
|
||||
* @param {Function} callback
|
||||
*/
|
||||
* Upload file on s3
|
||||
* @param {Object} params {path: String, filename: String}
|
||||
* @param {Function} callback
|
||||
*/
|
||||
upload (params, callback) {
|
||||
fs.readFile(params.path, (err, data) => {
|
||||
if (err) {
|
||||
|
@ -91,7 +93,9 @@ class Aws {
|
|||
|
||||
files.forEach((file) => {
|
||||
if (file) {
|
||||
items.push({ Key: file.replace(basePath, '') })
|
||||
items.push({
|
||||
Key: file.replace(basePath, '')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -103,7 +107,10 @@ class Aws {
|
|||
}
|
||||
}, callback)
|
||||
} else {
|
||||
callback(null, { code: 200, res: 'No file deleted' })
|
||||
callback(null, {
|
||||
code: 200,
|
||||
res: 'No file deleted'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
62
libs/resize.js
Normal file
62
libs/resize.js
Normal file
|
@ -0,0 +1,62 @@
|
|||
const fs = require('fs')
|
||||
const resizeImg = require('resize-img')
|
||||
|
||||
class Resize {
|
||||
constructor (width, heigth) {
|
||||
this.size = {
|
||||
width: 128,
|
||||
heigth: 128
|
||||
}
|
||||
|
||||
if (width && heigth) {
|
||||
this.setSize(width, heigth)
|
||||
}
|
||||
}
|
||||
|
||||
_createOutputFilename (input) {
|
||||
return `${input}_${this.size.width}x${this.size.heigth}`
|
||||
}
|
||||
|
||||
_resize (input, output, callback) {
|
||||
fs.readFile(input, (err, data) => {
|
||||
if (err) {
|
||||
callback(err, null)
|
||||
return false
|
||||
}
|
||||
|
||||
resizeImg(data, this.size).then(buf => {
|
||||
fs.writeFile(output, buf, (err) => {
|
||||
if (err) {
|
||||
callback(err, null)
|
||||
return false
|
||||
}
|
||||
callback(null, { input: input, output: output, size: this.size })
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
setSize (width, heigth) {
|
||||
this.size = {
|
||||
width: width,
|
||||
heigth: heigth
|
||||
}
|
||||
}
|
||||
|
||||
createThumbnail (file, callback) {
|
||||
this.setSize(128, 128)
|
||||
this._resize(file, this._createOutputFilename(file), callback)
|
||||
}
|
||||
|
||||
createLargeImage (file, callback) {
|
||||
this.setSize(1600, 1200)
|
||||
this._resize(file, this._createOutputFilename(file), callback)
|
||||
}
|
||||
|
||||
resize (file, width, heigth, callback) {
|
||||
this.setSize(width, heigth)
|
||||
this._resize(file, this._createOutputFilename(file), callback)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Resize
|
Loading…
Add table
Add a link
Reference in a new issue