diff --git a/server/routes/api/channelAvailability.js b/server/controllers/api/channelAvailability.js similarity index 88% rename from server/routes/api/channelAvailability.js rename to server/controllers/api/channelAvailability.js index 6ea7a76b..33f5f5f3 100644 --- a/server/routes/api/channelAvailability.js +++ b/server/controllers/api/channelAvailability.js @@ -1,4 +1,4 @@ -const { checkChannelAvailability } = require('../../controllers/publishController.js'); +const checkChannelAvailability = require('../utils/checkChannelAvailability.js'); const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); diff --git a/server/routes/api/channelClaims.js b/server/controllers/api/channelClaims.js similarity index 90% rename from server/routes/api/channelClaims.js rename to server/controllers/api/channelClaims.js index 980d3e8e..d9a3683e 100644 --- a/server/routes/api/channelClaims.js +++ b/server/controllers/api/channelClaims.js @@ -1,4 +1,4 @@ -const { getChannelClaims } = require('../../controllers/serveController.js'); +const { getChannelClaims } = require('../utils/serveHelpers.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const NO_CHANNEL = 'NO_CHANNEL'; diff --git a/server/routes/api/channelData.js b/server/controllers/api/channelData.js similarity index 90% rename from server/routes/api/channelData.js rename to server/controllers/api/channelData.js index 8f14cd47..92609412 100644 --- a/server/routes/api/channelData.js +++ b/server/controllers/api/channelData.js @@ -1,4 +1,4 @@ -const { getChannelData } = require('../../controllers/serveController.js'); +const { getChannelData } = require('../utils/serveHelpers.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const NO_CHANNEL = 'NO_CHANNEL'; diff --git a/server/routes/api/channelShortId.js b/server/controllers/api/channelShortId.js similarity index 91% rename from server/routes/api/channelShortId.js rename to server/controllers/api/channelShortId.js index c5575ab6..898456a1 100644 --- a/server/routes/api/channelShortId.js +++ b/server/controllers/api/channelShortId.js @@ -1,5 +1,5 @@ const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models'); +const db = require('../../models/index'); /* diff --git a/server/routes/api/claimAvailability.js b/server/controllers/api/claimAvailability.js similarity index 84% rename from server/routes/api/claimAvailability.js rename to server/controllers/api/claimAvailability.js index 575bd4d4..23a3a21f 100644 --- a/server/routes/api/claimAvailability.js +++ b/server/controllers/api/claimAvailability.js @@ -1,4 +1,4 @@ -const { claimNameIsAvailable } = require('../../controllers/publishController.js'); +const checkClaimAvailability = require('../utils/checkClaimAvailability.js'); const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); @@ -10,7 +10,7 @@ const { handleErrorResponse } = require('../utils/errorHandlers.js'); const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => { const gaStartTime = Date.now(); - claimNameIsAvailable(name) + checkClaimAvailability(name) .then(result => { res.status(200).json(result); sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); diff --git a/server/routes/api/claimData.js b/server/controllers/api/claimData.js similarity index 94% rename from server/routes/api/claimData.js rename to server/controllers/api/claimData.js index 965336df..4549671d 100644 --- a/server/routes/api/claimData.js +++ b/server/controllers/api/claimData.js @@ -1,5 +1,5 @@ const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models'); +const db = require('../../models/index'); /* diff --git a/server/routes/api/claimGet.js b/server/controllers/api/claimGet.js similarity index 93% rename from server/routes/api/claimGet.js rename to server/controllers/api/claimGet.js index 77553397..a5a04926 100644 --- a/server/routes/api/claimGet.js +++ b/server/controllers/api/claimGet.js @@ -1,5 +1,5 @@ -const { getClaim } = require('../../utils/lbryApi.js'); -const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js'); +const { getClaim } = require('../../lbrynet'); +const { addGetResultsToFileData, createFileData } = require('../utils/file.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const db = require('../../models'); diff --git a/server/routes/api/claimList.js b/server/controllers/api/claimList.js similarity index 86% rename from server/routes/api/claimList.js rename to server/controllers/api/claimList.js index 2502007a..300b1864 100644 --- a/server/routes/api/claimList.js +++ b/server/controllers/api/claimList.js @@ -1,4 +1,4 @@ -const { getClaimList } = require('../../utils/lbryApi.js'); +const { getClaimList } = require('../../lbrynet/index'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/claimLongId.js b/server/controllers/api/claimLongId.js similarity index 92% rename from server/routes/api/claimLongId.js rename to server/controllers/api/claimLongId.js index 3286afae..aaa9b171 100644 --- a/server/routes/api/claimLongId.js +++ b/server/controllers/api/claimLongId.js @@ -1,4 +1,4 @@ -const { getClaimId } = require('../../controllers/serveController.js'); +const { getClaimId } = require('../utils/serveHelpers.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const NO_CHANNEL = 'NO_CHANNEL'; diff --git a/server/routes/api/claimPublish.js b/server/controllers/api/claimPublish.js similarity index 87% rename from server/routes/api/claimPublish.js rename to server/controllers/api/claimPublish.js index d1312fdc..f776a46e 100644 --- a/server/routes/api/claimPublish.js +++ b/server/controllers/api/claimPublish.js @@ -1,9 +1,15 @@ -const { createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles } = require('../../helpers/publishHelpers.js'); -const { claimNameIsAvailable, publish } = require('../../controllers/publishController.js'); +const { details: { host } } = require('../../../config/siteConfig.js'); const { authenticateUser } = require('../../auth/authentication.js'); const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const { details: { host } } = require('../../../config/siteConfig.js'); +const checkClaimAvailability = require('../utils/checkClaimAvailability'); +const publish = require('../utils/publish.js'); +const { + createBasicPublishParams, + createThumbnailPublishParams, + parsePublishApiRequestBody, + parsePublishApiRequestFiles, +} = require('../utils/publishHelpers.js'); /* @@ -29,7 +35,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user }, res) => { Promise .all([ authenticateUser(channelName, channelId, channelPassword, user), - claimNameIsAvailable(name), + checkClaimAvailability(name), createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail), createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw), ]) @@ -39,7 +45,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user }, res) => { publishParams['channel_name'] = channelName; publishParams['channel_id'] = channelClaimId; } - // publish the thumbnail + // publish the thumbnail, if one exists if (thumbnailPublishParams) { publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType); } diff --git a/server/routes/api/claimResolve.js b/server/controllers/api/claimResolve.js similarity index 88% rename from server/routes/api/claimResolve.js rename to server/controllers/api/claimResolve.js index 459efa51..2d347ddd 100644 --- a/server/routes/api/claimResolve.js +++ b/server/controllers/api/claimResolve.js @@ -1,4 +1,4 @@ -const { resolveUri } = require('../../utils/lbryApi.js'); +const { resolveUri } = require('../../lbrynet/index'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/claimShortId.js b/server/controllers/api/claimShortId.js similarity index 91% rename from server/routes/api/claimShortId.js rename to server/controllers/api/claimShortId.js index db4833ba..4357c46c 100644 --- a/server/routes/api/claimShortId.js +++ b/server/controllers/api/claimShortId.js @@ -1,5 +1,5 @@ const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models'); +const db = require('../../models/index'); /* diff --git a/server/routes/api/fileAvailability.js b/server/controllers/api/fileAvailability.js similarity index 93% rename from server/routes/api/fileAvailability.js rename to server/controllers/api/fileAvailability.js index f5cdb5bc..116fcaaf 100644 --- a/server/routes/api/fileAvailability.js +++ b/server/controllers/api/fileAvailability.js @@ -1,5 +1,5 @@ const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models'); +const db = require('../../models/index'); /* diff --git a/server/routes/assets/serveAssetByClaim.js b/server/controllers/assets/serveAssetByClaim.js similarity index 100% rename from server/routes/assets/serveAssetByClaim.js rename to server/controllers/assets/serveAssetByClaim.js diff --git a/server/routes/assets/serveAssetByIdentifierAndClaim.js b/server/controllers/assets/serveAssetByIdentifierAndClaim.js similarity index 100% rename from server/routes/assets/serveAssetByIdentifierAndClaim.js rename to server/controllers/assets/serveAssetByIdentifierAndClaim.js diff --git a/server/routes/auth/login.js b/server/controllers/auth/login.js similarity index 91% rename from server/routes/auth/login.js rename to server/controllers/auth/login.js index 63f13814..229b4e98 100644 --- a/server/routes/auth/login.js +++ b/server/controllers/auth/login.js @@ -1,4 +1,4 @@ -const speechPassport = require('../../speechPassport'); +const speechPassport = require('../../speechPassport/index'); const login = (req, res, next) => { speechPassport.authenticate('local-login', (err, user, info) => { diff --git a/server/routes/auth/logout.js b/server/controllers/auth/logout.js similarity index 100% rename from server/routes/auth/logout.js rename to server/controllers/auth/logout.js diff --git a/server/routes/auth/signup.js b/server/controllers/auth/signup.js similarity index 100% rename from server/routes/auth/signup.js rename to server/controllers/auth/signup.js diff --git a/server/routes/auth/user.js b/server/controllers/auth/user.js similarity index 100% rename from server/routes/auth/user.js rename to server/controllers/auth/user.js diff --git a/server/routes/fallback/sendReactApp.js b/server/controllers/fallback/sendReactApp.js similarity index 100% rename from server/routes/fallback/sendReactApp.js rename to server/controllers/fallback/sendReactApp.js diff --git a/server/routes/pages/redirect.js b/server/controllers/pages/redirect.js similarity index 100% rename from server/routes/pages/redirect.js rename to server/controllers/pages/redirect.js diff --git a/server/routes/pages/sendEmbedPage.js b/server/controllers/pages/sendEmbedPage.js similarity index 100% rename from server/routes/pages/sendEmbedPage.js rename to server/controllers/pages/sendEmbedPage.js diff --git a/server/routes/pages/sendReactApp.js b/server/controllers/pages/sendReactApp.js similarity index 100% rename from server/routes/pages/sendReactApp.js rename to server/controllers/pages/sendReactApp.js diff --git a/server/controllers/publishController.js b/server/controllers/publishController.js deleted file mode 100644 index 9037fccc..00000000 --- a/server/controllers/publishController.js +++ /dev/null @@ -1,134 +0,0 @@ -const logger = require('winston'); -const db = require('../models'); -const lbryApi = require('../utils/lbryApi.js'); -const publishHelpers = require('../helpers/publishHelpers.js'); -const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../config/siteConfig.js'); -const Sequelize = require('sequelize'); -const Op = Sequelize.Op; - -module.exports = { - publish (publishParams, fileName, fileType) { - return new Promise((resolve, reject) => { - let publishResults, certificateId, channelName; - // publish the file - return lbryApi.publishClaim(publishParams) - .then(tx => { - logger.info(`Successfully published ${publishParams.name} ${fileName}`, tx); - publishResults = tx; - // get the channel information - if (publishParams.channel_name) { - logger.debug(`this claim was published in channel: ${publishParams.channel_name}`); - return db.Channel.findOne({ - where: { - channelName: publishParams.channel_name, - }, - }); - } else { - logger.debug('this claim was not published in a channel'); - return null; - } - }) - .then(channel => { - // set channel information - certificateId = null; - channelName = null; - if (channel) { - certificateId = channel.channelClaimId; - channelName = channel.channelName; - } - logger.debug(`certificateId: ${certificateId}`); - }) - .then(() => { - // 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, - }; - // 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 upsertCriteria = { - name : publishParams.name, - claimId: publishResults.claim_id, - }; - // upsert the records - 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)]); - }) - .then(() => { - logger.debug('File and Claim records successfully associated'); - resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim; - }) - .catch(error => { - logger.error('PUBLISH ERROR', error); - publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file - reject(error); - }); - }); - }, - claimNameIsAvailable (name) { - const claimAddresses = additionalClaimAddresses || []; - claimAddresses.push(primaryClaimAddress); - // find any records where the name is used - return db.Claim - .findAll({ - attributes: ['address'], - where : { - name, - address: { - [Op.or]: claimAddresses, - }, - }, - }) - .then(result => { - if (result.length >= 1) { - throw new Error('That claim is already in use'); - }; - return name; - }) - .catch(error => { - throw error; - }); - }, - checkChannelAvailability (name) { - return db.Channel - .findAll({ - where: { channelName: name }, - }) - .then(result => { - if (result.length >= 1) { - throw new Error('That channel has already been claimed'); - } - return name; - }) - .catch(error => { - throw error; - }); - }, -}; diff --git a/server/controllers/statsController.js b/server/controllers/statsController.js deleted file mode 100644 index 8292ddc9..00000000 --- a/server/controllers/statsController.js +++ /dev/null @@ -1,19 +0,0 @@ -const logger = require('winston'); -const db = require('../models'); - -module.exports = { - getRecentClaims () { - logger.debug('retrieving most recent claims'); - return new Promise((resolve, reject) => { - // get the raw requests data - db.File.getRecentClaims() - .then(results => { - resolve(results); - }) - .catch(error => { - logger.error('sequelize error', error); - reject(error); - }); - }); - }, -}; diff --git a/server/controllers/utils/channelPagination.js b/server/controllers/utils/channelPagination.js index e76a1158..01c35e2d 100644 --- a/server/controllers/utils/channelPagination.js +++ b/server/controllers/utils/channelPagination.js @@ -4,7 +4,7 @@ module.exports = { returnPaginatedChannelClaims (channelName, longChannelClaimId, claims, page) { const totalPages = module.exports.determineTotalPages(claims); const paginationPage = module.exports.getPageFromQuery(page); - const viewData = { + return { channelName : channelName, longChannelClaimId: longChannelClaimId, claims : module.exports.extractPageFromClaims(claims, paginationPage), @@ -14,7 +14,6 @@ module.exports = { totalPages : totalPages, totalResults : module.exports.determineTotalClaims(claims), }; - return viewData; }, getPageFromQuery (page) { if (page) { @@ -30,8 +29,7 @@ module.exports = { // logger.debug(`pageNumber ${pageNumber} is number?`, Number.isInteger(pageNumber)); const claimStartIndex = (pageNumber - 1) * CLAIMS_PER_PAGE; const claimEndIndex = claimStartIndex + CLAIMS_PER_PAGE; - const pageOfClaims = claims.slice(claimStartIndex, claimEndIndex); - return pageOfClaims; + return claims.slice(claimStartIndex, claimEndIndex); }, determineTotalPages (claims) { if (!claims) { diff --git a/server/controllers/utils/checkChannelAvailability.js b/server/controllers/utils/checkChannelAvailability.js new file mode 100644 index 00000000..8dfce07a --- /dev/null +++ b/server/controllers/utils/checkChannelAvailability.js @@ -0,0 +1,19 @@ +const db = require('../../models'); + +const checkChannelAvailability = (name) => { + return db.Channel + .findAll({ + where: { channelName: name }, + }) + .then(result => { + if (result.length >= 1) { + throw new Error('That channel has already been claimed'); + } + return name; + }) + .catch(error => { + throw error; + }); +}; + +module.exports = checkChannelAvailability; diff --git a/server/controllers/utils/checkClaimAvailability.js b/server/controllers/utils/checkClaimAvailability.js new file mode 100644 index 00000000..9cd3d09b --- /dev/null +++ b/server/controllers/utils/checkClaimAvailability.js @@ -0,0 +1,31 @@ +const db = require('../../models/index'); +const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../config/siteConfig.js'); +const Sequelize = require('sequelize'); +const Op = Sequelize.Op; + +const claimAvailability = (name) => { + const claimAddresses = additionalClaimAddresses || []; + claimAddresses.push(primaryClaimAddress); + // find any records where the name is used + return db.Claim + .findAll({ + attributes: ['address'], + where : { + name, + address: { + [Op.or]: claimAddresses, + }, + }, + }) + .then(result => { + if (result.length >= 1) { + throw new Error('That claim is already in use'); + } + return name; + }) + .catch(error => { + throw error; + }); +}; + +module.exports = claimAvailability; diff --git a/server/controllers/utils/deleteFile.js b/server/controllers/utils/deleteFile.js new file mode 100644 index 00000000..2381f001 --- /dev/null +++ b/server/controllers/utils/deleteFile.js @@ -0,0 +1,13 @@ +const logger = require('winston'); +const fs = require('fs'); + +const deleteFile = (filePath) => { + fs.unlink(filePath, err => { + if (err) { + return logger.error(`error deleting temporary file ${filePath}`); + } + logger.debug(`successfully deleted ${filePath}`); + }); +}; + +module.exports = deleteFile; diff --git a/server/routes/utils/errorHandlers.js b/server/controllers/utils/errorHandlers.js similarity index 98% rename from server/routes/utils/errorHandlers.js rename to server/controllers/utils/errorHandlers.js index 1c7336dd..c2d6cac5 100644 --- a/server/routes/utils/errorHandlers.js +++ b/server/controllers/utils/errorHandlers.js @@ -21,8 +21,8 @@ module.exports = { message = error.message; } else { message = error; - }; - }; + } + } return [status, message]; }, useObjectPropertiesIfNoKeys: function (err) { diff --git a/server/controllers/utils/file.js b/server/controllers/utils/file.js new file mode 100644 index 00000000..b786f8e8 --- /dev/null +++ b/server/controllers/utils/file.js @@ -0,0 +1,20 @@ +module.exports = { + addGetResultsToFileData (fileInfo, getResult) { + fileInfo.fileName = getResult.file_name; + fileInfo.filePath = getResult.download_path; + return fileInfo; + }, + createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) { + return { + name, + claimId, + outpoint, + height, + address, + fileName: '', + filePath: '', + fileType: contentType, + nsfw, + }; + }, +}; diff --git a/server/routes/utils/lbryUri.js b/server/controllers/utils/lbryUri.js similarity index 100% rename from server/routes/utils/lbryUri.js rename to server/controllers/utils/lbryUri.js diff --git a/server/controllers/utils/publish.js b/server/controllers/utils/publish.js new file mode 100644 index 00000000..5d2daea2 --- /dev/null +++ b/server/controllers/utils/publish.js @@ -0,0 +1,92 @@ +const logger = require('winston'); +const db = require('../../models'); +const { publishClaim } = require('../../lbrynet'); +const deleteFile = require('./deleteFile.js'); + +const publish = (publishParams, fileName, fileType) => { + return new Promise((resolve, reject) => { + let publishResults, certificateId, channelName; + // publish the file + return publishClaim(publishParams) + .then(tx => { + logger.info(`Successfully published ${publishParams.name} ${fileName}`, tx); + publishResults = tx; + // get the channel information + if (publishParams.channel_name) { + logger.debug(`this claim was published in channel: ${publishParams.channel_name}`); + return db.Channel.findOne({ + where: { + channelName: publishParams.channel_name, + }, + }); + } else { + logger.debug('this claim was not published in a channel'); + return null; + } + }) + .then(channel => { + // set channel information + certificateId = null; + channelName = null; + if (channel) { + certificateId = channel.channelClaimId; + channelName = channel.channelName; + } + logger.debug(`certificateId: ${certificateId}`); + }) + .then(() => { + // 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, + }; + // 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 upsertCriteria = { + name : publishParams.name, + claimId: publishResults.claim_id, + }; + // upsert the records + 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)]); + }) + .then(() => { + logger.debug('File and Claim records successfully associated'); + resolve(publishResults); // resolve the promise with the result from lbryApi publishClaim; + }) + .catch(error => { + logger.error('PUBLISH ERROR', error); + deleteFile(publishParams.file_path); // delete the local file + reject(error); + }); + }); +}; + +module.exports = publish; diff --git a/server/helpers/publishHelpers.js b/server/controllers/utils/publishHelpers.js similarity index 93% rename from server/helpers/publishHelpers.js rename to server/controllers/utils/publishHelpers.js index 03625d9e..8338f08e 100644 --- a/server/helpers/publishHelpers.js +++ b/server/controllers/utils/publishHelpers.js @@ -1,7 +1,5 @@ const logger = require('winston'); -const fs = require('fs'); - -const { details, publishing } = require('../../config/siteConfig.js'); +const { details, publishing } = require('../../../config/siteConfig.js'); module.exports = { parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) { @@ -146,15 +144,6 @@ module.exports = { channel_id : publishing.thumbnailChannelId, }; }, - deleteTemporaryFile (filePath) { - fs.unlink(filePath, err => { - if (err) { - logger.error(`error deleting temporary file ${filePath}`); - throw err; - } - logger.debug(`successfully deleted ${filePath}`); - }); - }, addGetResultsToFileData (fileInfo, getResult) { fileInfo.fileName = getResult.file_name; fileInfo.filePath = getResult.download_path; diff --git a/server/routes/utils/serve.js b/server/controllers/utils/serve.js similarity index 97% rename from server/routes/utils/serve.js rename to server/controllers/utils/serve.js index a5c9acd0..f9fb7f8b 100644 --- a/server/routes/utils/serve.js +++ b/server/controllers/utils/serve.js @@ -1,5 +1,5 @@ const logger = require('winston'); -const { getClaimId, getLocalFileRecord } = require('../../controllers/serveController.js'); +const { getClaimId, getLocalFileRecord } = require('./serveHelpers.js'); const { handleErrorResponse } = require('./errorHandlers.js'); const SERVE = 'SERVE'; diff --git a/server/controllers/serveController.js b/server/controllers/utils/serveHelpers.js similarity index 97% rename from server/controllers/serveController.js rename to server/controllers/utils/serveHelpers.js index 0513073a..cbed71de 100644 --- a/server/controllers/serveController.js +++ b/server/controllers/utils/serveHelpers.js @@ -1,6 +1,6 @@ -const db = require('../models'); +const db = require('../../models/index'); const logger = require('winston'); -const { returnPaginatedChannelClaims } = require('./utils/channelPagination.js'); +const { returnPaginatedChannelClaims } = require('./channelPagination.js'); const NO_CHANNEL = 'NO_CHANNEL'; const NO_CLAIM = 'NO_CLAIM'; diff --git a/server/utils/lbryApi.js b/server/lbrynet/index.js similarity index 85% rename from server/utils/lbryApi.js rename to server/lbrynet/index.js index 4d54ad2b..c8ea72a1 100644 --- a/server/utils/lbryApi.js +++ b/server/lbrynet/index.js @@ -1,24 +1,9 @@ const axios = require('axios'); const logger = require('winston'); const { api: { apiHost, apiPort } } = require('../../config/lbryConfig.js'); -const lbryApiUri = 'http://' + apiHost + ':' + apiPort; -const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('./googleAnalytics.js'); - -const handleLbrynetResponse = ({ data }, resolve, reject) => { - logger.debug('lbry api data:', data); - if (data.result) { - // check for an error - if (data.result.error) { - logger.debug('Lbrynet api error:', data.result.error); - reject(new Error(data.result.error)); - return; - }; - resolve(data.result); - return; - } - // fallback in case it just timed out - reject(JSON.stringify(data)); -}; +const lbrynetUri = 'http://' + apiHost + ':' + apiPort; +const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('../utils/googleAnalytics.js'); +const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js'); module.exports = { publishClaim (publishParams) { @@ -26,7 +11,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'publish', params: publishParams, }) @@ -44,7 +29,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'get', params: { uri, timeout: 20 }, }) @@ -62,7 +47,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'claim_list', params: { name: claimName }, }) @@ -80,7 +65,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'resolve', params: { uri }, }) @@ -102,7 +87,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'settings_get', }) .then(({ data }) => { @@ -124,7 +109,7 @@ module.exports = { const gaStartTime = Date.now(); return new Promise((resolve, reject) => { axios - .post(lbryApiUri, { + .post(lbrynetUri, { method: 'channel_new', params: { channel_name: name, diff --git a/server/lbrynet/utils/handleLbrynetResponse.js b/server/lbrynet/utils/handleLbrynetResponse.js new file mode 100644 index 00000000..8c430047 --- /dev/null +++ b/server/lbrynet/utils/handleLbrynetResponse.js @@ -0,0 +1,19 @@ +const logger = require('winston'); + +const handleLbrynetResponse = ({ data }, resolve, reject) => { + logger.debug('lbry api data:', data); + if (data.result) { + // check for an error + if (data.result.error) { + logger.debug('Lbrynet api error:', data.result.error); + reject(new Error(data.result.error)); + return; + }; + resolve(data.result); + return; + } + // fallback in case it just timed out + reject(JSON.stringify(data)); +}; + +module.exports = handleLbrynetResponse; diff --git a/server/routes/api/index.js b/server/routes/api/index.js index 59e9e169..15b272f5 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -1,16 +1,16 @@ -const channelAvailability = require('./channelAvailability'); -const channelClaims = require('./channelClaims'); -const channelData = require('./channelData'); -const channelShortId = require('./channelShortId'); -const claimAvailability = require('./claimAvailability'); -const claimData = require('./claimData'); -const claimGet = require('./claimGet'); -const claimLongId = require('./claimLongId'); -const claimPublish = require('./claimPublish'); -const claimResolve = require('./claimResolve'); -const claimShortId = require('./claimShortId'); -const claimList = require('./claimList'); -const fileAvailability = require('./fileAvailability'); +const channelAvailability = require('../../controllers/api/channelAvailability'); +const channelClaims = require('../../controllers/api/channelClaims'); +const channelData = require('../../controllers/api/channelData'); +const channelShortId = require('../../controllers/api/channelShortId'); +const claimAvailability = require('../../controllers/api/claimAvailability'); +const claimData = require('../../controllers/api/claimData'); +const claimGet = require('../../controllers/api/claimGet'); +const claimLongId = require('../../controllers/api/claimLongId'); +const claimPublish = require('../../controllers/api/claimPublish'); +const claimResolve = require('../../controllers/api/claimResolve'); +const claimShortId = require('../../controllers/api/claimShortId'); +const claimList = require('../../controllers/api/claimList'); +const fileAvailability = require('../../controllers/api/fileAvailability'); const multipartMiddleware = require('../utils/multipartMiddleware'); diff --git a/server/routes/assets/index.js b/server/routes/assets/index.js index 4f358a74..d1d82a2a 100644 --- a/server/routes/assets/index.js +++ b/server/routes/assets/index.js @@ -1,5 +1,5 @@ -const serveAssetByClaim = require('./serveAssetByClaim'); -const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim'); +const serveAssetByClaim = require('../../controllers/assets/serveAssetByClaim'); +const serveAssetByIdentifierAndClaim = require('../../controllers/assets/serveAssetByIdentifierAndClaim'); module.exports = (app, db) => { app.get('/:identifier/:claim', serveAssetByIdentifierAndClaim); diff --git a/server/routes/auth/index.js b/server/routes/auth/index.js index a0a4e64b..9754f5b2 100644 --- a/server/routes/auth/index.js +++ b/server/routes/auth/index.js @@ -1,8 +1,8 @@ const speechPassport = require('../../speechPassport'); -const handleSignupRequest = require('./signup'); -const handleLoginRequest = require('./login'); -const handleLogoutRequest = require('./logout'); -const handleUserRequest = require('./user'); +const handleSignupRequest = require('../../controllers/auth/signup'); +const handleLoginRequest = require('../../controllers/auth/login'); +const handleLogoutRequest = require('../../controllers/auth/logout'); +const handleUserRequest = require('../../controllers/auth/user'); module.exports = (app) => { app.post('/signup', speechPassport.authenticate('local-signup'), handleSignupRequest); diff --git a/server/routes/fallback/index.js b/server/routes/fallback/index.js index 35073607..0fc57d0c 100644 --- a/server/routes/fallback/index.js +++ b/server/routes/fallback/index.js @@ -1,4 +1,4 @@ -const handlePageRequest = require('./sendReactApp'); +const handlePageRequest = require('../../controllers/fallback/sendReactApp'); module.exports = (app) => { app.get('*', handlePageRequest); diff --git a/server/routes/pages/index.js b/server/routes/pages/index.js index 5d4c27ae..a7c3a936 100644 --- a/server/routes/pages/index.js +++ b/server/routes/pages/index.js @@ -1,6 +1,6 @@ -const handlePageRequest = require('./sendReactApp'); -const handleEmbedRequest = require('./sendEmbedPage'); -const redirect = require('./redirect'); +const handlePageRequest = require('../../controllers/pages/sendReactApp'); +const handleEmbedRequest = require('../../controllers/pages/sendEmbedPage'); +const redirect = require('../../controllers/pages/redirect'); module.exports = (app) => { app.get('/', handlePageRequest); diff --git a/server/speechPassport/utils/local-signup.js b/server/speechPassport/utils/local-signup.js index 0f4326e6..1a2067e8 100644 --- a/server/speechPassport/utils/local-signup.js +++ b/server/speechPassport/utils/local-signup.js @@ -1,7 +1,7 @@ const PassportLocalStrategy = require('passport-local').Strategy; -const lbryApi = require('../../utils/lbryApi.js'); +const { createChannel } = require('../../lbrynet'); const logger = require('winston'); -const db = require('../../models/index'); +const db = require('../../models'); module.exports = new PassportLocalStrategy( { @@ -14,7 +14,7 @@ module.exports = new PassportLocalStrategy( // server-side validaton of inputs (username, password) // create the channel and retrieve the metadata - return lbryApi.createChannel(`@${username}`) + return createChannel(`@${username}`) .then(tx => { // create user record const userData = { diff --git a/test/unit/publishHelpers.test.js b/test/unit/publishHelpers.test.js index c522b487..471d1f56 100644 --- a/test/unit/publishHelpers.test.js +++ b/test/unit/publishHelpers.test.js @@ -2,7 +2,7 @@ const chai = require('chai'); const expect = chai.expect; describe('publishHelpers.js', function () { - const publishHelpers = require('../../server/helpers/publishHelpers.js'); + const publishHelpers = require('../../server/controllers/utils/publishHelpers.js'); describe('#parsePublishApiRequestBody()', function () { it('should throw an error if no body', function () {