Skip to content

Commit

Permalink
feat: 预览视频列表
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed May 27, 2024
1 parent 0a947bc commit 366f759
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/desktop/electron/main/ipcMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ function initIpcMain() {
return viewImageWin.setIsAlwaysOnTopViewImageWin(e.sender.id, isAlwaysOnTop);
});
ipcMain.handle('vi:get-imgs', async (e, img) => {
const imgs = await viewImageWin.getImgs(img);
return imgs;
const res = await viewImageWin.getImgs(img);
return res;
});
ipcMain.on('vi:download-img', async (e, imgUrl) => {
viewImageWin.downloadImg(imgUrl);
Expand Down Expand Up @@ -286,8 +286,9 @@ function initIpcMain() {
ipcMain.on('vv:unmaximize-win', () => {
viewVideoWin.unmaximizeViewVideoWin();
});
ipcMain.on('vv:set-always-on-top', (e, isAlwaysOnTop) => {
viewVideoWin.setAlwaysOnTopViewVideoWin(isAlwaysOnTop);
ipcMain.handle('vv:get-videos', async (e, videoUrl) => {
const res = await viewVideoWin.getVideos(videoUrl);
return res;
});

// 录音;
Expand Down
30 changes: 29 additions & 1 deletion packages/desktop/electron/main/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { screen } from 'electron';
import * as fs from 'node:fs';
import path from 'node:path';
import path, { basename, extname, join } from 'node:path';
import { PEAR_FILES_PATH } from './constant';

function getScreenSize() {
Expand Down Expand Up @@ -121,6 +121,33 @@ function isAudioFile(filePath: string): boolean {
].includes(ext);
}

function getVideosByVideoUrl(videoUrl: string) {
const directoryPath = path.dirname(videoUrl);
const files = fs.readdirSync(directoryPath); // 读取目录内容
let videos: any[] = [];
let index = 0;
let currentIndex = 0;
files.forEach((file) => {
const filePath = join(directoryPath, file);
function isVideoFile(filePath: string): boolean {
const ext = extname(filePath).toLowerCase();
return ['.mp4', '.mkv', '.avi', '.mov', '.wmv', '.webm'].includes(ext);
}
if (isVideoFile(filePath)) {
const fileName = basename(filePath);
filePath == videoUrl && (currentIndex = index);
videos.push({
url: `pearrec://${filePath}`,
index,
name: fileName,
});
index++;
}
});

return { videos, currentIndex };
}

function readDirectoryVideo(filePath: string) {
filePath = filePath.replace(/\\/g, '/');
return filePath && `pearrec:///${filePath}`;
Expand All @@ -134,6 +161,7 @@ function readDirectoryImg(filePath: string) {
export {
downloadFile,
getAudiosByAudioUrl,
getVideosByVideoUrl,
getImgsByImgUrl,
getScreenSize,
readDirectoryImg,
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/electron/preload/electronAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
sendViAlwaysOnTopWin: (isTop: boolean) => ipcRenderer.send('vi:alwaysOnTop-win', isTop),
sendViOpenFile: (imgUrl: string) => ipcRenderer.send('vi:open-file', imgUrl),
invokeViSetIsAlwaysOnTop: () => ipcRenderer.invoke('vi:set-always-on-top'),
invokeEiGetImgsWin: (imgUrl: string) => ipcRenderer.invoke('vi:get-imgs', imgUrl),
invokeViGetImgs: (imgUrl: string) => ipcRenderer.invoke('vi:get-imgs', imgUrl),
sendViDownloadImg: (img: string) => ipcRenderer.send('vi:download-img', img),
sendViSetHistoryImg: (img: string) => {
ipcRenderer.send('vi:set-historyImg', img);
Expand Down Expand Up @@ -110,7 +110,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
//vvWin
sendVvOpenWin: (search?: string) => ipcRenderer.send('vv:open-win', search),
sendVvCloseWin: () => ipcRenderer.send('vv:close-win'),
invokeVvGetHistoryVideo: () => ipcRenderer.invoke('vv:get-historyVideo'),
invokeVvGetVideos: (videoUrl?: string) => ipcRenderer.invoke('vv:get-videos', videoUrl),
sendVvSetHistoryVideo: (img: string) => ipcRenderer.send('vv:set-historyVideo', img),

//vaWin
Expand Down
1 change: 0 additions & 1 deletion packages/desktop/electron/win/viewImageWin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BrowserWindow, dialog } from 'electron';
import { readFile, writeFile } from 'node:fs';
import { ICON, WEB_URL, WIN_CONFIG, preload, url } from '../main/constant';
import { getImgsByImgUrl } from '../main/utils';
import { v5 as uuidv5 } from 'uuid';

// let viewImageWin: BrowserWindow | null = null;
let viewImageWinMap = new Map<number, BrowserWindow | null>();
Expand Down
13 changes: 8 additions & 5 deletions packages/desktop/electron/win/viewVideoWin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserWindow } from 'electron';
import { ICON, WEB_URL, WIN_CONFIG, preload, url } from '../main/constant';
import { getVideosByVideoUrl } from '../main/utils';

let viewVideoWin: BrowserWindow | null = null;

Expand All @@ -15,15 +16,16 @@ function createViewVideoWin(search?: any): BrowserWindow {

const videoUrl = search?.videoUrl || '';
const recordId = search?.recordId || '';
// Open devTool if the app is not packaged
// viewVideoWin.webContents.openDevTools();

if (url) {
viewVideoWin.loadURL(
WEB_URL +
`viewVideo.html?${videoUrl ? 'videoUrl=' + videoUrl : ''}${
recordId ? 'recordId=' + recordId : ''
}`,
);
// Open devTool if the app is not packaged
// viewVideoWin.webContents.openDevTools();
} else {
viewVideoWin.loadFile(WIN_CONFIG.viewVideo.html, {
search: `?${videoUrl ? 'videoUrl=' + videoUrl : ''}${recordId ? 'recordId=' + recordId : ''}`,
Expand Down Expand Up @@ -69,9 +71,9 @@ function setAlwaysOnTopViewVideoWin(isAlwaysOnTop: boolean) {
viewVideoWin?.setAlwaysOnTop(isAlwaysOnTop);
}

async function getHistoryVideoPath() {
// const historyVideoPath = ((await getHistoryVideo()) as string) || '';
// return historyVideoPath;
async function getVideos(videoUrl: string) {
const res = await getVideosByVideoUrl(videoUrl);
return res;
}

async function sendHistoryVideo() {
Expand All @@ -90,4 +92,5 @@ export {
sendHistoryVideo,
setAlwaysOnTopViewVideoWin,
unmaximizeViewVideoWin,
getVideos,
};
2 changes: 1 addition & 1 deletion packages/web/src/pages/viewImage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const ViewImage = () => {
if (imgUrl) {
if (imgUrl.substring(0, 7) != 'pearrec' && imgUrl.substring(0, 4) != 'blob') {
if (window.isElectron) {
let { imgs, currentIndex } = await window.electronAPI.invokeEiGetImgsWin(imgUrl);
let { imgs, currentIndex } = await window.electronAPI.invokeViGetImgs(imgUrl);
setImgs([...imgs]);
initialViewIndexRef.current = currentIndex;
} else {
Expand Down
10 changes: 9 additions & 1 deletion packages/web/src/pages/viewVideo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ const ViewVideo = () => {
if (videoUrl.substring(0, 4) == 'blob' || videoUrl.substring(0, 7) == 'pearrec') {
setSource(videoUrl);
} else {
setSource(`pearrec://${videoUrl}`);
if (window.isElectron) {
let { videos, currentIndex } = await window.electronAPI.invokeVvGetVideos(videoUrl);
let _videos = [...videos];
setVideos(_videos);
setVideoIndex(currentIndex);
setSource(_videos[currentIndex].url);
} else {
setSource(`pearrec://${videoUrl}`);
}
}
} else if (recordId) {
let record = await db.records.where({ id: Number(recordId) }).first();
Expand Down

0 comments on commit 366f759

Please sign in to comment.