added imagemagic and created media processing wrapper modules
This commit is contained in:
parent
aa1784417e
commit
41ccf06370
6 changed files with 79 additions and 35 deletions
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -4395,6 +4395,11 @@
|
|||
"resolved": "https://registry.npmjs.org/image-size/-/image-size-0.6.3.tgz",
|
||||
"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": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz",
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
"get-video-dimensions": "^1.0.0",
|
||||
"helmet": "^3.8.1",
|
||||
"image-size": "^0.6.3",
|
||||
"imagemagick": "^0.1.3",
|
||||
"module-alias": "^2.0.6",
|
||||
"mysql2": "^1.3.5",
|
||||
"passport": "^0.4.0",
|
||||
|
|
|
@ -1,36 +1,4 @@
|
|||
const logger = require('winston');
|
||||
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,
|
||||
};
|
||||
}
|
||||
const getMediaDimensions = require('../../utils/getMediaDimensions.js');
|
||||
|
||||
async function createFileRecordDataAfterGet (resolveResult, getResult) {
|
||||
const {
|
||||
|
@ -48,7 +16,7 @@ async function createFileRecordDataAfterGet (resolveResult, getResult) {
|
|||
const {
|
||||
height: fileHeight,
|
||||
width: fileWidth,
|
||||
} = await getFileDimensions(fileType, filePath);
|
||||
} = await getMediaDimensions(fileType, filePath);
|
||||
|
||||
return {
|
||||
name,
|
||||
|
@ -77,7 +45,7 @@ async function createFileRecordDataAfterPublish (fileName, fileType, publishPara
|
|||
const {
|
||||
height: fileHeight,
|
||||
width: fileWidth,
|
||||
} = await getFileDimensions(fileType, filePath);
|
||||
} = await getMediaDimensions(fileType, filePath);
|
||||
|
||||
return {
|
||||
name,
|
||||
|
|
30
server/utils/getMediaDimensions.js
Normal file
30
server/utils/getMediaDimensions.js
Normal 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;
|
29
server/utils/imageProcessing.js
Normal file
29
server/utils/imageProcessing.js
Normal 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,
|
||||
};
|
11
server/utils/videoProcessing.js
Normal file
11
server/utils/videoProcessing.js
Normal 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,
|
||||
};
|
Loading…
Reference in a new issue