spee.ch/controllers/publishController.js

76 lines
3 KiB
JavaScript
Raw Normal View History

2017-06-20 01:15:28 +02:00
const logger = require('winston');
const db = require('../models');
2017-08-03 02:13:02 +02:00
const lbryApi = require('../helpers/lbryApi.js');
const publishHelpers = require('../helpers/publishHelpers.js');
2017-06-03 05:56:33 +02:00
module.exports = {
publish (publishParams, fileName, fileType) {
return new Promise((resolve, reject) => {
2017-08-16 20:00:17 +02:00
let publishResults = {};
// 1. make sure the name is available
2017-09-19 21:54:23 +02:00
publishHelpers.checkClaimNameAvailability(publishParams.name)
2017-08-16 20:00:17 +02:00
// 2. publish the file
.then(result => {
if (result === true) {
2017-08-16 20:00:17 +02:00
return lbryApi.publishClaim(publishParams);
} else {
2017-09-25 20:55:56 +02:00
return new Error('That name is already in use by spee.ch.');
}
})
2017-08-16 20:00:17 +02:00
// 3. upsert File record (update is in case the claim has been published before by this daemon)
.then(tx => {
logger.info(`Successfully published ${fileName}`, tx);
publishResults = tx;
2017-09-26 06:03:43 +02:00
return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
})
.then(user => {
logger.debug('found user', user.datavalues);
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,
2017-08-16 20:00:17 +02:00
fileName,
filePath : publishParams.file_path,
2017-08-16 20:00:17 +02:00
fileType,
nsfw : publishParams.metadata.nsfw,
2017-08-16 20:00:17 +02:00
};
const claimRecord = {
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,
contentType : fileType,
nsfw : publishParams.metadata.nsfw,
certificateId: user.channelClaimId,
amount : publishParams.bid,
};
const upsertCriteria = {
2017-08-16 20:00:17 +02:00
name : publishParams.name,
claimId: publishResults.claim_id,
};
// create the records
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim')]);
2017-08-16 20:00:17 +02:00
})
2017-09-25 20:55:56 +02:00
.then(([file, claim]) => {
2017-08-16 20:00:17 +02:00
logger.debug('File and Claim records successfully created');
return Promise.all([file.setClaim(claim), claim.setFile(file)]);
})
.then(() => {
logger.debug('File and Claim records successfully associated');
2017-08-16 20:00:17 +02:00
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
})
.catch(error => {
2017-08-16 20:00:17 +02:00
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
reject(error);
});
2017-07-03 23:48:35 +02:00
});
},
};