diff --git a/package-lock.json b/package-lock.json index 543859b4..38f430f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 85da362c..545741e1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/models/utils/createFileRecordData.js b/server/models/utils/createFileRecordData.js index d74f7242..aa0802cf 100644 --- a/server/models/utils/createFileRecordData.js +++ b/server/models/utils/createFileRecordData.js @@ -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, diff --git a/server/utils/getMediaDimensions.js b/server/utils/getMediaDimensions.js new file mode 100644 index 00000000..ac8d2011 --- /dev/null +++ b/server/utils/getMediaDimensions.js @@ -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; diff --git a/server/utils/imageProcessing.js b/server/utils/imageProcessing.js new file mode 100644 index 00000000..9a93c75e --- /dev/null +++ b/server/utils/imageProcessing.js @@ -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, +}; diff --git a/server/utils/videoProcessing.js b/server/utils/videoProcessing.js new file mode 100644 index 00000000..508f6d10 --- /dev/null +++ b/server/utils/videoProcessing.js @@ -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, +};