mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-05-05 02:29:03 +00:00
Merge branch 'feat/player-local-subtitles' of https://github.com/Stremio/stremio-web into feat/player-local-subtitles
This commit is contained in:
commit
caf9ba1c7f
5 changed files with 39 additions and 9 deletions
|
|
@ -41,6 +41,11 @@ const ICON_FOR_TYPE = new Map([
|
||||||
['other', 'movies'],
|
['other', 'movies'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const MIME_SIGNATURES = {
|
||||||
|
'application/x-subrip': ['310D0A', '310A'],
|
||||||
|
'text/vtt': ['574542565454'],
|
||||||
|
};
|
||||||
|
|
||||||
const SUPPORTED_LOCAL_SUBTITLES = [
|
const SUPPORTED_LOCAL_SUBTITLES = [
|
||||||
'application/x-subrip',
|
'application/x-subrip',
|
||||||
'text/vtt',
|
'text/vtt',
|
||||||
|
|
@ -118,6 +123,7 @@ module.exports = {
|
||||||
WRITERS_LINK_CATEGORY,
|
WRITERS_LINK_CATEGORY,
|
||||||
TYPE_PRIORITIES,
|
TYPE_PRIORITIES,
|
||||||
ICON_FOR_TYPE,
|
ICON_FOR_TYPE,
|
||||||
|
MIME_SIGNATURES,
|
||||||
SUPPORTED_LOCAL_SUBTITLES,
|
SUPPORTED_LOCAL_SUBTITLES,
|
||||||
EXTERNAL_PLAYERS,
|
EXTERNAL_PLAYERS,
|
||||||
WHITELISTED_HOSTS,
|
WHITELISTED_HOSTS,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import { isFileType } from './utils';
|
||||||
|
|
||||||
export type FileType = string;
|
export type FileType = string;
|
||||||
export type FileDropListener = (file: File) => void;
|
export type FileDropListener = (filename: string, buffer: ArrayBuffer) => void;
|
||||||
|
|
||||||
type FileDropContext = {
|
type FileDropContext = {
|
||||||
on: (type: FileType, listener: FileDropListener) => void,
|
on: (type: FileType, listener: FileDropListener) => void,
|
||||||
|
|
@ -31,15 +32,18 @@ const FileDropProvider = ({ className, children }: Props) => {
|
||||||
|
|
||||||
const onDrop = useCallback((event: DragEvent) => {
|
const onDrop = useCallback((event: DragEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const { dataTransfer } = event;
|
const { dataTransfer } = event;
|
||||||
|
|
||||||
if (dataTransfer && dataTransfer.files.length > 0) {
|
if (dataTransfer && dataTransfer?.files.length > 0) {
|
||||||
const file = dataTransfer.files[0];
|
const file = dataTransfer.files[0];
|
||||||
|
|
||||||
listeners
|
file
|
||||||
.filter(([type]) => type === file.type)
|
.arrayBuffer()
|
||||||
.forEach(([, listerner]) => listerner(file));
|
.then((buffer) => {
|
||||||
|
listeners
|
||||||
|
.filter(([type]) => file.type ? type === file.type : isFileType(buffer, type))
|
||||||
|
.forEach(([, listerner]) => listerner(file.name, buffer));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setActive(false);
|
setActive(false);
|
||||||
|
|
|
||||||
19
src/common/FileDrop/utils.ts
Normal file
19
src/common/FileDrop/utils.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { MIME_SIGNATURES } from 'stremio/common/CONSTANTS';
|
||||||
|
|
||||||
|
const SIGNATURES = MIME_SIGNATURES as Record<string, string[]>;
|
||||||
|
|
||||||
|
const isFileType = (buffer: ArrayBuffer, type: string) => {
|
||||||
|
const signatures = SIGNATURES[type];
|
||||||
|
|
||||||
|
return signatures.some((signature) => {
|
||||||
|
const array = new Uint8Array(buffer);
|
||||||
|
const signatureBuffer = Buffer.from(signature, 'hex');
|
||||||
|
const bufferToCompare = array.subarray(0, signatureBuffer.length);
|
||||||
|
|
||||||
|
return Buffer.compare(signatureBuffer, bufferToCompare) === 0;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
isFileType,
|
||||||
|
};
|
||||||
|
|
@ -277,9 +277,8 @@ const Player = ({ urlParams, queryParams }) => {
|
||||||
event.nativeEvent.immersePrevented = true;
|
event.nativeEvent.immersePrevented = true;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
onFileDrop(CONSTANTS.SUPPORTED_LOCAL_SUBTITLES, async (file) => {
|
onFileDrop(CONSTANTS.SUPPORTED_LOCAL_SUBTITLES, async (filename, buffer) => {
|
||||||
const buffer = await file.arrayBuffer();
|
video.addLocalSubtitles(filename, buffer);
|
||||||
video.addLocalSubtitles(file.name, buffer);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ function DragAndDrop({ core }) {
|
||||||
break;
|
break;
|
||||||
case 'text/vtt':
|
case 'text/vtt':
|
||||||
break;
|
break;
|
||||||
|
case '':
|
||||||
|
break;
|
||||||
default: {
|
default: {
|
||||||
events.emit('error', {
|
events.emit('error', {
|
||||||
message: 'Unsupported file',
|
message: 'Unsupported file',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue