2020-07-23 11:57:44 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
|
2020-11-15 19:41:35 +03:00
|
|
|
import JSZip from "jszip";
|
|
|
|
|
2021-07-07 23:50:57 +03:00
|
|
|
const USERNAME_REGEX = new RegExp("^[a-zA-Z0-9_]{0,}[a-zA-Z]+[0-9]*$");
|
2020-07-23 11:57:44 +03:00
|
|
|
const MIN_PASSWORD_LENGTH = 8;
|
2021-07-09 06:26:27 +03:00
|
|
|
const EMAIL_REGEX = /^[\w.!#$%&'*+\/=?^_`{|}~-]+@[-a-zA-Z0-9_.]+?\.[a-zA-Z]{2,50}$/;
|
2021-06-09 01:53:30 +03:00
|
|
|
const CONTAINS_DIGIT_REGEX = /\d/;
|
|
|
|
const CONTAINS_UPPERCASE_REGEX = /[A-Z]/;
|
|
|
|
const CONTAINS_LOWERCASE_REGEX = /[a-z]/;
|
|
|
|
const CONTAINS_SYMBOL_REGEX = /[ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/;
|
|
|
|
const PIN_REGEX = /^[0-9]{6}$/;
|
2021-07-07 23:50:57 +03:00
|
|
|
// const URL_REGEX = /(http(s)?:\/\/.)?(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)/;
|
|
|
|
const IFRAMELY_URL_REGEX = /^https?:\/\//i;
|
2020-08-07 02:28:54 +03:00
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
// TODO(jim): Regex should cover some of this.
|
|
|
|
const REJECT_LIST = [
|
2020-08-07 02:28:54 +03:00
|
|
|
"..",
|
2020-11-16 11:07:11 +03:00
|
|
|
"$",
|
|
|
|
"#",
|
2020-08-07 02:06:54 +03:00
|
|
|
"_",
|
2020-08-07 02:36:03 +03:00
|
|
|
"_next",
|
|
|
|
"next",
|
|
|
|
"webpack",
|
2020-08-19 09:15:14 +03:00
|
|
|
"system",
|
|
|
|
"experience",
|
2020-08-07 02:06:54 +03:00
|
|
|
"root",
|
|
|
|
"www",
|
|
|
|
"website",
|
|
|
|
"index",
|
|
|
|
"api",
|
|
|
|
"public",
|
|
|
|
"static",
|
|
|
|
"admin",
|
|
|
|
"administrator",
|
|
|
|
"webmaster",
|
2020-09-22 20:21:58 +03:00
|
|
|
"download",
|
|
|
|
"downloads",
|
2020-08-07 02:06:54 +03:00
|
|
|
"403",
|
|
|
|
"404",
|
|
|
|
"500",
|
2020-08-19 21:17:12 +03:00
|
|
|
"maintenance",
|
2020-09-21 22:39:36 +03:00
|
|
|
"guidelines",
|
2020-08-19 21:17:12 +03:00
|
|
|
"updates",
|
2020-08-07 02:06:54 +03:00
|
|
|
"login",
|
2020-08-07 02:28:54 +03:00
|
|
|
"authenticate",
|
2020-08-07 02:06:54 +03:00
|
|
|
"sign-in",
|
2020-08-07 02:28:54 +03:00
|
|
|
"sign_in",
|
2020-08-07 02:06:54 +03:00
|
|
|
"signin",
|
|
|
|
"log-in",
|
2020-08-07 02:28:54 +03:00
|
|
|
"log_in",
|
2020-08-07 02:06:54 +03:00
|
|
|
"logout",
|
2020-09-04 10:16:59 +03:00
|
|
|
"terms",
|
|
|
|
"terms-of-service",
|
|
|
|
"community",
|
|
|
|
"privacy",
|
|
|
|
"reset-password",
|
|
|
|
"reset",
|
|
|
|
"logout",
|
|
|
|
"dashboard",
|
|
|
|
"analytics",
|
|
|
|
"data",
|
2020-10-05 05:17:08 +03:00
|
|
|
"timeout",
|
|
|
|
"please-dont-use-timeout",
|
2020-08-07 02:06:54 +03:00
|
|
|
];
|
2020-07-23 11:57:44 +03:00
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
export const userRoute = (text) => {
|
|
|
|
if (!USERNAME_REGEX.test(text)) {
|
2020-07-23 11:57:44 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
if (REJECT_LIST.includes(text)) {
|
2020-07-23 11:57:44 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-09-06 05:42:30 +03:00
|
|
|
export const slatename = (text) => {
|
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text.length > 48) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
export const username = (text) => {
|
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-13 01:08:36 +03:00
|
|
|
if (text.length > 48 || text.length < 1) {
|
2020-09-06 05:42:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:36:03 +03:00
|
|
|
if (!userRoute(text)) {
|
2020-08-07 02:06:54 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
export const email = (text) => {
|
2020-07-23 11:57:44 +03:00
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
if (text.length > 254 || text.length < 5) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EMAIL_REGEX.test(text)) {
|
2020-07-23 11:57:44 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
//NOTE(toast): add this if the sendgrid plan is upgraded
|
|
|
|
// const sgEmailValidation = validateEmail({ email: text });
|
|
|
|
// if (sgEmailValidation.verdict !== "Valid") {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
return true;
|
|
|
|
};
|
2020-08-02 09:00:04 +03:00
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
// NOTE(amine): used to validate old users password (currently only used in signin)
|
|
|
|
export const legacyPassword = (text) => {
|
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
2020-09-08 08:23:22 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
if (text.length < MIN_PASSWORD_LENGTH) {
|
|
|
|
return false;
|
2020-09-08 08:23:22 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
return true;
|
|
|
|
};
|
2020-08-02 09:00:04 +03:00
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
export const passwordForm = (text) => {
|
|
|
|
const validations = {
|
|
|
|
validLength: false,
|
|
|
|
containsLowerCase: false,
|
|
|
|
containsUpperCase: false,
|
|
|
|
containsSymbol: false,
|
|
|
|
containsNumbers: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Strings.isEmpty(text)) return validations;
|
|
|
|
|
|
|
|
if (text.length >= MIN_PASSWORD_LENGTH) validations.validLength = true;
|
|
|
|
if (CONTAINS_DIGIT_REGEX.test(text)) validations.containsNumbers = true;
|
|
|
|
if (CONTAINS_LOWERCASE_REGEX.test(text)) validations.containsLowerCase = true;
|
|
|
|
if (CONTAINS_UPPERCASE_REGEX.test(text)) validations.containsUpperCase = true;
|
|
|
|
if (CONTAINS_SYMBOL_REGEX.test(text)) validations.containsSymbol = true;
|
|
|
|
|
|
|
|
return validations;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const password = (text) => {
|
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
2020-08-10 13:10:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
if (text.length <= MIN_PASSWORD_LENGTH) {
|
|
|
|
return false;
|
2020-08-04 06:15:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
let reqCount = 0;
|
|
|
|
|
|
|
|
if (CONTAINS_DIGIT_REGEX.test(text)) {
|
|
|
|
reqCount += 1;
|
|
|
|
}
|
|
|
|
if (CONTAINS_LOWERCASE_REGEX.test(text)) {
|
|
|
|
reqCount += 1;
|
|
|
|
}
|
|
|
|
if (CONTAINS_UPPERCASE_REGEX.test(text)) {
|
|
|
|
reqCount += 1;
|
|
|
|
}
|
|
|
|
if (CONTAINS_SYMBOL_REGEX.test(text)) {
|
|
|
|
reqCount += 1;
|
2020-08-02 09:00:04 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
return reqCount === 4;
|
|
|
|
};
|
|
|
|
|
2021-07-07 23:50:57 +03:00
|
|
|
export const endsWithAny = (options, string = "") =>
|
|
|
|
options.some((option) => {
|
|
|
|
if (string) {
|
|
|
|
return string.endsWith(option);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
export const verificationPin = (pin) => {
|
|
|
|
if (Strings.isEmpty(pin)) {
|
|
|
|
return false;
|
2020-08-02 10:29:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
return PIN_REGEX.test(pin);
|
2020-08-02 09:00:04 +03:00
|
|
|
};
|
2020-11-11 04:44:21 +03:00
|
|
|
|
2021-07-07 23:50:57 +03:00
|
|
|
export const link = (text) => {
|
|
|
|
if (Strings.isEmpty(text)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text.length < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IFRAMELY_URL_REGEX.test(text);
|
|
|
|
};
|
|
|
|
|
2020-11-11 04:44:21 +03:00
|
|
|
export const isPreviewableImage = (type = "") => {
|
|
|
|
if (type.startsWith("image/svg")) return false;
|
|
|
|
|
|
|
|
return type.startsWith("image/");
|
|
|
|
};
|
2020-11-15 19:41:35 +03:00
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isImageType = (type = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return type.startsWith("image/");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isAudioType = (type = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return type.startsWith("audio/");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isVideoType = (type = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return type.startsWith("video/");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isPdfType = (type = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return type.startsWith("application/pdf");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isEpubType = (type = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return type.startsWith("application/epub");
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isUnityType = (type = "") => {
|
|
|
|
return type === "application/unity";
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isFontFile = (fileName = "") => {
|
2021-07-07 23:50:57 +03:00
|
|
|
return endsWithAny([".ttf", ".otf", ".woff", ".woff2"], fileName.toLowerCase());
|
2021-05-04 15:31:12 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
export const isMarkdown = (filename = "", type = "") => {
|
2021-05-04 15:31:12 +03:00
|
|
|
return filename.toLowerCase().endsWith(".md") || type.startsWith("text/plain");
|
|
|
|
};
|
|
|
|
|
2020-11-15 19:41:35 +03:00
|
|
|
export const isUnityFile = async (file) => {
|
2020-11-16 05:31:18 +03:00
|
|
|
try {
|
|
|
|
const zip = new JSZip();
|
2020-11-15 19:41:35 +03:00
|
|
|
|
2020-11-16 05:31:18 +03:00
|
|
|
const contents = await zip.loadAsync(file);
|
|
|
|
const fileNames = Object.keys(contents.files);
|
2020-11-15 19:41:35 +03:00
|
|
|
|
2021-04-02 18:00:27 +03:00
|
|
|
// NOTE(daniel): every Unity game file will have this file
|
|
|
|
const isUnityLoaderFile = (fileName) =>
|
|
|
|
[/unityloader.js/i, /(.*)\.loader.js/i].some((item) => item.test(fileName));
|
2020-11-15 20:14:36 +03:00
|
|
|
|
2021-04-02 18:00:27 +03:00
|
|
|
return fileNames.some((file) => isUnityLoaderFile(file));
|
2020-11-16 05:31:18 +03:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-15 19:41:35 +03:00
|
|
|
};
|