2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-06-22 01:36:08 +02:00
|
|
|
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 = {
|
2017-07-04 03:27:35 +02:00
|
|
|
publish (publishParams, fileName, fileType) {
|
2017-08-03 19:44:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-16 20:00:17 +02:00
|
|
|
let publishResults = {};
|
2017-07-04 09:02:47 +02:00
|
|
|
// 1. make sure the name is available
|
2017-08-16 20:00:17 +02:00
|
|
|
publishHelpers.checkNameAvailability(publishParams.name)
|
|
|
|
// 2. publish the file
|
2017-07-04 09:02:47 +02:00
|
|
|
.then(result => {
|
|
|
|
if (result === true) {
|
2017-08-16 20:00:17 +02:00
|
|
|
return lbryApi.publishClaim(publishParams);
|
2017-07-04 09:02:47 +02:00
|
|
|
} else {
|
2017-08-16 20:00:17 +02:00
|
|
|
return new Error('That name has already been claimed by spee.ch. Please choose a new claim name.');
|
2017-07-04 09:02:47 +02:00
|
|
|
}
|
|
|
|
})
|
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(result => {
|
|
|
|
let fileRecord;
|
|
|
|
let upsertCriteria;
|
|
|
|
publishResults = result;
|
|
|
|
logger.info(`Successfully published ${fileName}`, publishResults);
|
|
|
|
fileRecord = {
|
|
|
|
name : publishParams.name,
|
|
|
|
claimId : publishResults.claim_id,
|
|
|
|
address : publishParams.claim_address,
|
|
|
|
outpoint: `${publishResults.txid}:${publishResults.nout}`,
|
|
|
|
height : 0,
|
|
|
|
fileName,
|
|
|
|
filePath: publishParams.file_path,
|
|
|
|
fileType,
|
|
|
|
nsfw : publishParams.metadata.nsfw,
|
|
|
|
};
|
|
|
|
upsertCriteria = {
|
|
|
|
name : publishParams.name,
|
|
|
|
claimId: publishResults.claim_id,
|
|
|
|
};
|
|
|
|
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, fileRecord, upsertCriteria, 'Claim')]);
|
|
|
|
})
|
|
|
|
.then((fileRecordResults, claimRecordResults) => {
|
|
|
|
logger.debug('File and Claim records successfully created');
|
|
|
|
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
|
|
|
|
})
|
2017-07-04 09:02:47 +02:00
|
|
|
.catch(error => {
|
2017-08-16 20:00:17 +02:00
|
|
|
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
|
2017-07-04 09:02:47 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|