From dcc7061c195f79ca71b4c764bc0e213b37c6576e Mon Sep 17 00:00:00 2001 From: Daniel Shleifman Date: Wed, 9 Jun 2021 20:38:00 +0300 Subject: [PATCH] torrent source regex added --- web/src/components/Add/AddDialog.jsx | 17 ++++++++++++++--- web/src/components/Add/helpers.js | 6 ++++++ web/src/components/Add/style.js | 4 ++-- web/src/locales/en/translation.json | 3 ++- web/src/locales/ru/translation.json | 5 +++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/web/src/components/Add/AddDialog.jsx b/web/src/components/Add/AddDialog.jsx index e87c2d0..f077d91 100644 --- a/web/src/components/Add/AddDialog.jsx +++ b/web/src/components/Add/AddDialog.jsx @@ -35,7 +35,7 @@ import { TorrentIconWrapper, RightSideContainer, } from './style' -import { checkImageURL, getMoviePosters } from './helpers' +import { checkImageURL, getMoviePosters, chechTorrentSource } from './helpers' export default function AddDialog({ handleClose }) { const { t } = useTranslation() @@ -44,6 +44,7 @@ export default function AddDialog({ handleClose }) { const [title, setTitle] = useState('') const [posterUrl, setPosterUrl] = useState('') const [isPosterUrlCorrect, setIsPosterUrlCorrect] = useState(false) + const [isTorrentSourceCorrect, setIsTorrentSourceCorrect] = useState(false) const [posterList, setPosterList] = useState() const [isUserInteractedWithPoster, setIsUserInteractedWithPoster] = useState(false) const [isUserInteractedWithTitle, setIsUserInteractedWithTitle] = useState(false) @@ -102,6 +103,10 @@ export default function AddDialog({ handleClose }) { }) }, [selectedFile, delayedPosterSearch, torrentSource, posterSearchLanguage, isUserInteractedWithTitle]) + useEffect(() => { + setIsTorrentSourceCorrect(chechTorrentSource(torrentSource)) + }, [torrentSource]) + const handleCapture = files => { const [file] = files if (!file) return @@ -214,7 +219,7 @@ export default function AddDialog({ handleClose }) { - + - + diff --git a/web/src/components/Add/helpers.js b/web/src/components/Add/helpers.js index 215d2f8..2648b45 100644 --- a/web/src/components/Add/helpers.js +++ b/web/src/components/Add/helpers.js @@ -21,3 +21,9 @@ export const checkImageURL = async url => { return false } } + +const magnetRegex = /magnet:\?xt=urn:[a-z0-9]{20,50}/i +const hashRegex = /\b[0-9a-f]{32}\b|\b[0-9a-f]{40}\b|\b[0-9a-f]{64}\b/i +const torrentRegex = /^.*\.(torrent)$/i +export const chechTorrentSource = source => + source.match(hashRegex) !== null || source.match(magnetRegex) !== null || source.match(torrentRegex) !== null diff --git a/web/src/components/Add/style.js b/web/src/components/Add/style.js index 9f586b2..b262263 100644 --- a/web/src/components/Add/style.js +++ b/web/src/components/Add/style.js @@ -30,7 +30,7 @@ export const RightSide = styled.div` ` export const RightSideContainer = styled.div` - ${({ isHidden, notificationMessage }) => css` + ${({ isHidden, notificationMessage, isError }) => css` height: 455px; ${notificationMessage && @@ -44,7 +44,7 @@ export const RightSideContainer = styled.div` content: '${notificationMessage}'; display: grid; place-items: center; - background: #84cda7; + background: ${isError ? '#cda184' : '#84cda7'}; padding: 10px 15px; position: absolute; top: 50%; diff --git a/web/src/locales/en/translation.json b/web/src/locales/en/translation.json index 536a2fa..4e8acca 100644 --- a/web/src/locales/en/translation.json +++ b/web/src/locales/en/translation.json @@ -103,5 +103,6 @@ }, "TorrentSourceOptions": "magnet / hash / .torrent file link", "Clear": "Clear", - "AddTorrentSourceNotification": "First add your torrent source" + "AddTorrentSourceNotification": "First add your torrent source", + "WrongTorrentSource": "Wrong torrent source" } \ No newline at end of file diff --git a/web/src/locales/ru/translation.json b/web/src/locales/ru/translation.json index d0a9841..fefc90e 100644 --- a/web/src/locales/ru/translation.json +++ b/web/src/locales/ru/translation.json @@ -101,7 +101,8 @@ "Or": "ИЛИ", "ClickOrDrag": "НАЖМИТЕ / ПЕРЕТАЩИТЕ ФАЙЛ (.torrent)" }, - "TorrentSourceOptions": "магнитная ссылка / хеш / ссылка на .torrent файл", + "TorrentSourceOptions": "magnet ссылка / хеш / ссылка на .torrent файл", "Clear": "Очистить", - "AddTorrentSourceNotification": "Сначала добавьте торрент источник" + "AddTorrentSourceNotification": "Сначала добавьте торрент источник", + "WrongTorrentSource": "Неправильный torrent источник" } \ No newline at end of file