spee.ch/server/models/utils/createFileRecordData.js
2018-07-26 17:57:26 -07:00

89 lines
1.7 KiB
JavaScript

const logger = require('winston');
const sizeOf = require('image-size');
const 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 dimensions = sizeOf(filePath);
height = dimensions.height;
width = dimensions.width;
break;
case 'video/mp4':
logger.debug('creating File data for a video');
break;
default:
logger.error('unable to create File data for unspported file type:', fileType);
break;
}
return {
height,
width,
};
};
const createFileRecordDataAfterGet = (resolveResult, getResult) => {
const {
name,
claimId,
outpoint,
contentType: fileType,
} = resolveResult;
const {
file_name: fileName,
download_path: filePath,
} = getResult;
const {
height: fileHeight,
width: fileWidth,
} = getFileDimensions(fileType, filePath);
return {
name,
claimId,
outpoint,
fileHeight,
fileWidth,
fileName,
filePath,
fileType,
};
};
const createFileRecordDataAfterPublish = (fileName, fileType, publishParams, publishResults) => {
const {
name,
file_path: filePath,
} = publishParams;
const {
claim_id: claimId,
txid,
nout,
} = publishResults;
const { height: fileHeight, width: fileWidth } = getFileDimensions(fileType, filePath);
return {
name,
claimId,
outpoint: `${txid}:${nout}`,
fileHeight,
fileWidth,
fileName,
filePath,
fileType,
};
};
module.exports = {
createFileRecordDataAfterGet,
createFileRecordDataAfterPublish,
};