2017-12-14 23:25:51 +03:00
|
|
|
const url = require('url'),
|
|
|
|
_ = require('lodash');
|
|
|
|
|
2017-12-14 00:06:31 +03:00
|
|
|
let _private = {};
|
|
|
|
|
|
|
|
_private.removeDoubleCharacters = function removeDoubleCharacters(character, string) {
|
|
|
|
let stringArray = string.split('');
|
2016-08-23 14:47:59 +03:00
|
|
|
|
|
|
|
return stringArray.reduce(function (newString, currentCharacter, index) {
|
|
|
|
if (
|
|
|
|
currentCharacter === character &&
|
|
|
|
stringArray[index + 1] === character
|
|
|
|
) {
|
|
|
|
return newString;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newString + currentCharacter;
|
|
|
|
}, '');
|
2017-12-14 00:06:31 +03:00
|
|
|
};
|
2016-08-23 14:47:59 +03:00
|
|
|
|
2017-12-14 00:06:31 +03:00
|
|
|
module.exports.removeOpenRedirectFromUrl = function removeOpenRedirectFromUrl(urlString) {
|
|
|
|
let parsedUrl = url.parse(urlString);
|
2016-08-23 14:47:59 +03:00
|
|
|
|
|
|
|
return (
|
2017-11-01 16:44:54 +03:00
|
|
|
// http://
|
|
|
|
(parsedUrl.protocol ? parsedUrl.protocol + '//' : '') +
|
2016-08-23 14:47:59 +03:00
|
|
|
(parsedUrl.auth || '') +
|
|
|
|
(parsedUrl.host || '') +
|
2017-12-14 00:06:31 +03:00
|
|
|
_private.removeDoubleCharacters('/', parsedUrl.path) +
|
2016-08-23 14:47:59 +03:00
|
|
|
(parsedUrl.hash || '')
|
|
|
|
);
|
2017-12-14 00:06:31 +03:00
|
|
|
};
|
2017-12-14 23:25:51 +03:00
|
|
|
|
|
|
|
module.exports.checkFileExists = function checkFileExists(fileData) {
|
|
|
|
return !!(fileData.mimetype && fileData.path);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.checkFileIsValid = function checkFileIsValid(fileData, types, extensions) {
|
2018-08-30 19:30:36 +03:00
|
|
|
const type = fileData.mimetype;
|
2017-12-14 23:25:51 +03:00
|
|
|
|
2018-08-30 19:30:36 +03:00
|
|
|
if (_.includes(types, type) && _.includes(extensions, fileData.ext)) {
|
2017-12-14 23:25:51 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|