Skip to content

Commit

Permalink
update valid text
Browse files Browse the repository at this point in the history
  • Loading branch information
cuinjune committed May 6, 2021
1 parent 4e70e9b commit 4679f39
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
18 changes: 7 additions & 11 deletions models/project.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mongoose = require("mongoose");
const config = require("./../config/config");
const ASCIIFolder = require("fold-to-ascii/lib/ascii-folder");
const AWS = require("aws-sdk");
const Stream = require("stream");
const path = require("path");
Expand Down Expand Up @@ -291,10 +292,6 @@ projectSchema.statics.getValidText = function (text) {
if (!newText.length) {
return newText;
}
if (/&/.test(newText)) {
// replace all '&' with 'and'
newText = newText.replace(/\s&\s|&/g, " and ").trim();
}
if (/[[\]]/.test(newText)) {
// remove all square brackets including the text inside
newText = newText.replace(/(\[.*?\])/g, "").replace(/[[\]]/g, "").trim();
Expand All @@ -307,18 +304,17 @@ projectSchema.statics.getValidText = function (text) {
// remove all angle brackets including the text inside
newText = newText.replace(/(<.*?>)/g, "").replace(/[<>]/g, "").trim();
}
if (/&/.test(newText)) {
// replace all '&' with 'and'
newText = newText.replace(/\s&\s|&/g, " and ").trim();
}
if (/[^\x20-\x7E]/.test(newText)) {
// replace all non-ascii characters to space
newText = newText.replace(/[^\x20-\x7E]/g, " ").trim();
// replace all non-ascii characters to the closest ascii characters
newText = ASCIIFolder.foldReplacing(newText).replace(/[^\x20-\x7E]/g, "").trim();
}
return newText;
}

// validate text
projectSchema.statics.validateText = function (text) {
return typeof text === "string" && text.length >= projectSchema.obj.text.minlength && text.length <= projectSchema.obj.text.maxlength;
}

// validate voice
projectSchema.statics.validateVoice = function (voice) {
return typeof voice === "string" && projectSchema.obj.voice.enum.includes(voice);
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"ejs": "^3.1.6",
"email-validator": "^2.0.4",
"express": "^4.17.1",
"fold-to-ascii": "^5.0.0",
"geoip-lite": "^1.4.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.2",
Expand Down
9 changes: 1 addition & 8 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,8 @@ router.put("/api/v1/project/:urlKey", auth, (req, res) => {
const { text, voice, speed, subtitle } = req.body;
const updatedData = { text: project.text, voice: project.voice, speed: project.speed, subtitle: project.subtitle };
if (typeof text === "string") {
const newText = Project.getValidText(text);
const maxNumCharacters = req.user.role === "basic" ? 3000 : 12000;
if (newText.length > maxNumCharacters) {
return res.status(400).json({ auth: false, message: `You have exceeded the maximum number of characters allowed (${maxNumCharacters}) for your account` });
}
if (!Project.validateText(newText)) {
return res.status(400).json({ auth: false, message: "Invalid input text" });
}
updatedData.text = newText;
updatedData.text = Project.getValidText(text).substr(0, maxNumCharacters);
}
if (voice) {
if (!Project.validateVoice(voice)) {
Expand Down
63 changes: 32 additions & 31 deletions views/edit.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@
const clearText = document.getElementById("main-form-textarea-clear");
const video = document.getElementById("main-video");
const iframe = document.getElementById("main-video-iframe");
const maxNumCharacters = "<%= role %>" === "basic" ? 3000 : 12000;
const updateCharsUsed = () => {
charsUsed.innerText = `${textArea.value.length.toLocaleString("en-US")} characters used`;
charsUsed.style.color = textArea.value.length <= maxNumCharacters ? "rgb(125, 125, 125)" : "rgb(226, 83, 79)";
if (textArea.value.length) {
viviify.style.backgroundColor = "rgb(49, 243, 202)";
viviify.style.pointerEvents = "auto";
clearText.style.color = "rgb(112, 146, 255)";
clearText.style.pointerEvents = "auto";
}
else {
viviify.style.backgroundColor = "rgb(175, 175, 175)";
viviify.style.pointerEvents = "none";
clearText.style.color = "rgb(175, 175, 175)";
clearText.style.pointerEvents = "none";
}
}
updateCharsUsed();
logout.addEventListener("click", () => {
fetch("/api/v1/user/logout", {
Expand Down Expand Up @@ -134,14 +153,6 @@
return false;
}
textArea.value = textArea.value.trim();
if (/&/.test(textArea.value)) {
const result = confirm("The symbol '&' cannot be used.\nDo you want to replace them with 'and'?");
if (!result) {
return false;
}
// replace all '&' with 'and'
textArea.value = textArea.value.replace(/\s&\s|&/g, " and ").trim();
}
if (/[[\]]/.test(textArea.value)) {
const result = confirm("Square brackets cannot be used.\nDo you want to remove them including the text inside?");
if (!result) {
Expand All @@ -166,14 +177,23 @@
// remove all angle brackets including the text inside
textArea.value = textArea.value.replace(/(<.*?>)/g, "").replace(/[<>]/g, "").trim();
}
if (/[^\x20-\x7E]/.test(textArea.value)) {
const result = confirm("Non-ascii characters cannot be used.\nDo you want to remove them?");
if (/&/.test(textArea.value)) {
const result = confirm("The symbol '&' cannot be used.\nDo you want to replace them with 'and'?");
if (!result) {
return false;
}
// replace all '&' with 'and'
textArea.value = textArea.value.replace(/\s&\s|&/g, " and ").trim();
}
if (textArea.value.length > maxNumCharacters) {
const result = confirm(`You have exceeded the maximum characters allowed (${maxNumCharacters.toLocaleString("en-US")}) for your account. Do you want to remove the exceeded characters?`);
if (!result) {
return false;
}
// replace all non-ascii characters to space
textArea.value = textArea.value.replace(/[^\x20-\x7E]/g, " ").trim();
// remove exceeded characters
textArea.value = textArea.value.substr(0, maxNumCharacters);
}
updateCharsUsed();
saved.innerText = "Saving...";
return true;
}
Expand Down Expand Up @@ -228,25 +248,6 @@
}
});
const updateCharsUsed = () => {
charsUsed.innerText = `${textArea.value.length} characters used`;
const maxNumCharacters = "<%= role %>" === "basic" ? 3000 : 12000;
charsUsed.style.color = textArea.value.length <= maxNumCharacters ? "rgb(125, 125, 125)" : "rgb(226, 83, 79)";
if (textArea.value.length) {
viviify.style.backgroundColor = "rgb(49, 243, 202)";
viviify.style.pointerEvents = "auto";
clearText.style.color = "rgb(112, 146, 255)";
clearText.style.pointerEvents = "auto";
}
else {
viviify.style.backgroundColor = "rgb(175, 175, 175)";
viviify.style.pointerEvents = "none";
clearText.style.color = "rgb(175, 175, 175)";
clearText.style.pointerEvents = "none";
}
}
updateCharsUsed();
textArea.addEventListener("input", () => {
updateCharsUsed();
});
Expand Down

0 comments on commit 4679f39

Please sign in to comment.