diff --git a/controllers/publishController.js b/controllers/publishController.js index a9088181..89db3768 100644 --- a/controllers/publishController.js +++ b/controllers/publishController.js @@ -63,4 +63,18 @@ module.exports = { }); return deferred; }, + checkNameAvailability: (name) => { + const deferred = new Promise((resolve, reject) => { + // find any records where the name is used + db.File + .findAll({ where: { name } }) + .then(result => { + resolve(result); + }) + .catch(error => { + reject(error); + }); + }); + return deferred; + }, }; diff --git a/public/assets/js/claimPublish.js b/public/assets/js/claimPublish.js index 70d90dc6..df8df752 100644 --- a/public/assets/js/claimPublish.js +++ b/public/assets/js/claimPublish.js @@ -83,7 +83,7 @@ document.getElementById('publish-submit').addEventListener('click', function(eve event.preventDefault(); var name = document.getElementById('publish-name').value; var invalidCharacters = /[^A-Za-z0-9,-]/.exec(name); - // validate 'name' + // validate 'name' field if (invalidCharacters) { alert(invalidCharacters + ' is not allowed. A-Z, a-z, 0-9, and "-" only.'); return; @@ -91,28 +91,41 @@ document.getElementById('publish-submit').addEventListener('click', function(eve alert("You must enter a name for your claim"); return; } - // make sure a file was selected - if (stagedFiles) { - // make sure only 1 file was selected - if (stagedFiles.length > 1) { - alert("Only one file is allowed at a time"); - return; - } - // make sure the content type is acceptable - switch (stagedFiles[0].type) { - case "image/png": - case "image/jpeg": - case "image/gif": - case "video/mp4": - uploader.submitFiles(stagedFiles); - break; - default: - alert("Only .png, .jpeg, .gif, and .mp4 files are currently supported"); - break; - } - } else { + // make sure only 1 file was selected + if (!stagedFiles) { alert("Please select a file"); + return; + } else if (stagedFiles.length > 1) { + alert("Only one file is allowed at a time"); + return; } + // make sure the content type is acceptable + switch (stagedFiles[0].type) { + case "image/png": + case "image/jpeg": + case "image/gif": + case "video/mp4": + break; + default: + alert("Only .png, .jpeg, .gif, and .mp4 files are currently supported"); + return; + } + // make sure the name is available + var xhttp; + xhttp = new XMLHttpRequest(); + xhttp.open('GET', '/api/isClaimAvailable/' + name, true); + xhttp.responseType = 'json'; + xhttp.onreadystatechange = function() { + if (this.readyState == 4 ) { + if ( this.status == 200) { + console.log(this.response); + //uploader.submitFiles(stagedFiles); + } else { + console.log("request to check claim name failed with status:", this.status); + }; + } + }; + xhttp.send(); }) /* socketio-file-upload listeners */ diff --git a/routes/api-routes.js b/routes/api-routes.js index 411c7343..fb76a244 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -9,14 +9,14 @@ const { postToStats, sendGoogleAnalytics } = require('../controllers/statsContro module.exports = app => { // route to run a claim_list request on the daemon - app.get('/api/claim_list/:claim', ({ ip, originalUrl, params }, res) => { + app.get('/api/claim_list/:name', ({ ip, originalUrl, params }, res) => { // google analytics sendGoogleAnalytics('serve', ip, originalUrl); // log logger.verbose(`GET request on ${originalUrl} from ${ip}`); // serve the content lbryApi - .getClaimsList(params.claim) + .getClaimsList(params.name) .then(claimsList => { postToStats('serve', originalUrl, ip, 'success'); res.status(200).json(claimsList); @@ -25,6 +25,24 @@ module.exports = app => { errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); }); }); + // route to check whether spee.ch has published to a claim + app.get('/api/isClaimAvailable/:name', ({ ip, originalUrl, params }, res) => { + // log + logger.verbose(`GET request on ${originalUrl} from ${ip}`); + // send response + publishController + .checkNameAvailability(params.name) + .then(result => { + if (result.length >= 1) { + res.status(200).json(false); + } else { + res.status(200).json(true); + } + }) + .catch(error => { + res.status(500).json(error); + }); + }); // route to run a resolve request on the daemon app.get('/api/resolve/:uri', ({ ip, originalUrl, params }, res) => { // google analytics