From aa1834e1c6138549ce906d3e7dcc2e14d2c1b567 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 27 Apr 2018 09:54:36 -0700 Subject: [PATCH] organized api controllers and helpers --- .../availability}/checkChannelAvailability.js | 2 +- .../availability/index.js} | 6 +- .../channel/claims}/channelPagination.js | 0 .../api/channel/claims/getChannelClaims.js | 31 ++++ .../claims/index.js} | 4 +- .../api/channel/data/getChannelData.js | 32 ++++ .../{channelData.js => channel/data/index.js} | 4 +- .../shortId/index.js} | 4 +- .../availability/index.js} | 4 +- .../api/{claimData.js => claim/data/index.js} | 4 +- .../api/{claimGet.js => claim/get/index.js} | 8 +- .../api/{claimList.js => claim/list/index.js} | 4 +- .../{claimLongId.js => claim/longId/index.js} | 4 +- .../claim/publish/createBasicPublishParams.js | 40 +++++ .../publish/createThumbnailPublishParams.js | 28 +++ .../claim/publish}/deleteFile.js | 0 .../publish/index.js} | 22 ++- .../publish/parsePublishApiRequestBody.js | 27 +++ .../publish/parsePublishApiRequestFiles.js | 34 ++++ .../{utils => api/claim/publish}/publish.js | 4 +- .../claim/publish/validateFileTypeAndSize.js | 33 ++++ .../resolve/index.js} | 4 +- .../shortId/index.js} | 4 +- .../claim}/utils/checkClaimAvailability.js | 4 +- .../availability/index.js} | 4 +- .../index.js} | 8 +- .../index.js} | 8 +- server/controllers/utils/publishHelpers.js | 165 ------------------ server/controllers/utils/serveHelpers.js | 55 +----- server/routes/api/index.js | 26 +-- server/routes/assets/index.js | 4 +- server/speechPassport/utils/local-login.js | 2 +- .../utils.test.js} | 27 ++- 33 files changed, 310 insertions(+), 296 deletions(-) rename server/controllers/{utils => api/channel/availability}/checkChannelAvailability.js (89%) rename server/controllers/api/{channelAvailability.js => channel/availability/index.js} (69%) rename server/controllers/{utils => api/channel/claims}/channelPagination.js (100%) create mode 100644 server/controllers/api/channel/claims/getChannelClaims.js rename server/controllers/api/{channelClaims.js => channel/claims/index.js} (84%) create mode 100644 server/controllers/api/channel/data/getChannelData.js rename server/controllers/api/{channelData.js => channel/data/index.js} (82%) rename server/controllers/api/{channelShortId.js => channel/shortId/index.js} (77%) rename server/controllers/api/{claimAvailability.js => claim/availability/index.js} (78%) rename server/controllers/api/{claimData.js => claim/data/index.js} (83%) rename server/controllers/api/{claimGet.js => claim/get/index.js} (84%) rename server/controllers/api/{claimList.js => claim/list/index.js} (70%) rename server/controllers/api/{claimLongId.js => claim/longId/index.js} (86%) create mode 100644 server/controllers/api/claim/publish/createBasicPublishParams.js create mode 100644 server/controllers/api/claim/publish/createThumbnailPublishParams.js rename server/controllers/{utils => api/claim/publish}/deleteFile.js (100%) rename server/controllers/api/{claimPublish.js => claim/publish/index.js} (78%) create mode 100644 server/controllers/api/claim/publish/parsePublishApiRequestBody.js create mode 100644 server/controllers/api/claim/publish/parsePublishApiRequestFiles.js rename server/controllers/{utils => api/claim/publish}/publish.js (97%) create mode 100644 server/controllers/api/claim/publish/validateFileTypeAndSize.js rename server/controllers/api/{claimResolve.js => claim/resolve/index.js} (73%) rename server/controllers/api/{claimShortId.js => claim/shortId/index.js} (77%) rename server/controllers/{ => api/claim}/utils/checkClaimAvailability.js (87%) rename server/controllers/api/{fileAvailability.js => file/availability/index.js} (82%) rename server/controllers/assets/{serveAssetByClaim.js => serveByClaim/index.js} (83%) rename server/controllers/assets/{serveAssetByIdentifierAndClaim.js => serveByIdentifierAndClaim/index.js} (87%) delete mode 100644 server/controllers/utils/publishHelpers.js rename test/unit/{publishHelpers.test.js => publish/utils.test.js} (62%) diff --git a/server/controllers/utils/checkChannelAvailability.js b/server/controllers/api/channel/availability/checkChannelAvailability.js similarity index 89% rename from server/controllers/utils/checkChannelAvailability.js rename to server/controllers/api/channel/availability/checkChannelAvailability.js index 8dfce07a..3c5d78b5 100644 --- a/server/controllers/utils/checkChannelAvailability.js +++ b/server/controllers/api/channel/availability/checkChannelAvailability.js @@ -1,4 +1,4 @@ -const db = require('../../models'); +const db = require('../../../../models'); const checkChannelAvailability = (name) => { return db.Channel diff --git a/server/controllers/api/channelAvailability.js b/server/controllers/api/channel/availability/index.js similarity index 69% rename from server/controllers/api/channelAvailability.js rename to server/controllers/api/channel/availability/index.js index 33f5f5f3..ae2a3807 100644 --- a/server/controllers/api/channelAvailability.js +++ b/server/controllers/api/channel/availability/index.js @@ -1,6 +1,6 @@ -const checkChannelAvailability = require('../utils/checkChannelAvailability.js'); -const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const checkChannelAvailability = require('./checkChannelAvailability.js'); +const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); /* diff --git a/server/controllers/utils/channelPagination.js b/server/controllers/api/channel/claims/channelPagination.js similarity index 100% rename from server/controllers/utils/channelPagination.js rename to server/controllers/api/channel/claims/channelPagination.js diff --git a/server/controllers/api/channel/claims/getChannelClaims.js b/server/controllers/api/channel/claims/getChannelClaims.js new file mode 100644 index 00000000..aec6f03e --- /dev/null +++ b/server/controllers/api/channel/claims/getChannelClaims.js @@ -0,0 +1,31 @@ +const db = require('../../../../models'); +const { returnPaginatedChannelClaims } = require('./channelPagination.js'); +const NO_CHANNEL = 'NO_CHANNEL'; + +const getChannelClaims = (channelName, channelClaimId, page) => { + return new Promise((resolve, reject) => { + // 1. get the long channel Id (make sure channel exists) + db.Certificate.getLongChannelId(channelName, channelClaimId) + .then(longChannelClaimId => { + if (!longChannelClaimId) { + return [null, null, null]; + } + // 2. get the short ID and all claims for that channel + return Promise.all([longChannelClaimId, db.Claim.getAllChannelClaims(longChannelClaimId)]); + }) + .then(([longChannelClaimId, channelClaimsArray]) => { + if (!longChannelClaimId) { + return resolve(NO_CHANNEL); + } + // 3. format the data for the view, including pagination + let paginatedChannelViewData = returnPaginatedChannelClaims(channelName, longChannelClaimId, channelClaimsArray, page); + // 4. return all the channel information and contents + resolve(paginatedChannelViewData); + }) + .catch(error => { + reject(error); + }); + }); +}; + +module.exports = getChannelClaims; diff --git a/server/controllers/api/channelClaims.js b/server/controllers/api/channel/claims/index.js similarity index 84% rename from server/controllers/api/channelClaims.js rename to server/controllers/api/channel/claims/index.js index d9a3683e..4c7b6777 100644 --- a/server/controllers/api/channelClaims.js +++ b/server/controllers/api/channel/claims/index.js @@ -1,5 +1,5 @@ -const { getChannelClaims } = require('../utils/serveHelpers.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const getChannelClaims = require('./getChannelClaims.js'); const NO_CHANNEL = 'NO_CHANNEL'; diff --git a/server/controllers/api/channel/data/getChannelData.js b/server/controllers/api/channel/data/getChannelData.js new file mode 100644 index 00000000..98c3a8d5 --- /dev/null +++ b/server/controllers/api/channel/data/getChannelData.js @@ -0,0 +1,32 @@ +const db = require('../../../../models'); +const NO_CHANNEL = 'NO_CHANNEL'; + +const getChannelData = (channelName, channelClaimId, page) => { + return new Promise((resolve, reject) => { + // 1. get the long channel Id (make sure channel exists) + db.Certificate.getLongChannelId(channelName, channelClaimId) + .then(longChannelClaimId => { + if (!longChannelClaimId) { + return [null, null, null]; + } + // 2. get the short ID and all claims for that channel + return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName)]); + }) + .then(([longChannelClaimId, shortChannelClaimId]) => { + if (!longChannelClaimId) { + return resolve(NO_CHANNEL); + } + // 3. return all the channel information + resolve({ + channelName, + longChannelClaimId, + shortChannelClaimId, + }); + }) + .catch(error => { + reject(error); + }); + }); +}; + +module.exports = getChannelData; diff --git a/server/controllers/api/channelData.js b/server/controllers/api/channel/data/index.js similarity index 82% rename from server/controllers/api/channelData.js rename to server/controllers/api/channel/data/index.js index 92609412..ad678d78 100644 --- a/server/controllers/api/channelData.js +++ b/server/controllers/api/channel/data/index.js @@ -1,5 +1,5 @@ -const { getChannelData } = require('../utils/serveHelpers.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { getChannelData } = require('../../../utils/serveHelpers.js'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const NO_CHANNEL = 'NO_CHANNEL'; diff --git a/server/controllers/api/channelShortId.js b/server/controllers/api/channel/shortId/index.js similarity index 77% rename from server/controllers/api/channelShortId.js rename to server/controllers/api/channel/shortId/index.js index 898456a1..937d92b8 100644 --- a/server/controllers/api/channelShortId.js +++ b/server/controllers/api/channel/shortId/index.js @@ -1,5 +1,5 @@ -const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models/index'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const db = require('../../../../models'); /* diff --git a/server/controllers/api/claimAvailability.js b/server/controllers/api/claim/availability/index.js similarity index 78% rename from server/controllers/api/claimAvailability.js rename to server/controllers/api/claim/availability/index.js index 23a3a21f..9f9d330d 100644 --- a/server/controllers/api/claimAvailability.js +++ b/server/controllers/api/claim/availability/index.js @@ -1,6 +1,6 @@ const checkClaimAvailability = require('../utils/checkClaimAvailability.js'); -const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); /* diff --git a/server/controllers/api/claimData.js b/server/controllers/api/claim/data/index.js similarity index 83% rename from server/controllers/api/claimData.js rename to server/controllers/api/claim/data/index.js index 4549671d..49361d71 100644 --- a/server/controllers/api/claimData.js +++ b/server/controllers/api/claim/data/index.js @@ -1,5 +1,5 @@ -const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models/index'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const db = require('../../../../models'); /* diff --git a/server/controllers/api/claimGet.js b/server/controllers/api/claim/get/index.js similarity index 84% rename from server/controllers/api/claimGet.js rename to server/controllers/api/claim/get/index.js index a5a04926..d54fcc87 100644 --- a/server/controllers/api/claimGet.js +++ b/server/controllers/api/claim/get/index.js @@ -1,7 +1,7 @@ -const { getClaim } = require('../../lbrynet'); -const { addGetResultsToFileData, createFileData } = require('../utils/file.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models'); +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/controllers/api/claimList.js b/server/controllers/api/claim/list/index.js similarity index 70% rename from server/controllers/api/claimList.js rename to server/controllers/api/claim/list/index.js index 300b1864..bb7eb1d0 100644 --- a/server/controllers/api/claimList.js +++ b/server/controllers/api/claim/list/index.js @@ -1,5 +1,5 @@ -const { getClaimList } = require('../../lbrynet/index'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { getClaimList } = require('../../../../lbrynet'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); /* diff --git a/server/controllers/api/claimLongId.js b/server/controllers/api/claim/longId/index.js similarity index 86% rename from server/controllers/api/claimLongId.js rename to server/controllers/api/claim/longId/index.js index aaa9b171..b84ded1e 100644 --- a/server/controllers/api/claimLongId.js +++ b/server/controllers/api/claim/longId/index.js @@ -1,5 +1,5 @@ -const { getClaimId } = require('../utils/serveHelpers.js'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { getClaimId } = require('../../../utils/serveHelpers.js'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const NO_CHANNEL = 'NO_CHANNEL'; const NO_CLAIM = 'NO_CLAIM'; diff --git a/server/controllers/api/claim/publish/createBasicPublishParams.js b/server/controllers/api/claim/publish/createBasicPublishParams.js new file mode 100644 index 00000000..a8d84404 --- /dev/null +++ b/server/controllers/api/claim/publish/createBasicPublishParams.js @@ -0,0 +1,40 @@ +const logger = require('winston'); +const { details, publishing } = require('../../../../../config/siteConfig.js'); + +const createBasicPublishParams = (filePath, name, title, description, license, nsfw, thumbnail) => { + logger.debug(`Creating Publish Parameters`); + // provide defaults for title + if (title === null || title.trim() === '') { + title = name; + } + // provide default for description + if (description === null || description.trim() === '') { + description = ''; + } + // provide default for license + if (license === null || license.trim() === '') { + license = ' '; // default to empty string + } + // create the publish params + const publishParams = { + name, + file_path: filePath, + bid : 0.01, + metadata : { + description, + title, + author : details.title, + language: 'en', + license, + nsfw, + }, + claim_address: publishing.primaryClaimAddress, + }; + // add thumbnail to channel if video + if (thumbnail) { + publishParams['metadata']['thumbnail'] = thumbnail; + } + return publishParams; +}; + +module.exports = createBasicPublishParams; diff --git a/server/controllers/api/claim/publish/createThumbnailPublishParams.js b/server/controllers/api/claim/publish/createThumbnailPublishParams.js new file mode 100644 index 00000000..c10d9cd9 --- /dev/null +++ b/server/controllers/api/claim/publish/createThumbnailPublishParams.js @@ -0,0 +1,28 @@ +const logger = require('winston'); +const { details, publishing } = require('../../../../../config/siteConfig.js'); + +const createThumbnailPublishParams = (thumbnailFilePath, claimName, license, nsfw) => { + if (!thumbnailFilePath) { + return; + } + logger.debug(`Creating Thumbnail Publish Parameters`); + // create the publish params + return { + name : `${claimName}-thumb`, + file_path: thumbnailFilePath, + bid : 0.01, + metadata : { + title : `${claimName} thumbnail`, + description: `a thumbnail for ${claimName}`, + author : details.title, + language : 'en', + license, + nsfw, + }, + claim_address: publishing.primaryClaimAddress, + channel_name : publishing.thumbnailChannel, + channel_id : publishing.thumbnailChannelId, + }; +}; + +module.exports = createThumbnailPublishParams; diff --git a/server/controllers/utils/deleteFile.js b/server/controllers/api/claim/publish/deleteFile.js similarity index 100% rename from server/controllers/utils/deleteFile.js rename to server/controllers/api/claim/publish/deleteFile.js diff --git a/server/controllers/api/claimPublish.js b/server/controllers/api/claim/publish/index.js similarity index 78% rename from server/controllers/api/claimPublish.js rename to server/controllers/api/claim/publish/index.js index f776a46e..2d4ee10d 100644 --- a/server/controllers/api/claimPublish.js +++ b/server/controllers/api/claim/publish/index.js @@ -1,15 +1,13 @@ -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 checkClaimAvailability = require('../utils/checkClaimAvailability'); -const publish = require('../utils/publish.js'); -const { - createBasicPublishParams, - createThumbnailPublishParams, - parsePublishApiRequestBody, - parsePublishApiRequestFiles, -} = require('../utils/publishHelpers.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 publish = require('./publish.js'); +const createBasicPublishParams = require('./createBasicPublishParams.js'); +const createThumbnailPublishParams = require('./createThumbnailPublishParams.js'); +const parsePublishApiRequestBody = require('./parsePublishApiRequestBody.js'); +const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js'); +const checkClaimAvailability = require('../utils/checkClaimAvailability.js'); /* diff --git a/server/controllers/api/claim/publish/parsePublishApiRequestBody.js b/server/controllers/api/claim/publish/parsePublishApiRequestBody.js new file mode 100644 index 00000000..e417e0a9 --- /dev/null +++ b/server/controllers/api/claim/publish/parsePublishApiRequestBody.js @@ -0,0 +1,27 @@ +const parsePublishApiRequestBody = ({name, nsfw, license, title, description, thumbnail}) => { + // validate name + if (!name) { + throw new Error('no name field found in request'); + } + const invalidNameCharacters = /[^A-Za-z0-9,-]/.exec(name); + if (invalidNameCharacters) { + throw new Error('The claim name you provided is not allowed. Only the following characters are allowed: A-Z, a-z, 0-9, and "-"'); + } + // optional parameters + nsfw = (nsfw === 'true'); + license = license || null; + title = title || null; + description = description || null; + thumbnail = thumbnail || null; + // return results + return { + name, + nsfw, + license, + title, + description, + thumbnail, + }; +}; + +module.exports = parsePublishApiRequestBody; diff --git a/server/controllers/api/claim/publish/parsePublishApiRequestFiles.js b/server/controllers/api/claim/publish/parsePublishApiRequestFiles.js new file mode 100644 index 00000000..d7b509e9 --- /dev/null +++ b/server/controllers/api/claim/publish/parsePublishApiRequestFiles.js @@ -0,0 +1,34 @@ +const validateFileTypeAndSize = require('./validateFileTypeAndSize.js'); + +const parsePublishApiRequestFiles = ({file, thumbnail}) => { + // make sure a file was provided + if (!file) { + throw new Error('no file with key of [file] found in request'); + } + if (!file.path) { + throw new Error('no file path found'); + } + if (!file.type) { + throw new Error('no file type found'); + } + if (!file.size) { + throw new Error('no file type found'); + } + // validate the file name + if (/'/.test(file.name)) { + throw new Error('apostrophes are not allowed in the file name'); + } + // validate the file + validateFileTypeAndSize(file); + // return results + return { + fileName : file.name, + filePath : file.path, + fileType : file.type, + thumbnailFileName: (thumbnail ? thumbnail.name : null), + thumbnailFilePath: (thumbnail ? thumbnail.path : null), + thumbnailFileType: (thumbnail ? thumbnail.type : null), + }; +}; + +module.exports = parsePublishApiRequestFiles; diff --git a/server/controllers/utils/publish.js b/server/controllers/api/claim/publish/publish.js similarity index 97% rename from server/controllers/utils/publish.js rename to server/controllers/api/claim/publish/publish.js index 5d2daea2..b80dc0b7 100644 --- a/server/controllers/utils/publish.js +++ b/server/controllers/api/claim/publish/publish.js @@ -1,6 +1,6 @@ const logger = require('winston'); -const db = require('../../models'); -const { publishClaim } = require('../../lbrynet'); +const db = require('../../../../models'); +const { publishClaim } = require('../../../../lbrynet'); const deleteFile = require('./deleteFile.js'); const publish = (publishParams, fileName, fileType) => { diff --git a/server/controllers/api/claim/publish/validateFileTypeAndSize.js b/server/controllers/api/claim/publish/validateFileTypeAndSize.js new file mode 100644 index 00000000..ca3d4f17 --- /dev/null +++ b/server/controllers/api/claim/publish/validateFileTypeAndSize.js @@ -0,0 +1,33 @@ +const logger = require('winston'); + +const validateFileTypeAndSize = (file) => { + // check file type and size + switch (file.type) { + case 'image/jpeg': + case 'image/jpg': + case 'image/png': + if (file.size > 10000000) { + logger.debug('publish > file validation > .jpeg/.jpg/.png was too big'); + throw new Error('Sorry, images are limited to 10 megabytes.'); + } + break; + case 'image/gif': + if (file.size > 50000000) { + logger.debug('publish > file validation > .gif was too big'); + throw new Error('Sorry, .gifs are limited to 50 megabytes.'); + } + break; + case 'video/mp4': + if (file.size > 50000000) { + logger.debug('publish > file validation > .mp4 was too big'); + throw new Error('Sorry, videos are limited to 50 megabytes.'); + } + break; + default: + logger.debug('publish > file validation > unrecognized file type'); + throw new Error('The ' + file.type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.'); + } + return file; +}; + +module.exports = validateFileTypeAndSize; diff --git a/server/controllers/api/claimResolve.js b/server/controllers/api/claim/resolve/index.js similarity index 73% rename from server/controllers/api/claimResolve.js rename to server/controllers/api/claim/resolve/index.js index 2d347ddd..9b057bda 100644 --- a/server/controllers/api/claimResolve.js +++ b/server/controllers/api/claim/resolve/index.js @@ -1,5 +1,5 @@ -const { resolveUri } = require('../../lbrynet/index'); -const { handleErrorResponse } = require('../utils/errorHandlers.js'); +const { resolveUri } = require('../../../../lbrynet/index'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); /* diff --git a/server/controllers/api/claimShortId.js b/server/controllers/api/claim/shortId/index.js similarity index 77% rename from server/controllers/api/claimShortId.js rename to server/controllers/api/claim/shortId/index.js index 4357c46c..c10e41ee 100644 --- a/server/controllers/api/claimShortId.js +++ b/server/controllers/api/claim/shortId/index.js @@ -1,5 +1,5 @@ -const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models/index'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const db = require('../../../../models'); /* diff --git a/server/controllers/utils/checkClaimAvailability.js b/server/controllers/api/claim/utils/checkClaimAvailability.js similarity index 87% rename from server/controllers/utils/checkClaimAvailability.js rename to server/controllers/api/claim/utils/checkClaimAvailability.js index 9cd3d09b..f10f2590 100644 --- a/server/controllers/utils/checkClaimAvailability.js +++ b/server/controllers/api/claim/utils/checkClaimAvailability.js @@ -1,5 +1,5 @@ -const db = require('../../models/index'); -const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../config/siteConfig.js'); +const db = require('../../../../models'); +const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../../../config/siteConfig.js'); const Sequelize = require('sequelize'); const Op = Sequelize.Op; diff --git a/server/controllers/api/fileAvailability.js b/server/controllers/api/file/availability/index.js similarity index 82% rename from server/controllers/api/fileAvailability.js rename to server/controllers/api/file/availability/index.js index 116fcaaf..d55ed230 100644 --- a/server/controllers/api/fileAvailability.js +++ b/server/controllers/api/file/availability/index.js @@ -1,5 +1,5 @@ -const { handleErrorResponse } = require('../utils/errorHandlers.js'); -const db = require('../../models/index'); +const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const db = require('../../../../models'); /* diff --git a/server/controllers/assets/serveAssetByClaim.js b/server/controllers/assets/serveByClaim/index.js similarity index 83% rename from server/controllers/assets/serveAssetByClaim.js rename to server/controllers/assets/serveByClaim/index.js index 63592043..51d9a500 100644 --- a/server/controllers/assets/serveAssetByClaim.js +++ b/server/controllers/assets/serveByClaim/index.js @@ -1,11 +1,11 @@ -const { sendGAServeEvent } = require('../../utils/googleAnalytics'); +const { sendGAServeEvent } = require('../../../utils/googleAnalytics'); const { determineResponseType, logRequestData, getClaimIdAndServeAsset, -} = require('../utils/serve.js'); -const lbryUri = require('../utils/lbryUri.js'); -const handleShowRender = require('../../render/build/handleShowRender.js'); +} = require('../../utils/serve.js'); +const lbryUri = require('../../utils/lbryUri.js'); +const handleShowRender = require('../../../render/build/handleShowRender.js'); const SERVE = 'SERVE'; /* diff --git a/server/controllers/assets/serveAssetByIdentifierAndClaim.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js similarity index 87% rename from server/controllers/assets/serveAssetByIdentifierAndClaim.js rename to server/controllers/assets/serveByIdentifierAndClaim/index.js index f7bd8d18..954421d9 100644 --- a/server/controllers/assets/serveAssetByIdentifierAndClaim.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -1,12 +1,12 @@ -const { sendGAServeEvent } = require('../../utils/googleAnalytics'); +const { sendGAServeEvent } = require('../../../utils/googleAnalytics'); const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset, -} = require('../utils/serve.js'); -const lbryUri = require('../utils/lbryUri.js'); -const handleShowRender = require('../../render/build/handleShowRender.js'); +} = require('../../utils/serve.js'); +const lbryUri = require('../../utils/lbryUri.js'); +const handleShowRender = require('../../../render/build/handleShowRender.js'); const SERVE = 'SERVE'; diff --git a/server/controllers/utils/publishHelpers.js b/server/controllers/utils/publishHelpers.js deleted file mode 100644 index 8338f08e..00000000 --- a/server/controllers/utils/publishHelpers.js +++ /dev/null @@ -1,165 +0,0 @@ -const logger = require('winston'); -const { details, publishing } = require('../../../config/siteConfig.js'); - -module.exports = { - parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) { - // validate name - if (!name) { - throw new Error('no name field found in request'); - } - const invalidNameCharacters = /[^A-Za-z0-9,-]/.exec(name); - if (invalidNameCharacters) { - throw new Error('The claim name you provided is not allowed. Only the following characters are allowed: A-Z, a-z, 0-9, and "-"'); - } - // optional parameters - nsfw = (nsfw === 'true'); - license = license || null; - title = title || null; - description = description || null; - thumbnail = thumbnail || null; - // return results - return { - name, - nsfw, - license, - title, - description, - thumbnail, - }; - }, - parsePublishApiRequestFiles ({file, thumbnail}) { - // make sure a file was provided - if (!file) { - throw new Error('no file with key of [file] found in request'); - } - if (!file.path) { - throw new Error('no file path found'); - } - if (!file.type) { - throw new Error('no file type found'); - } - if (!file.size) { - throw new Error('no file type found'); - } - // validate the file name - if (/'/.test(file.name)) { - throw new Error('apostrophes are not allowed in the file name'); - } - // validate the file - module.exports.validateFileTypeAndSize(file); - // return results - return { - fileName : file.name, - filePath : file.path, - fileType : file.type, - thumbnailFileName: (thumbnail ? thumbnail.name : null), - thumbnailFilePath: (thumbnail ? thumbnail.path : null), - thumbnailFileType: (thumbnail ? thumbnail.type : null), - }; - }, - validateFileTypeAndSize (file) { - // check file type and size - switch (file.type) { - case 'image/jpeg': - case 'image/jpg': - case 'image/png': - if (file.size > 10000000) { - logger.debug('publish > file validation > .jpeg/.jpg/.png was too big'); - throw new Error('Sorry, images are limited to 10 megabytes.'); - } - break; - case 'image/gif': - if (file.size > 50000000) { - logger.debug('publish > file validation > .gif was too big'); - throw new Error('Sorry, .gifs are limited to 50 megabytes.'); - } - break; - case 'video/mp4': - if (file.size > 50000000) { - logger.debug('publish > file validation > .mp4 was too big'); - throw new Error('Sorry, videos are limited to 50 megabytes.'); - } - break; - default: - logger.debug('publish > file validation > unrecognized file type'); - throw new Error('The ' + file.type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.'); - } - return file; - }, - createBasicPublishParams (filePath, name, title, description, license, nsfw, thumbnail) { - logger.debug(`Creating Publish Parameters`); - // provide defaults for title - if (title === null || title.trim() === '') { - title = name; - } - // provide default for description - if (description === null || description.trim() === '') { - description = ''; - } - // provide default for license - if (license === null || license.trim() === '') { - license = ' '; // default to empty string - } - // create the publish params - const publishParams = { - name, - file_path: filePath, - bid : 0.01, - metadata : { - description, - title, - author : details.title, - language: 'en', - license, - nsfw, - }, - claim_address: publishing.primaryClaimAddress, - }; - // add thumbnail to channel if video - if (thumbnail) { - publishParams['metadata']['thumbnail'] = thumbnail; - } - return publishParams; - }, - createThumbnailPublishParams (thumbnailFilePath, claimName, license, nsfw) { - if (!thumbnailFilePath) { - return; - } - logger.debug(`Creating Thumbnail Publish Parameters`); - // create the publish params - return { - name : `${claimName}-thumb`, - file_path: thumbnailFilePath, - bid : 0.01, - metadata : { - title : `${claimName} thumbnail`, - description: `a thumbnail for ${claimName}`, - author : details.title, - language : 'en', - license, - nsfw, - }, - claim_address: publishing.primaryClaimAddress, - channel_name : publishing.thumbnailChannel, - channel_id : publishing.thumbnailChannelId, - }; - }, - 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/controllers/utils/serveHelpers.js b/server/controllers/utils/serveHelpers.js index cbed71de..68b46335 100644 --- a/server/controllers/utils/serveHelpers.js +++ b/server/controllers/utils/serveHelpers.js @@ -1,6 +1,5 @@ -const db = require('../../models/index'); +const db = require('../../models'); const logger = require('winston'); -const { returnPaginatedChannelClaims } = require('./channelPagination.js'); const NO_CHANNEL = 'NO_CHANNEL'; const NO_CLAIM = 'NO_CLAIM'; @@ -53,58 +52,6 @@ module.exports = { }); }); }, - getChannelData (channelName, channelClaimId, page) { - return new Promise((resolve, reject) => { - // 1. get the long channel Id (make sure channel exists) - db.Certificate.getLongChannelId(channelName, channelClaimId) - .then(longChannelClaimId => { - if (!longChannelClaimId) { - return [null, null, null]; - } - // 2. get the short ID and all claims for that channel - return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName)]); - }) - .then(([longChannelClaimId, shortChannelClaimId]) => { - if (!longChannelClaimId) { - return resolve(NO_CHANNEL); - } - // 3. return all the channel information - resolve({ - channelName, - longChannelClaimId, - shortChannelClaimId, - }); - }) - .catch(error => { - reject(error); - }); - }); - }, - getChannelClaims (channelName, channelClaimId, page) { - return new Promise((resolve, reject) => { - // 1. get the long channel Id (make sure channel exists) - db.Certificate.getLongChannelId(channelName, channelClaimId) - .then(longChannelClaimId => { - if (!longChannelClaimId) { - return [null, null, null]; - } - // 2. get the short ID and all claims for that channel - return Promise.all([longChannelClaimId, db.Claim.getAllChannelClaims(longChannelClaimId)]); - }) - .then(([longChannelClaimId, channelClaimsArray]) => { - if (!longChannelClaimId) { - return resolve(NO_CHANNEL); - } - // 3. format the data for the view, including pagination - let paginatedChannelViewData = returnPaginatedChannelClaims(channelName, longChannelClaimId, channelClaimsArray, page); - // 4. return all the channel information and contents - resolve(paginatedChannelViewData); - }) - .catch(error => { - reject(error); - }); - }); - }, getLocalFileRecord (claimId, name) { return db.File.findOne({where: {claimId, name}}) .then(file => { diff --git a/server/routes/api/index.js b/server/routes/api/index.js index 15b272f5..a1a46974 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -1,16 +1,16 @@ -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 channelAvailability = require('../../controllers/api/channel/availability'); +const channelClaims = require('../../controllers/api/channel/claims'); +const channelData = require('../../controllers/api/channel/data'); +const channelShortId = require('../../controllers/api/channel/shortId'); +const claimAvailability = require('../../controllers/api/claim/availability'); +const claimData = require('../../controllers/api/claim/data/'); +const claimGet = require('../../controllers/api/claim/get'); +const claimLongId = require('../../controllers/api/claim/longId'); +const claimPublish = require('../../controllers/api/claim/publish'); +const claimResolve = require('../../controllers/api/claim/resolve'); +const claimShortId = require('../../controllers/api/claim/shortId'); +const claimList = require('../../controllers/api/claim/list'); +const fileAvailability = require('../../controllers/api/file/availability'); const multipartMiddleware = require('../utils/multipartMiddleware'); diff --git a/server/routes/assets/index.js b/server/routes/assets/index.js index d1d82a2a..4e87d2dd 100644 --- a/server/routes/assets/index.js +++ b/server/routes/assets/index.js @@ -1,5 +1,5 @@ -const serveAssetByClaim = require('../../controllers/assets/serveAssetByClaim'); -const serveAssetByIdentifierAndClaim = require('../../controllers/assets/serveAssetByIdentifierAndClaim'); +const serveAssetByClaim = require('../../controllers/assets/serveByClaim'); +const serveAssetByIdentifierAndClaim = require('../../controllers/assets/serveByIdentifierAndClaim'); module.exports = (app, db) => { app.get('/:identifier/:claim', serveAssetByIdentifierAndClaim); diff --git a/server/speechPassport/utils/local-login.js b/server/speechPassport/utils/local-login.js index a8a11b8f..d9f889c8 100644 --- a/server/speechPassport/utils/local-login.js +++ b/server/speechPassport/utils/local-login.js @@ -1,6 +1,6 @@ const PassportLocalStrategy = require('passport-local').Strategy; const logger = require('winston'); -const db = require('../../models/index'); +const db = require('../../models'); const returnUserAndChannelInfo = (userInstance) => { return new Promise((resolve, reject) => { diff --git a/test/unit/publishHelpers.test.js b/test/unit/publish/utils.test.js similarity index 62% rename from test/unit/publishHelpers.test.js rename to test/unit/publish/utils.test.js index 471d1f56..4d8aac20 100644 --- a/test/unit/publishHelpers.test.js +++ b/test/unit/publish/utils.test.js @@ -1,27 +1,34 @@ const chai = require('chai'); const expect = chai.expect; -describe('publishHelpers.js', function () { - const publishHelpers = require('../../server/controllers/utils/publishHelpers.js'); +describe('publish utils', function () { describe('#parsePublishApiRequestBody()', function () { + const parsePublishApiRequestBody = require('../../../server/controllers/api/claim/publish/parsePublishApiRequestBody.js'); + it('should throw an error if no body', function () { - expect(publishHelpers.parsePublishApiRequestBody.bind(this, null)).to.throw(); + expect(parsePublishApiRequestBody.bind(this, null)).to.throw(); }); + it('should throw an error if no body.name', function () { const bodyNoName = {}; - expect(publishHelpers.parsePublishApiRequestBody.bind(this, bodyNoName)).to.throw(); + expect(parsePublishApiRequestBody.bind(this, bodyNoName)).to.throw(); }); + }); describe('#parsePublishApiRequestFiles()', function () { + const parsePublishApiRequestFiles = require('../../../server/controllers/api/claim/publish/parsePublishApiRequestFiles.js'); + it('should throw an error if no files', function () { - expect(publishHelpers.parsePublishApiRequestFiles.bind(this, null)).to.throw(); + expect(parsePublishApiRequestFiles.bind(this, null)).to.throw(); }); + it('should throw an error if no files.file', function () { const filesNoFile = {}; - expect(publishHelpers.parsePublishApiRequestFiles.bind(this, filesNoFile)).to.throw(); + expect(parsePublishApiRequestFiles.bind(this, filesNoFile)).to.throw(); }); + it('should throw an error if file.size is too large', function () { const filesTooBig = { file: { @@ -31,8 +38,9 @@ describe('publishHelpers.js', function () { size: 10000001, }, }; - expect(publishHelpers.parsePublishApiRequestFiles.bind(this, filesTooBig)).to.throw(); + expect(parsePublishApiRequestFiles.bind(this, filesTooBig)).to.throw(); }); + it('should throw error if not an accepted file type', function () { const filesWrongType = { file: { @@ -42,8 +50,9 @@ describe('publishHelpers.js', function () { size: 10000000, }, }; - expect(publishHelpers.parsePublishApiRequestFiles.bind(this, filesWrongType)).to.throw(); + expect(parsePublishApiRequestFiles.bind(this, filesWrongType)).to.throw(); }); + it('should throw NO error if no problems', function () { const filesNoProblems = { file: { @@ -53,7 +62,7 @@ describe('publishHelpers.js', function () { size: 10000000, }, }; - expect(publishHelpers.parsePublishApiRequestFiles.bind(this, filesNoProblems)).to.not.throw(); + expect(parsePublishApiRequestFiles.bind(this, filesNoProblems)).to.not.throw(); }); });