torrent source regex added

This commit is contained in:
Daniel Shleifman
2021-06-09 20:38:00 +03:00
parent 388416289e
commit dcc7061c19
5 changed files with 27 additions and 8 deletions

View File

@@ -35,7 +35,7 @@ import {
TorrentIconWrapper, TorrentIconWrapper,
RightSideContainer, RightSideContainer,
} from './style' } from './style'
import { checkImageURL, getMoviePosters } from './helpers' import { checkImageURL, getMoviePosters, chechTorrentSource } from './helpers'
export default function AddDialog({ handleClose }) { export default function AddDialog({ handleClose }) {
const { t } = useTranslation() const { t } = useTranslation()
@@ -44,6 +44,7 @@ export default function AddDialog({ handleClose }) {
const [title, setTitle] = useState('') const [title, setTitle] = useState('')
const [posterUrl, setPosterUrl] = useState('') const [posterUrl, setPosterUrl] = useState('')
const [isPosterUrlCorrect, setIsPosterUrlCorrect] = useState(false) const [isPosterUrlCorrect, setIsPosterUrlCorrect] = useState(false)
const [isTorrentSourceCorrect, setIsTorrentSourceCorrect] = useState(false)
const [posterList, setPosterList] = useState() const [posterList, setPosterList] = useState()
const [isUserInteractedWithPoster, setIsUserInteractedWithPoster] = useState(false) const [isUserInteractedWithPoster, setIsUserInteractedWithPoster] = useState(false)
const [isUserInteractedWithTitle, setIsUserInteractedWithTitle] = useState(false) const [isUserInteractedWithTitle, setIsUserInteractedWithTitle] = useState(false)
@@ -102,6 +103,10 @@ export default function AddDialog({ handleClose }) {
}) })
}, [selectedFile, delayedPosterSearch, torrentSource, posterSearchLanguage, isUserInteractedWithTitle]) }, [selectedFile, delayedPosterSearch, torrentSource, posterSearchLanguage, isUserInteractedWithTitle])
useEffect(() => {
setIsTorrentSourceCorrect(chechTorrentSource(torrentSource))
}, [torrentSource])
const handleCapture = files => { const handleCapture = files => {
const [file] = files const [file] = files
if (!file) return if (!file) return
@@ -214,7 +219,7 @@ export default function AddDialog({ handleClose }) {
</LeftSide> </LeftSide>
<RightSide> <RightSide>
<RightSideContainer isHidden={!torrentSource}> <RightSideContainer isHidden={!isTorrentSourceCorrect}>
<TextField <TextField
onChange={handleTitleChange} onChange={handleTitleChange}
value={title} value={title}
@@ -279,7 +284,13 @@ export default function AddDialog({ handleClose }) {
</PosterWrapper> </PosterWrapper>
</RightSideContainer> </RightSideContainer>
<RightSideContainer notificationMessage={t('AddTorrentSourceNotification')} isHidden={torrentSource} /> <RightSideContainer
isError={torrentSource && !isTorrentSourceCorrect}
notificationMessage={
!torrentSource ? t('AddTorrentSourceNotification') : !isTorrentSourceCorrect && t('WrongTorrentSource')
}
isHidden={isTorrentSourceCorrect}
/>
</RightSide> </RightSide>
</Content> </Content>

View File

@@ -21,3 +21,9 @@ export const checkImageURL = async url => {
return false 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

View File

@@ -30,7 +30,7 @@ export const RightSide = styled.div`
` `
export const RightSideContainer = styled.div` export const RightSideContainer = styled.div`
${({ isHidden, notificationMessage }) => css` ${({ isHidden, notificationMessage, isError }) => css`
height: 455px; height: 455px;
${notificationMessage && ${notificationMessage &&
@@ -44,7 +44,7 @@ export const RightSideContainer = styled.div`
content: '${notificationMessage}'; content: '${notificationMessage}';
display: grid; display: grid;
place-items: center; place-items: center;
background: #84cda7; background: ${isError ? '#cda184' : '#84cda7'};
padding: 10px 15px; padding: 10px 15px;
position: absolute; position: absolute;
top: 50%; top: 50%;

View File

@@ -103,5 +103,6 @@
}, },
"TorrentSourceOptions": "magnet / hash / .torrent file link", "TorrentSourceOptions": "magnet / hash / .torrent file link",
"Clear": "Clear", "Clear": "Clear",
"AddTorrentSourceNotification": "First add your torrent source" "AddTorrentSourceNotification": "First add your torrent source",
"WrongTorrentSource": "Wrong torrent source"
} }

View File

@@ -101,7 +101,8 @@
"Or": "ИЛИ", "Or": "ИЛИ",
"ClickOrDrag": "НАЖМИТЕ / ПЕРЕТАЩИТЕ ФАЙЛ (.torrent)" "ClickOrDrag": "НАЖМИТЕ / ПЕРЕТАЩИТЕ ФАЙЛ (.torrent)"
}, },
"TorrentSourceOptions": "магнитная ссылка / хеш / ссылка на .torrent файл", "TorrentSourceOptions": "magnet ссылка / хеш / ссылка на .torrent файл",
"Clear": "Очистить", "Clear": "Очистить",
"AddTorrentSourceNotification": "Сначала добавьте торрент источник" "AddTorrentSourceNotification": "Сначала добавьте торрент источник",
"WrongTorrentSource": "Неправильный torrent источник"
} }