added imagemagic and created media processing wrapper modules

This commit is contained in:
bill bittner 2018-07-27 09:58:00 -07:00
parent aa1784417e
commit 41ccf06370
6 changed files with 79 additions and 35 deletions

5
package-lock.json generated
View file

@ -4395,6 +4395,11 @@
"resolved": "https://registry.npmjs.org/image-size/-/image-size-0.6.3.tgz", "resolved": "https://registry.npmjs.org/image-size/-/image-size-0.6.3.tgz",
"integrity": "sha512-47xSUiQioGaB96nqtp5/q55m0aBQSQdyIloMOc/x+QVTDZLNmXE892IIDrJ0hM1A5vcNUDD5tDffkSP5lCaIIA==" "integrity": "sha512-47xSUiQioGaB96nqtp5/q55m0aBQSQdyIloMOc/x+QVTDZLNmXE892IIDrJ0hM1A5vcNUDD5tDffkSP5lCaIIA=="
}, },
"imagemagick": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/imagemagick/-/imagemagick-0.1.3.tgz",
"integrity": "sha1-dIPOoJO02fLi85aFetyIIbU3xWo="
},
"import-lazy": { "import-lazy": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz", "resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz",

View file

@ -40,6 +40,7 @@
"get-video-dimensions": "^1.0.0", "get-video-dimensions": "^1.0.0",
"helmet": "^3.8.1", "helmet": "^3.8.1",
"image-size": "^0.6.3", "image-size": "^0.6.3",
"imagemagick": "^0.1.3",
"module-alias": "^2.0.6", "module-alias": "^2.0.6",
"mysql2": "^1.3.5", "mysql2": "^1.3.5",
"passport": "^0.4.0", "passport": "^0.4.0",

View file

@ -1,36 +1,4 @@
const logger = require('winston'); const getMediaDimensions = require('../../utils/getMediaDimensions.js');
const sizeOfImage = require('image-size');
const sizeOfVideo = require('get-video-dimensions');
async function getFileDimensions (fileType, filePath) {
let height = 0;
let width = 0;
switch (fileType) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
case 'image/gif':
logger.debug('creating File data for an image');
const imageDimensions = sizeOfImage(filePath);
height = imageDimensions.height;
width = imageDimensions.width;
break;
case 'video/mp4':
logger.debug('creating File data for a video');
const videoDimensions = await sizeOfVideo(filePath);
logger.debug('video dimensions', videoDimensions);
height = videoDimensions.height;
width = videoDimensions.width;
break;
default:
logger.error('unable to create File data for unspported file type:', fileType);
break;
}
return {
height,
width,
};
}
async function createFileRecordDataAfterGet (resolveResult, getResult) { async function createFileRecordDataAfterGet (resolveResult, getResult) {
const { const {
@ -48,7 +16,7 @@ async function createFileRecordDataAfterGet (resolveResult, getResult) {
const { const {
height: fileHeight, height: fileHeight,
width: fileWidth, width: fileWidth,
} = await getFileDimensions(fileType, filePath); } = await getMediaDimensions(fileType, filePath);
return { return {
name, name,
@ -77,7 +45,7 @@ async function createFileRecordDataAfterPublish (fileName, fileType, publishPara
const { const {
height: fileHeight, height: fileHeight,
width: fileWidth, width: fileWidth,
} = await getFileDimensions(fileType, filePath); } = await getMediaDimensions(fileType, filePath);
return { return {
name, name,

View file

@ -0,0 +1,30 @@
const logger = require('winston');
const { getImageHeightAndWidth } = require('./imageProcessing');
const { getVideoHeightAndWidth } = require('./utils/videoProcessing');
async function getMediaDimensions (fileType, filePath) {
let height = 0;
let width = 0;
switch (fileType) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
case 'image/gif':
logger.debug('creating File data for an image');
[ height, width ] = await getImageHeightAndWidth(filePath);
break;
case 'video/mp4':
logger.debug('creating File data for a video');
[ height, width ] = await getVideoHeightAndWidth(filePath);
break;
default:
logger.error('unable to create File data for unspported file type:', fileType);
break;
}
return {
height,
width,
};
}
module.exports = getMediaDimensions;

View file

@ -0,0 +1,29 @@
const im = require('imagemagick');
const getImageMetadata = (filePath) => {
return im.readMetadata(filePath, (err, metadata) => {
if (err) throw err;
return metadata;
});
};
const getImageDetails = (filePath) => {
return im.identify(filePath, (err, details) => {
if (err) throw err;
return details;
});
};
const getImageHeightAndWidth = (filePath) => {
return im.identify(filePath, (err, details) => {
if (err) throw err;
const { height, width } = details;
return [height, width];
});
};
module.exports = {
getImageMetadata,
getImageDetails,
getImageHeightAndWidth,
};

View file

@ -0,0 +1,11 @@
const getVideoDimensions = require('get-video-dimensions');
async function getVideoHeightAndWidth (filePath) {
const videoDimensions = await getVideoDimensions(filePath);
const { height, width } = videoDimensions;
return [ height, width ];
}
module.exports = {
getVideoHeightAndWidth,
};