add better video path string encoding/decoding
This commit is contained in:
parent
c98ad299cc
commit
7f0fc385a1
|
@ -6,13 +6,15 @@ import Video from "@/components/Pages/Video";
|
||||||
|
|
||||||
import { fetchVideoData } from "@/utils/scrape/video";
|
import { fetchVideoData } from "@/utils/scrape/video";
|
||||||
|
|
||||||
|
import { decodeVideoUrlPath } from '@/utils/string';
|
||||||
|
|
||||||
import { useLocale } from 'next-intl';
|
import { useLocale } from 'next-intl';
|
||||||
|
|
||||||
export default async function VideoPage({ params }: { params: { id: string } }) {
|
export default async function VideoPage({ params }: { params: { id: string } }) {
|
||||||
|
|
||||||
const locale = useLocale()
|
const locale = useLocale()
|
||||||
|
|
||||||
const decodedId = decodeURIComponent(params.id)
|
const decodedId = decodeVideoUrlPath(params.id)
|
||||||
|
|
||||||
const [data, related] = await fetchVideoData(decodedId)
|
const [data, related] = await fetchVideoData(decodedId)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import classNames from 'classnames';
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
|
||||||
import style from './Thumbnail.module.scss'
|
import style from './Thumbnail.module.scss'
|
||||||
|
import { encodeVideoUrlPath } from '@/utils/string';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
locale: string
|
locale: string
|
||||||
|
@ -22,7 +23,7 @@ const Thumbnail: React.FC<Props> = (props) => {
|
||||||
|
|
||||||
const { locale, videoUrl, imgUrl, text, show } = props
|
const { locale, videoUrl, imgUrl, text, show } = props
|
||||||
|
|
||||||
const encodedUri = encodeURIComponent(videoUrl)
|
const encodedUri = encodeVideoUrlPath(videoUrl)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(style.thumbnailContainer, { [style.show]: show } )}>
|
<div className={classNames(style.thumbnailContainer, { [style.show]: show } )}>
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
import { GalleryData } from "@/meta/data";
|
import { GalleryData } from "@/meta/data";
|
||||||
|
|
||||||
export const findVideoUrlInsideTagStringByFunctionNameAndExtension = (
|
export const findVideoUrlInsideTagStringByFunctionNameAndExtension = (
|
||||||
tagBlock: string, functionName: string, extension: string): string|null => {
|
tagBlock: string, functionName: string, extension: string): string | null => {
|
||||||
const start = tagBlock.indexOf(`html5player.${functionName}('`) + `html5player.${functionName}('`.length;
|
const start = tagBlock.indexOf(`html5player.${functionName}('`) + `html5player.${functionName}('`.length;
|
||||||
const end = tagBlock.toString().indexOf("'", start);
|
const end = tagBlock.toString().indexOf("'", start);
|
||||||
|
|
||||||
const substr = tagBlock.substring(start, end);
|
const substr = tagBlock.substring(start, end);
|
||||||
|
|
||||||
if (substr.includes(extension)) {
|
if (substr.includes(extension)) {
|
||||||
return substr
|
return substr
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export const findRelatedVideos = (tagBlock: string): GalleryData[]|null => {
|
export const findRelatedVideos = (tagBlock: string): GalleryData[] | null => {
|
||||||
if (!(tagBlock.includes('video_related=['))) {
|
if (!(tagBlock.includes('video_related=['))) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -22,13 +22,13 @@ export const findRelatedVideos = (tagBlock: string): GalleryData[]|null => {
|
||||||
// Trova l'inizio e la fine dell'array di oggetti nell'input
|
// Trova l'inizio e la fine dell'array di oggetti nell'input
|
||||||
const start = tagBlock.indexOf('[{');
|
const start = tagBlock.indexOf('[{');
|
||||||
const end = tagBlock.lastIndexOf('}]') + 2;
|
const end = tagBlock.lastIndexOf('}]') + 2;
|
||||||
|
|
||||||
// Estrai la sottostringa contenente l'array di oggetti
|
// Estrai la sottostringa contenente l'array di oggetti
|
||||||
const jsonString = tagBlock.substring(start, end);
|
const jsonString = tagBlock.substring(start, end);
|
||||||
|
|
||||||
// Parsea la stringa JSON in un array di oggetti
|
// Parsea la stringa JSON in un array di oggetti
|
||||||
const videoRelatedArray = JSON.parse(jsonString);
|
const videoRelatedArray = JSON.parse(jsonString);
|
||||||
|
|
||||||
// Mappa ogni oggetto nell'array per rinominare le chiavi
|
// Mappa ogni oggetto nell'array per rinominare le chiavi
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
const parsedArray = videoRelatedArray.map(obj => ({
|
const parsedArray = videoRelatedArray.map(obj => ({
|
||||||
|
@ -37,15 +37,23 @@ export const findRelatedVideos = (tagBlock: string): GalleryData[]|null => {
|
||||||
imgUrl: obj.i,
|
imgUrl: obj.i,
|
||||||
text: obj.tf
|
text: obj.tf
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return parsedArray;
|
return parsedArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const removeHttpS = (url: string): string => {
|
export const removeHttpS = (url: string): string => {
|
||||||
if (url.startsWith("http://")) {
|
if (url.startsWith("http://")) {
|
||||||
return url.slice(7);
|
return url.slice(7);
|
||||||
} else if (url.startsWith("https://")) {
|
} else if (url.startsWith("https://")) {
|
||||||
return url.slice(8);
|
return url.slice(8);
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const encodeVideoUrlPath = (input: string): string => {
|
||||||
|
return input.replace(/^\/+/, '').replace(/\/+$/, '').replace(/\//g, "-");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decodeVideoUrlPath = (input: string): string => {
|
||||||
|
return `/${input.replace(/^\-+/, '').replace(/\-+$/, '').replace(/\-/g, "/")}`;
|
||||||
|
};
|
Loading…
Reference in New Issue