feat: torrent player
This commit is contained in:
1 parent
6788dea0cd
commit
b379bc229d
7 files changed
+4170
-17
No files matched your search
@@ -0,0 +1,54 @@
|
||||
'use client';
|
||||
|
||||
import { FC, useEffect, useRef } from 'react';
|
||||
import { InstanceProps } from 'react-modal-promise';
|
||||
import video from 'video.js';
|
||||
import WebTorrent from 'webtorrent/dist/webtorrent.min.js';
|
||||
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
|
||||
import 'video.js/dist/video-js.css';
|
||||
|
||||
interface Props extends InstanceProps<void> {
|
||||
magnet: string;
|
||||
}
|
||||
|
||||
const Player: FC<Props> = ({ onResolve, magnet }) => {
|
||||
const videoRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let skip = false;
|
||||
const client = new WebTorrent();
|
||||
|
||||
navigator.serviceWorker.register('./sw.min.js', { scope: './' }).then((registration) => {
|
||||
const worker = registration.active || registration.waiting || registration.installing;
|
||||
|
||||
const createServer = (worker: ServiceWorker | null) => {
|
||||
if (worker?.state !== 'activated' || skip) return false;
|
||||
|
||||
client.createServer({ controller: registration });
|
||||
|
||||
console.log(client);
|
||||
};
|
||||
|
||||
if (!createServer(worker)) {
|
||||
worker?.addEventListener('statechange', ({ target }) => createServer(target as ServiceWorker));
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
skip = true;
|
||||
client.removeAllListeners();
|
||||
client.destroy();
|
||||
navigator.serviceWorker.getRegistration('./sw.min.js').then((registration) => registration?.unregister());
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Modal className='block p-0 max-w-[930px] border-none bg-black' style={{ aspectRatio: '16/9' }} onClose={onResolve}>
|
||||
<div ref={videoRef} className='w-full h-full' />
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
export default Player;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { create } from 'react-modal-promise';
|
||||
import dynamic from 'next/dynamic';
|
||||
|
||||
const Player = dynamic(() => import('./Player'), { ssr: false });
|
||||
|
||||
const showPlayer = create(Player);
|
||||
|
||||
export default showPlayer;
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { Fragment, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { Cast, Copy, Loader, Magnet } from 'lucide-react';
|
||||
import { Cast, Copy, Loader, Magnet, Play } from 'lucide-react';
|
||||
|
||||
import { Sort } from '@/api/parsers';
|
||||
import { Button } from '@/components/ui/button';
|
||||
@@ -9,6 +9,7 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts';
|
||||
|
||||
import showPlayer from './components/Player';
|
||||
import TableHeadSortable from './components/TableHeadSortable';
|
||||
import { providers } from './constants';
|
||||
import showHostManagerModal from './HostManager';
|
||||
@@ -101,6 +102,12 @@ const showTorrentsModal = create(({ title, year, showType, showId, onResolve }:
|
||||
<TableCell className='p-2'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='text-center p-1 pr-2'>
|
||||
<div className='flex gap-2 justify-end'>
|
||||
<div
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg cursor-pointer'
|
||||
onClick={() => showPlayer({ magnet: torrent.magnet })}
|
||||
>
|
||||
<Play size={20} />
|
||||
</div>
|
||||
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||
<Magnet size={20} />
|
||||
</a>
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
declare module 'webtorrent/dist/webtorrent.min.js' {
|
||||
import WebTorrent = require('webtorrent');
|
||||
|
||||
export = WebTorrent;
|
||||
}
|
||||
Generated
+4089
-15
File diff suppressed because it is too large.
Load diff
+4
-1
@@ -36,13 +36,16 @@
|
||||
"tailwind-merge": "^1.14.0",
|
||||
"tailwindcss-animate": "^1.0.6",
|
||||
"typescript": "5.1.6",
|
||||
"vaul": "^0.8.9"
|
||||
"vaul": "^0.8.9",
|
||||
"video.js": "^8.17.4",
|
||||
"webtorrent": "^2.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-cookie": "^3.0.3",
|
||||
"@types/node": "20.4.4",
|
||||
"@types/react": "18.2.15",
|
||||
"@types/react-dom": "18.2.7",
|
||||
"@types/webtorrent": "^0.109.8",
|
||||
"autoprefixer": "^10.4.14",
|
||||
"eslint-plugin-simple-import-sort": "^10.0.0",
|
||||
"postcss": "^8.4.27",
|
||||
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
(()=>{"use strict";let e=!1;self.addEventListener("install",(()=>{self.skipWaiting()})),self.addEventListener("fetch",(s=>{const t=(s=>{const{url:t}=s.request;return t.includes(self.registration.scope+"webtorrent/")?t.includes(self.registration.scope+"webtorrent/keepalive/")?new Response:t.includes(self.registration.scope+"webtorrent/cancel/")?new Response(new ReadableStream({cancel(){e=!0}})):async function({request:s}){const{url:t,method:n,headers:o,destination:a}=s,l=await clients.matchAll({type:"window",includeUncontrolled:!0}),[r,i]=await new Promise((e=>{for(const s of l){const l=new MessageChannel,{port1:r,port2:i}=l;r.onmessage=({data:s})=>{e([s,r])},s.postMessage({url:t,method:n,headers:Object.fromEntries(o.entries()),scope:self.registration.scope,destination:a,type:"webtorrent"},[i])}}));let c=null;const d=()=>{i.postMessage(!1),clearTimeout(c),i.onmessage=null};return"STREAM"!==r.body?(d(),new Response(r.body,r)):new Response(new ReadableStream({pull:s=>new Promise((t=>{i.onmessage=({data:e})=>{e?s.enqueue(e):(d(),s.close()),t()},e||(clearTimeout(c),"document"!==a&&(c=setTimeout((()=>{d(),t()}),5e3))),i.postMessage(!0)})),cancel(){d()}}),r)}(s):null})(s);t&&s.respondWith(t)})),self.addEventListener("activate",(()=>{self.clients.claim()}))})();
|
||||
//# sourceMappingURL=sw.min.js.map
|
||||
Reference in new issue
Block a user