From fdcecb0a758f83907030e8a40a718a7e0cafaf33 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 16 Apr 2021 21:12:56 +0200 Subject: [PATCH] * no unauthorized init * more dynamic Preview element --- apiGo/api/ApiBase.go | 8 --- apiGo/api/Init.go | 39 ---------- apiGo/api/Settings.go | 28 ++++++++ apiGo/main.go | 1 - src/App.tsx | 14 +++- .../DynamicContentContainer.tsx | 20 +++++- src/elements/Preview/Preview.tsx | 71 +++++++++++-------- .../VideoContainer/VideoContainer.tsx | 19 ++++- .../ActorOverviewPage/ActorOverviewPage.tsx | 14 ++-- src/pages/ActorPage/ActorPage.tsx | 2 +- src/pages/CategoryPage/TagView.tsx | 22 +++--- src/pages/HomePage/HomePage.tsx | 2 +- src/pages/TVShowPage/TVShowPage.tsx | 7 +- src/utils/Api.ts | 3 +- 14 files changed, 142 insertions(+), 108 deletions(-) delete mode 100644 apiGo/api/Init.go diff --git a/apiGo/api/ApiBase.go b/apiGo/api/ApiBase.go index 8914dfb..4dd760b 100644 --- a/apiGo/api/ApiBase.go +++ b/apiGo/api/ApiBase.go @@ -15,7 +15,6 @@ const ( TagNode = iota SettingsNode = iota ActorNode = iota - InitNode = iota ) type actionStruct struct { @@ -42,9 +41,6 @@ func ServerInit() { http.Handle(APIPREFIX+"/settings", oauth.ValidateToken(settingsHandler)) http.Handle(APIPREFIX+"/actor", oauth.ValidateToken(actorHandler)) - // initialization api calls to check if password is neccessaray - http.Handle(APIPREFIX+"/init", http.HandlerFunc(initHandler)) - // initialize oauth service and add corresponding auth routes oauth.InitOAuth() } @@ -85,10 +81,6 @@ func settingsHandler(rw http.ResponseWriter, req *http.Request) { handlefunc(rw, req, SettingsNode) } -func initHandler(rw http.ResponseWriter, req *http.Request) { - handlefunc(rw, req, InitNode) -} - func handlefunc(rw http.ResponseWriter, req *http.Request, node int) { // only allow post requests if req.Method != "POST" { diff --git a/apiGo/api/Init.go b/apiGo/api/Init.go deleted file mode 100644 index b79eda9..0000000 --- a/apiGo/api/Init.go +++ /dev/null @@ -1,39 +0,0 @@ -package api - -import ( - "encoding/json" - "openmediacenter/apiGo/database/settings" - "regexp" - "strings" -) - -func AddInitHandlers() { - passwordNeeded() -} - -func passwordNeeded() { - AddHandler("loadInitialData", InitNode, nil, func() []byte { - sett := settings.LoadSettings() - - type InitialDataTypeResponse struct { - DarkMode bool - Pasword bool - MediacenterName string - VideoPath string - } - - regexMatchUrl := regexp.MustCompile("^http(|s):\\/\\/([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}:[0-9]{1,5}") - videoUrl := regexMatchUrl.FindString(sett.VideoPath) - serverVideoPath := strings.TrimPrefix(sett.VideoPath, videoUrl) - - res := InitialDataTypeResponse{ - DarkMode: sett.DarkMode, - Pasword: sett.Pasword != "-1", - MediacenterName: sett.Mediacenter_name, - VideoPath: serverVideoPath, - } - - str, _ := json.Marshal(res) - return str - }) -} diff --git a/apiGo/api/Settings.go b/apiGo/api/Settings.go index 3383f38..2f6a296 100644 --- a/apiGo/api/Settings.go +++ b/apiGo/api/Settings.go @@ -1,9 +1,13 @@ package api import ( + "encoding/json" "openmediacenter/apiGo/api/types" "openmediacenter/apiGo/database" + "openmediacenter/apiGo/database/settings" "openmediacenter/apiGo/videoparser" + "regexp" + "strings" ) func AddSettingsHandlers() { @@ -17,6 +21,30 @@ func getSettingsFromDB() { result := database.GetSettings() return jsonify(result) }) + AddHandler("loadInitialData", SettingsNode, nil, func() []byte { + sett := settings.LoadSettings() + + type InitialDataTypeResponse struct { + DarkMode bool + Pasword bool + MediacenterName string + VideoPath string + } + + regexMatchUrl := regexp.MustCompile("^http(|s):\\/\\/([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}:[0-9]{1,5}") + videoUrl := regexMatchUrl.FindString(sett.VideoPath) + serverVideoPath := strings.TrimPrefix(sett.VideoPath, videoUrl) + + res := InitialDataTypeResponse{ + DarkMode: sett.DarkMode, + Pasword: sett.Pasword != "-1", + MediacenterName: sett.Mediacenter_name, + VideoPath: serverVideoPath, + } + + str, _ := json.Marshal(res) + return str + }) } func saveSettingsToDB() { diff --git a/apiGo/main.go b/apiGo/main.go index 77652c5..17974e8 100644 --- a/apiGo/main.go +++ b/apiGo/main.go @@ -30,7 +30,6 @@ func main() { api.AddSettingsHandlers() api.AddTagHandlers() api.AddActorsHandlers() - api.AddInitHandlers() // add the static files static.ServeStaticFiles() diff --git a/src/App.tsx b/src/App.tsx index 91b9e4f..d3d4c81 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,7 +9,7 @@ import style from './App.module.css'; import SettingsPage from './pages/SettingsPage/SettingsPage'; import CategoryPage from './pages/CategoryPage/CategoryPage'; -import {APINode, apiTokenValid, callApiUnsafe, refreshAPIToken} from './utils/Api'; +import {APINode, apiTokenValid, callAPI, refreshAPIToken} from './utils/Api'; import {BrowserRouter as Router, NavLink, Route, Switch} from 'react-router-dom'; import Player from './pages/Player/Player'; @@ -75,7 +75,7 @@ class App extends React.Component<{}, state> { initialAPICall(): void { // this is the first api call so if it fails we know there is no connection to backend - callApiUnsafe(APINode.Init, {action: 'loadInitialData'}, (result: SettingsTypes.initialApiCallData) => { + callAPI(APINode.Settings, {action: 'loadInitialData'}, (result: SettingsTypes.initialApiCallData) => { // set theme GlobalInfos.enableDarkTheme(result.DarkMode); @@ -99,7 +99,15 @@ class App extends React.Component<{}, state> { if (this.state.password === true) { // render authentication page if auth is neccessary - return this.setState({password: false})} />; + return ( + { + this.setState({password: false}); + // reinit general infos + this.initialAPICall(); + }} + /> + ); } else if (this.state.password === false) { return ( diff --git a/src/elements/DynamicContentContainer/DynamicContentContainer.tsx b/src/elements/DynamicContentContainer/DynamicContentContainer.tsx index c49300b..4b19b99 100644 --- a/src/elements/DynamicContentContainer/DynamicContentContainer.tsx +++ b/src/elements/DynamicContentContainer/DynamicContentContainer.tsx @@ -9,7 +9,6 @@ interface Props { interface state { loadeditems: T[]; - selectionnr: number; } /** @@ -24,8 +23,7 @@ class DynamicContentContainer extends React.Component, state> { super(props); this.state = { - loadeditems: [], - selectionnr: 0 + loadeditems: [] }; } @@ -35,6 +33,22 @@ class DynamicContentContainer extends React.Component, state> { this.loadPreviewBlock(this.props.initialLoadNr ? this.props.initialLoadNr : 16); } + componentDidUpdate(prevProps: Props): void { + // when source props change force update! + if (prevProps.data.length !== this.props.data.length) { + this.clean(); + this.loadPreviewBlock(this.props.initialLoadNr ? this.props.initialLoadNr : 16); + } + } + + /** + * clear all elements rendered... + */ + clean(): void { + this.loadindex = 0; + this.setState({loadeditems: []}); + } + render(): JSX.Element { return (
diff --git a/src/elements/Preview/Preview.tsx b/src/elements/Preview/Preview.tsx index 52b9bec..93abc7c 100644 --- a/src/elements/Preview/Preview.tsx +++ b/src/elements/Preview/Preview.tsx @@ -3,17 +3,18 @@ import style from './Preview.module.css'; import {Spinner} from 'react-bootstrap'; import {Link} from 'react-router-dom'; import GlobalInfos from '../../utils/GlobalInfos'; -import {APINode, callAPIPlain} from '../../utils/Api'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {faPhotoVideo} from '@fortawesome/free-solid-svg-icons'; interface PreviewProps { name: string; - movieId: number; + picLoader: (callback: (pic: string) => void) => void; + linkPath?: string; + onClick?: () => void; } interface PreviewState { - previewpicture: string | null; + picLoaded: boolean | null; } /** @@ -21,49 +22,61 @@ interface PreviewState { * floating side by side */ class Preview extends React.Component { + // store the picture to display + pic?: string; + constructor(props: PreviewProps) { super(props); this.state = { - previewpicture: null + picLoaded: null }; } componentDidMount(): void { - callAPIPlain(APINode.Video, {action: 'readThumbnail', movieid: this.props.movieId}, (result) => { + this.props.picLoader((result) => { + this.pic = result; this.setState({ - previewpicture: result + picLoaded: result !== '' }); }); } render(): JSX.Element { + if (this.props.linkPath !== undefined) { + return {this.content()}; + } else { + return this.content(); + } + } + + content(): JSX.Element { const themeStyle = GlobalInfos.getThemeStyle(); return ( - -
-
{this.props.name}
-
- {this.state.previewpicture === '' ? ( - - ) : this.state.previewpicture === null ? ( - - - - ) : ( - Pic loading. - )} -
-
+
+
{this.props.name}
+
+ {this.state.picLoaded === false ? ( + + ) : this.state.picLoaded === null ? ( + + + + ) : ( + Pic loading. + )}
- +
+
); } } diff --git a/src/elements/VideoContainer/VideoContainer.tsx b/src/elements/VideoContainer/VideoContainer.tsx index efce56c..e2804f5 100644 --- a/src/elements/VideoContainer/VideoContainer.tsx +++ b/src/elements/VideoContainer/VideoContainer.tsx @@ -2,6 +2,7 @@ import React from 'react'; import Preview from '../Preview/Preview'; import {VideoTypes} from '../../types/ApiTypes'; import DynamicContentContainer from '../DynamicContentContainer/DynamicContentContainer'; +import {APINode, callAPIPlain} from '../../utils/Api'; interface Props { data: VideoTypes.VideoUnloadedType[]; @@ -11,7 +12,23 @@ interface Props { const VideoContainer = (props: Props): JSX.Element => { return ( } + renderElement={(el): JSX.Element => ( + void): void => { + callAPIPlain( + APINode.Video, + { + action: 'readThumbnail', + movieid: el.MovieId + }, + (result) => callback(result) + ); + }} + name={el.MovieName} + linkPath={'/player/' + el.MovieId} + /> + )} data={props.data}> {props.children} diff --git a/src/pages/ActorOverviewPage/ActorOverviewPage.tsx b/src/pages/ActorOverviewPage/ActorOverviewPage.tsx index 0e74a37..bdc0cf9 100644 --- a/src/pages/ActorOverviewPage/ActorOverviewPage.tsx +++ b/src/pages/ActorOverviewPage/ActorOverviewPage.tsx @@ -4,9 +4,10 @@ import {ActorType} from '../../types/VideoTypes'; import ActorTile from '../../elements/ActorTile/ActorTile'; import PageTitle from '../../elements/PageTitle/PageTitle'; import SideBar from '../../elements/SideBar/SideBar'; -import style from './ActorOverviewPage.module.css'; +// import style from './ActorOverviewPage.module.css'; import {Button} from '../../elements/GPElements/Button'; import NewActorPopup from '../../elements/Popups/NewActorPopup/NewActorPopup'; +import DynamicContentContainer from '../../elements/DynamicContentContainer/DynamicContentContainer'; interface Props {} @@ -36,11 +37,12 @@ class ActorOverviewPage extends React.Component {
Attention: This is an early preview! - {this.state.data.length !== 0 ? :
No Data found!
} + ); } diff --git a/src/pages/CategoryPage/TagView.tsx b/src/pages/CategoryPage/TagView.tsx index cf99f4d..da47e02 100644 --- a/src/pages/CategoryPage/TagView.tsx +++ b/src/pages/CategoryPage/TagView.tsx @@ -53,19 +53,15 @@ class TagView extends React.Component { Add a new Tag! - {this.state.loadedtags.length !== 0 ? ( - ( - - - - )} - initialLoadNr={20} - /> - ) : ( - 'loading' - )} + ( + + + + )} + initialLoadNr={20} + /> {this.handlePopups()} ); diff --git a/src/pages/HomePage/HomePage.tsx b/src/pages/HomePage/HomePage.tsx index b7af48e..dd6b102 100644 --- a/src/pages/HomePage/HomePage.tsx +++ b/src/pages/HomePage/HomePage.tsx @@ -157,7 +157,7 @@ export class HomePage extends React.Component { }} /> - {this.state.data.length !== 0 ? :
No Data found!
} +
diff --git a/src/pages/TVShowPage/TVShowPage.tsx b/src/pages/TVShowPage/TVShowPage.tsx index 7d6edd3..dfec9d5 100644 --- a/src/pages/TVShowPage/TVShowPage.tsx +++ b/src/pages/TVShowPage/TVShowPage.tsx @@ -1,8 +1,13 @@ import React from 'react'; +import Preview from '../../elements/Preview/Preview'; class TVShowPage extends React.Component { render(): JSX.Element { - return <>TvShowPage; + return ( + <> + callback('')} /> + + ); } } diff --git a/src/utils/Api.ts b/src/utils/Api.ts index 0649014..404b408 100644 --- a/src/utils/Api.ts +++ b/src/utils/Api.ts @@ -279,6 +279,5 @@ export enum APINode { Settings = 'settings', Tags = 'tags', Actor = 'actor', - Video = 'video', - Init = 'init' + Video = 'video' }