refactored File and Claim data creating functions
This commit is contained in:
parent
0b52a819c0
commit
62137f3ca9
8 changed files with 129 additions and 50 deletions
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -4377,6 +4377,11 @@
|
|||
"integrity": "sha1-SMptcvbGo68Aqa1K5odr44ieKwk=",
|
||||
"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": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz",
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
"express": "^4.15.2",
|
||||
"express-handlebars": "^3.0.0",
|
||||
"helmet": "^3.8.1",
|
||||
"image-size": "^0.6.3",
|
||||
"module-alias": "^2.0.6",
|
||||
"mysql2": "^1.3.5",
|
||||
"passport": "^0.4.0",
|
||||
|
|
|
@ -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;
|
|
@ -1,5 +1,5 @@
|
|||
const { getClaim } = require('../../../../lbrynet');
|
||||
const createFileData = require('./createFileData.js');
|
||||
const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js');
|
||||
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
||||
const db = require('../../../../models');
|
||||
|
||||
|
@ -27,7 +27,7 @@ const claimGet = ({ ip, originalUrl, params }, res) => {
|
|||
getResult = result;
|
||||
})
|
||||
.then(() => {
|
||||
const fileData = createFileData(resolveResult, getResult);
|
||||
const fileData = createFileRecordDataAfterGet(resolveResult, getResult);
|
||||
const upsertCriteria = { name, claimId};
|
||||
return db.upsert(db.File, fileData, upsertCriteria, 'File')
|
||||
})
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
const logger = require('winston');
|
||||
const db = require('../../../../models');
|
||||
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 publish = (publishParams, fileName, fileType) => {
|
||||
|
@ -35,51 +37,32 @@ const publish = (publishParams, fileName, fileType) => {
|
|||
logger.debug(`certificateId: ${certificateId}`);
|
||||
})
|
||||
.then(() => {
|
||||
const { name, claim_id: claimId } = publishParams;
|
||||
// create the File record
|
||||
const fileRecord = {
|
||||
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,
|
||||
};
|
||||
const fileRecord = createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults);
|
||||
// create the Claim record
|
||||
const claimRecord = {
|
||||
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 claimRecord = createClaimRecordDataAfterPublish(certificateId, channelName, fileName, fileType, publishParams, publishResults);
|
||||
const upsertCriteria = {
|
||||
name : publishParams.name,
|
||||
claimId: publishResults.claim_id,
|
||||
name,
|
||||
claimId,
|
||||
};
|
||||
// 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]) => {
|
||||
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(() => {
|
||||
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 => {
|
||||
logger.error('PUBLISH ERROR', error);
|
||||
|
|
|
@ -42,7 +42,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
|
|||
);
|
||||
|
||||
File.associate = db => {
|
||||
File.hasMany(db.Request);
|
||||
File.hasOne(db.Claim);
|
||||
};
|
||||
|
||||
|
|
37
server/models/utils/createClaimRecordData.js
Normal file
37
server/models/utils/createClaimRecordData.js
Normal 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,
|
||||
};
|
66
server/models/utils/createFileRecordData.js
Normal file
66
server/models/utils/createFileRecordData.js
Normal 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,
|
||||
};
|
Loading…
Reference in a new issue