Skip to content

Commit

Permalink
video server
Browse files Browse the repository at this point in the history
  • Loading branch information
caorushizi committed Jan 13, 2024
1 parent 48158eb commit b1a2d45
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 638 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "internal"]
path = internal
url = https://github.com/caorushizi/mediago
1 change: 1 addition & 0 deletions internal
Submodule internal added at ca43a3
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:mobile": "pnpm -F mobile run build",
"build:main": "pnpm -F main run build",
"build:renderer": "pnpm -F renderer run build",
"build:server": "cd internal && go build -o ../packages/main/bin/win32/server.exe",
"pack": "pnpm -F media-downloader run pack",
"release": "pnpm run build && pnpm -F main run release",
"docs:dev": "vitepress dev docs",
Expand Down
2 changes: 2 additions & 0 deletions packages/main/bin/win32/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Downloads
Logs

server.exe*
13 changes: 0 additions & 13 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
"@electron/rebuild": "^3.2.13",
"@rushstack/eslint-patch": "^1.3.2",
"@types/fs-extra": "^11.0.1",
"@types/koa": "^2.13.6",
"@types/koa-range": "^0.3.2",
"@types/koa-send": "^4.1.3",
"@types/koa__cors": "^4.0.0",
"@types/koa__router": "^12.0.0",
"@types/lodash": "^4.14.195",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "5.60.0",
Expand All @@ -48,9 +43,6 @@
"dependencies": {
"@cliqz/adblocker-electron": "^1.26.6",
"@cliqz/adblocker-electron-preload": "^1.26.6",
"@koa/cors": "^4.0.0",
"@koa/router": "^12.0.0",
"@types/koa-static": "^4.0.2",
"@types/semver": "^7.5.0",
"axios": "^1.4.0",
"better-sqlite3": "^8.4.0",
Expand All @@ -63,13 +55,8 @@
"electron-store": "^8.1.0",
"execa": "^7.1.1",
"fs-extra": "^11.1.1",
"glob": "^10.3.0",
"iconv-lite": "^0.6.3",
"inversify": "^6.0.1",
"koa": "^2.14.2",
"koa-range": "^0.3.0",
"koa-send": "^5.0.1",
"koa-static": "^5.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"ts-node": "^10.9.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/main/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import IpcHandlerService from "./services/IpcHandlerService";
import DatabaseService from "./services/DatabaseService";
import WebviewService from "./services/WebviewService";
import DevToolsService from "./services/DevToolsService";
import WebService from "./services/WebService";
import StoreService from "./services/StoreService";
import VideoRepository from "./repository/VideoRepository";
import VideoService from "./services/VideoService";

@injectable()
export default class ElectronApp {
Expand All @@ -34,10 +34,10 @@ export default class ElectronApp {
private readonly videoRepository: VideoRepository,
@inject(TYPES.DevToolsService)
private readonly devTools: DevToolsService,
@inject(TYPES.WebService)
private readonly webService: WebService,
@inject(TYPES.StoreService)
private readonly storeService: StoreService
private readonly storeService: StoreService,
@inject(TYPES.VideoService)
private readonly videoService: VideoService
) {}

private async seriveInit(): Promise<void> {
Expand All @@ -47,7 +47,7 @@ export default class ElectronApp {
this.ipcHandler.init();
this.updateService.init();
this.webview.init(), this.devTools.init();
this.webService.init();
this.videoService.init();
}

async init(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/controller/DownloadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class DownloadController implements Controller {
if (video.status === DownloadStatus.Success) {
return {
...video,
exist: existsSync(`${localDir}/${video.name}`),
exist: existsSync(`${localDir}/${video.name}.mp4`),
};
}
return video;
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ export default class HomeController implements Controller {
}

@handle("open-player-window")
async openPlayerWindow(event: IpcMainEvent, id: number) {
async openPlayerWindow(event: IpcMainEvent, name: string) {
// 打开播放器窗口
this.playerWindow.openWindow(id);
this.playerWindow.openWindow(name);
}

@handle("get-local-ip")
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/helper/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const m3u8DownloaderBin =
process.platform === "win32"
? resolve(__bin__, "N_m3u8DL-CLI")
: resolve(__bin__, "N_m3u8DL-RE");
export const videoServerBin = resolve(__bin__, "server.exe");

// mobile path
export const mobilePath = resolve(
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import LoggerService from "./services/LoggerService";
import ProtocolService from "./services/ProtocolService";
import UpdateService from "./services/UpdateService";
import { TYPES } from "./types";
import WebService from "./services/WebService";
import MainWindow from "./windows/MainWindow";
import PlayerWindow from "./windows/PlayerWindow";
import BrowserWindow from "./windows/BrowserWindow";
import VideoService from "./services/VideoService";

const container = new Container({
skipBaseClassChecks: true,
Expand All @@ -41,7 +41,7 @@ container.bind<DatabaseService>(TYPES.DatabaseService).to(DatabaseService);
container.bind<WebviewService>(TYPES.WebviewService).to(WebviewService);
container.bind<DownloadService>(TYPES.DownloadService).to(DownloadService);
container.bind<DevToolsService>(TYPES.DevToolsService).to(DevToolsService);
container.bind<WebService>(TYPES.WebService).to(WebService);
container.bind<VideoService>(TYPES.VideoService).to(VideoService);

// windows
container.bind<MainWindow>(TYPES.MainWindow).to(MainWindow);
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const electronApi = {
ipcRenderer.invoke("combine-to-home-page", store),
editDownloadItem: (video: DownloadItem): Promise<void> =>
ipcRenderer.invoke("edit-download-item", video),
openPlayerWindow: (videoId: number): Promise<void> =>
openPlayerWindow: (videoId: string): Promise<void> =>
ipcRenderer.invoke("open-player-window", videoId),
getLocalIP: (): Promise<string> => ipcRenderer.invoke("get-local-ip"),
openBrowser: (url: string): Promise<void> => shell.openExternal(url),
Expand Down
29 changes: 29 additions & 0 deletions packages/main/src/services/VideoService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { inject, injectable } from "inversify";
import { execa } from "execa";
import { mobilePath, videoServerBin } from "../helper";
import { TYPES } from "../types";
import StoreService from "./StoreService";

@injectable()
export default class VideoService {
constructor(
@inject(TYPES.StoreService)
private readonly storeService: StoreService
) {}

init(): void {
const local = this.storeService.get("local");
// empty
const child = execa(videoServerBin, [
"--static-path",
mobilePath,
"--video-path",
local,
"--port",
process.env.APP_SERVER_PORT || "",
]);

child.stdout?.pipe(process.stdout);
child.stderr?.pipe(process.stderr);
}
}
92 changes: 0 additions & 92 deletions packages/main/src/services/WebService.ts

This file was deleted.

23 changes: 13 additions & 10 deletions packages/main/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
export const TYPES = {
ElectronApp: Symbol.for("ElectronApp"),
MainWindow: Symbol.for("MainWindow"),
StoreService: Symbol.for("StoreService"),
SessionService: Symbol.for("SessionService"),
DatabaseService: Symbol.for("DatabaseService"),
ProtocolService: Symbol.for("ProtocolService"),
UpdateService: Symbol.for("UpdateService"),
LoggerService: Symbol.for("LoggerService"),
IpcHandlerService: Symbol.for("IpcHandlerService"),
Controller: Symbol.for("Controller"),
// repository
VideoRepository: Symbol.for("VideoRepository"),
FavoriteRepository: Symbol.for("FavoriteRepository"),
// windows
BrowserWindow: Symbol.for("BrowserWindow"),
PlayerWindow: Symbol.for("PlayerWindow"),
MainWindow: Symbol.for("MainWindow"),
// services
WebviewService: Symbol.for("WebviewService"),
DownloadService: Symbol.for("DownloadService"),
BrowserWindow: Symbol.for("BrowserWindow"),
DevToolsService: Symbol.for("DevToolsService"),
WebService: Symbol.for("WebService"),
PlayerWindow: Symbol.for("PlayerWindow"),
VideoService: Symbol.for("VideoService"),
StoreService: Symbol.for("StoreService"),
SessionService: Symbol.for("SessionService"),
DatabaseService: Symbol.for("DatabaseService"),
ProtocolService: Symbol.for("ProtocolService"),
UpdateService: Symbol.for("UpdateService"),
LoggerService: Symbol.for("LoggerService"),
};

export enum AppTheme {
Expand Down
18 changes: 6 additions & 12 deletions packages/main/src/windows/PlayerWindow.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import isDev from "electron-is-dev";
import { inject, injectable } from "inversify";
import { injectable } from "inversify";
import { resolve } from "path";
import { TYPES } from "../types";
import Window from "./window";
import VideoRepository from "../repository/VideoRepository";

@injectable()
export default class PlayerWindowServiceImpl extends Window {
url = isDev ? "http://localhost:8555/player" : "mediago://index.html/player";

constructor(
@inject(TYPES.VideoRepository)
private readonly videoRepository: VideoRepository
) {
constructor() {
super({
width: 1100,
minWidth: 1100,
Expand All @@ -26,16 +21,15 @@ export default class PlayerWindowServiceImpl extends Window {
});
}

openWindow = async (id: number) => {
openWindow = async (name: string) => {
if (!this.window) {
const url = new URL(this.url);
url.searchParams.set("name", encodeURIComponent(name));
this.url = url.toString();
this.window = this.create();
}

const video = await this.videoRepository.findVideo(id);
if (!video) throw new Error("video not found");

isDev && this.window.webContents.openDevTools();
this.window.webContents.send("open-player-window", video.id);
this.window.show();
};
}
1 change: 0 additions & 1 deletion packages/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const App: FC = () => {
};

const onReceiveDownloadItem = () => {
console.log("onReceiveDownloadItem");
dispatch(increase());
};

Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/src/nodes/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
disabled={!item.exist}
icon={<PlayCircleOutlined />}
title="播放视频"
onClick={() => openPlayerWindow(item.id)}
onClick={() => openPlayerWindow(item.name)}
/>,
<Dropdown
key="more"
Expand Down
Loading

0 comments on commit b1a2d45

Please sign in to comment.