refactored File and Claim data creating functions

This commit is contained in:
bill bittner 2018-07-26 13:48:53 -07:00
parent 0b52a819c0
commit 62137f3ca9
8 changed files with 129 additions and 50 deletions

5
package-lock.json generated
View file

@ -4377,6 +4377,11 @@
"integrity": "sha1-SMptcvbGo68Aqa1K5odr44ieKwk=", "integrity": "sha1-SMptcvbGo68Aqa1K5odr44ieKwk=",
"dev": true "dev": true
}, },
"image-size": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/image-size/-/image-size-0.6.3.tgz",
"integrity": "sha512-47xSUiQioGaB96nqtp5/q55m0aBQSQdyIloMOc/x+QVTDZLNmXE892IIDrJ0hM1A5vcNUDD5tDffkSP5lCaIIA=="
},
"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

@ -38,6 +38,7 @@
"express": "^4.15.2", "express": "^4.15.2",
"express-handlebars": "^3.0.0", "express-handlebars": "^3.0.0",
"helmet": "^3.8.1", "helmet": "^3.8.1",
"image-size": "^0.6.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,12 +0,0 @@
const createFileData = ({ name, claimId, outpoint, contentType: fileType }, { file_name: fileName, download_path: filePath }) => {
return {
name,
claimId,
outpoint,
fileName,
filePath,
fileType,
};
};
module.exports = createFileData;

View file

@ -1,5 +1,5 @@
const { getClaim } = require('../../../../lbrynet'); const { getClaim } = require('../../../../lbrynet');
const createFileData = require('./createFileData.js'); const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js');
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const db = require('../../../../models'); const db = require('../../../../models');
@ -27,7 +27,7 @@ const claimGet = ({ ip, originalUrl, params }, res) => {
getResult = result; getResult = result;
}) })
.then(() => { .then(() => {
const fileData = createFileData(resolveResult, getResult); const fileData = createFileRecordDataAfterGet(resolveResult, getResult);
const upsertCriteria = { name, claimId}; const upsertCriteria = { name, claimId};
return db.upsert(db.File, fileData, upsertCriteria, 'File') return db.upsert(db.File, fileData, upsertCriteria, 'File')
}) })

View file

@ -1,6 +1,8 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../../../../models');
const { publishClaim } = require('../../../../lbrynet'); const { publishClaim } = require('../../../../lbrynet');
const db = require('../../../../models');
const { createFileRecordDataAfterPublish } = require('../../../../models/utils/createFileRecordData.js');
const { createClaimRecordDataAfterPublish } = require('../../../../models/utils/createClaimRecordData.js');
const deleteFile = require('./deleteFile.js'); const deleteFile = require('./deleteFile.js');
const publish = (publishParams, fileName, fileType) => { const publish = (publishParams, fileName, fileType) => {
@ -35,51 +37,32 @@ const publish = (publishParams, fileName, fileType) => {
logger.debug(`certificateId: ${certificateId}`); logger.debug(`certificateId: ${certificateId}`);
}) })
.then(() => { .then(() => {
const { name, claim_id: claimId } = publishParams;
// create the File record // create the File record
const fileRecord = { const fileRecord = createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults);
name : publishParams.name,
claimId : publishResults.claim_id,
title : publishParams.metadata.title,
description: publishParams.metadata.description,
address : publishParams.claim_address,
outpoint : `${publishResults.txid}:${publishResults.nout}`,
height : 0,
fileName,
filePath : publishParams.file_path,
fileType,
nsfw : publishParams.metadata.nsfw,
};
// create the Claim record // create the Claim record
const claimRecord = { const claimRecord = createClaimRecordDataAfterPublish(certificateId, channelName, fileName, fileType, publishParams, publishResults);
name : publishParams.name,
claimId : publishResults.claim_id,
title : publishParams.metadata.title,
description: publishParams.metadata.description,
address : publishParams.claim_address,
thumbnail : publishParams.metadata.thumbnail,
outpoint : `${publishResults.txid}:${publishResults.nout}`,
height : 0,
contentType: fileType,
nsfw : publishParams.metadata.nsfw,
amount : publishParams.bid,
certificateId,
channelName,
};
// upsert criteria
const upsertCriteria = { const upsertCriteria = {
name : publishParams.name, name,
claimId: publishResults.claim_id, claimId,
}; };
// upsert the records // upsert the records
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim')]); return Promise.all([
db.upsert(db.File, fileRecord, upsertCriteria, 'File'),
db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim'),
]);
}) })
.then(([file, claim]) => { .then(([file, claim]) => {
logger.debug('File and Claim records successfully created'); logger.debug('File and Claim records successfully created');
return Promise.all([file.setClaim(claim), claim.setFile(file)]); return Promise.all([
file.setClaim(claim),
claim.setFile(file),
]);
}) })
.then(() => { .then(() => {
logger.debug('File and Claim records successfully associated'); logger.debug('File and Claim records successfully associated');
resolve(publishResults); // resolve the promise with the result from lbryApi publishClaim; // resolve the promise with the result from lbryApi publishClaim;
resolve(publishResults);
}) })
.catch(error => { .catch(error => {
logger.error('PUBLISH ERROR', error); logger.error('PUBLISH ERROR', error);

View file

@ -42,7 +42,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
); );
File.associate = db => { File.associate = db => {
File.hasMany(db.Request);
File.hasOne(db.Claim); File.hasOne(db.Claim);
}; };

View file

@ -0,0 +1,37 @@
const createClaimRecordDataAfterPublish = (certificateId, channelName, fileType, publishParams, publishResults) => {
const {
name,
metadata: {
title,
description,
thumbnail,
nsfw,
},
claim_address: address,
bid: amount,
} = publishParams;
const {
claim_id: claimId,
txid,
nout,
} = publishResults;
return {
name,
claimId,
title,
description,
address,
thumbnail,
outpoint : `${txid}:${nout}`,
height : 0,
contentType: fileType,
nsfw,
amount,
certificateId,
channelName,
};
};
module.exports = {
createClaimRecordDataAfterPublish,
};

View file

@ -0,0 +1,66 @@
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, claim_id: claimId, file_path: filePath } = publishParams;
const { 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,
};