enh: URL checking for import web addresses (#2377)

* enh: URL checking for import web addresses

* Add the source for Regex

* Removing the console.log for production
This commit is contained in:
Kush Trivedi 2020-03-12 03:52:49 +05:30 committed by GitHub
parent 8b94b142cb
commit 3fd3faca11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -80,7 +80,7 @@
"core-index-import/warning-data-file": "You must specify a data file to import.",
"core-index-import/uploading-data": "Uploading data…",
"core-index-import/web-address": "Web Addresses (URLs)",
"core-index-import/warning-web-address": "You must specify a web address (URL) to import.",
"core-index-import/warning-web-address": "You must specify a valid web address (URL) to import.",
"core-index-import/downloading-data": "Downloading data…",
"core-index-import/clipboard": "Clipboard",
"core-index-import/warning-clipboard": "You must paste some data to import.",

View File

@ -34,6 +34,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function ThisComputerImportingSourceUI(controller) {
this._controller = controller;
}
// Function to check if the URL getting entered is valid or not
function isUrlValid(url) {
// regex for a valid URL pattern
// Derived from the jquery-validation repository https://github.com/jquery-validation/jquery-validation/blob/master/src/additional/url2.js
return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url);
}
Refine.DefaultImportingController.sources.push({
"label": $.i18n('core-index-import/this-computer'),
"id": "upload",
@ -83,7 +90,7 @@ UrlImportingSourceUI.prototype.attachUI = function(bodyDiv) {
this._elmts.nextButton.html($.i18n('core-buttons/next'));
this._elmts.nextButton.click(function(evt) {
if ($.trim(self._elmts.urlInput[0].value).length === 0) {
if(!isUrlValid(self._elmts.urlInput[0].value)) {
window.alert($.i18n('core-index-import/warning-web-address'));
} else {
self._controller.startImportJob(self._elmts.form, $.i18n('core-index-import/downloading-data'));