added async await for dimension processing
This commit is contained in:
parent
6da08a5f47
commit
aa1784417e
2 changed files with 27 additions and 17 deletions
|
@ -37,16 +37,18 @@ const publish = (publishParams, fileName, fileType) => {
|
||||||
logger.debug(`certificateId: ${certificateId}`);
|
logger.debug(`certificateId: ${certificateId}`);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
return Promise.all([
|
||||||
|
createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults),
|
||||||
|
createClaimRecordDataAfterPublish(certificateId, channelName, fileName, fileType, publishParams, publishResults),
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
.then(([fileRecord, claimRecord]) => {
|
||||||
|
// upsert the records
|
||||||
const {name, claim_id: claimId} = publishParams;
|
const {name, claim_id: claimId} = publishParams;
|
||||||
// create the File record
|
|
||||||
const fileRecord = createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults);
|
|
||||||
// create the Claim record
|
|
||||||
const claimRecord = createClaimRecordDataAfterPublish(certificateId, channelName, fileName, fileType, publishParams, publishResults);
|
|
||||||
const upsertCriteria = {
|
const upsertCriteria = {
|
||||||
name,
|
name,
|
||||||
claimId,
|
claimId,
|
||||||
};
|
};
|
||||||
// upsert the records
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
db.upsert(db.File, fileRecord, upsertCriteria, 'File'),
|
db.upsert(db.File, fileRecord, upsertCriteria, 'File'),
|
||||||
db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim'),
|
db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim'),
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const sizeOf = require('image-size');
|
const sizeOfImage = require('image-size');
|
||||||
|
const sizeOfVideo = require('get-video-dimensions');
|
||||||
|
|
||||||
const getFileDimensions = (fileType, filePath) => {
|
async function getFileDimensions (fileType, filePath) {
|
||||||
let height = 0;
|
let height = 0;
|
||||||
let width = 0;
|
let width = 0;
|
||||||
switch (fileType) {
|
switch (fileType) {
|
||||||
|
@ -10,12 +11,16 @@ const getFileDimensions = (fileType, filePath) => {
|
||||||
case 'image/png':
|
case 'image/png':
|
||||||
case 'image/gif':
|
case 'image/gif':
|
||||||
logger.debug('creating File data for an image');
|
logger.debug('creating File data for an image');
|
||||||
const dimensions = sizeOf(filePath);
|
const imageDimensions = sizeOfImage(filePath);
|
||||||
height = dimensions.height;
|
height = imageDimensions.height;
|
||||||
width = dimensions.width;
|
width = imageDimensions.width;
|
||||||
break;
|
break;
|
||||||
case 'video/mp4':
|
case 'video/mp4':
|
||||||
logger.debug('creating File data for a video');
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
logger.error('unable to create File data for unspported file type:', fileType);
|
logger.error('unable to create File data for unspported file type:', fileType);
|
||||||
|
@ -25,9 +30,9 @@ const getFileDimensions = (fileType, filePath) => {
|
||||||
height,
|
height,
|
||||||
width,
|
width,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
const createFileRecordDataAfterGet = (resolveResult, getResult) => {
|
async function createFileRecordDataAfterGet (resolveResult, getResult) {
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
claimId,
|
claimId,
|
||||||
|
@ -43,7 +48,7 @@ const createFileRecordDataAfterGet = (resolveResult, getResult) => {
|
||||||
const {
|
const {
|
||||||
height: fileHeight,
|
height: fileHeight,
|
||||||
width: fileWidth,
|
width: fileWidth,
|
||||||
} = getFileDimensions(fileType, filePath);
|
} = await getFileDimensions(fileType, filePath);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
|
@ -57,7 +62,7 @@ const createFileRecordDataAfterGet = (resolveResult, getResult) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const createFileRecordDataAfterPublish = (fileName, fileType, publishParams, publishResults) => {
|
async function createFileRecordDataAfterPublish (fileName, fileType, publishParams, publishResults) {
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
file_path: filePath,
|
file_path: filePath,
|
||||||
|
@ -69,7 +74,10 @@ const createFileRecordDataAfterPublish = (fileName, fileType, publishParams, pub
|
||||||
nout,
|
nout,
|
||||||
} = publishResults;
|
} = publishResults;
|
||||||
|
|
||||||
const { height: fileHeight, width: fileWidth } = getFileDimensions(fileType, filePath);
|
const {
|
||||||
|
height: fileHeight,
|
||||||
|
width: fileWidth,
|
||||||
|
} = await getFileDimensions(fileType, filePath);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
|
@ -81,7 +89,7 @@ const createFileRecordDataAfterPublish = (fileName, fileType, publishParams, pub
|
||||||
filePath,
|
filePath,
|
||||||
fileType,
|
fileType,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createFileRecordDataAfterGet,
|
createFileRecordDataAfterGet,
|
||||||
|
|
Loading…
Add table
Reference in a new issue