1
1
mirror of https://github.com/neosubhamoy/neodlp.git synced 2026-03-22 06:55:51 +05:30

refactor: use global flatpak detection

This commit is contained in:
2026-02-27 21:21:11 +05:30
Verified
parent 6e76747f06
commit 7a7f6705d4
8 changed files with 59 additions and 21 deletions

View File

@@ -29,6 +29,7 @@
"targets": ["deb", "rpm"], "targets": ["deb", "rpm"],
"createUpdaterArtifacts": true, "createUpdaterArtifacts": true,
"licenseFile": "../LICENSE", "licenseFile": "../LICENSE",
"category": "Utility",
"icon": [ "icon": [
"icons/32x32.png", "icons/32x32.png",
"icons/128x128.png", "icons/128x128.png",

View File

@@ -29,6 +29,7 @@
"targets": ["deb"], "targets": ["deb"],
"createUpdaterArtifacts": true, "createUpdaterArtifacts": true,
"licenseFile": "../LICENSE", "licenseFile": "../LICENSE",
"category": "Utility",
"icon": [ "icon": [
"icons/32x32.png", "icons/32x32.png",
"icons/128x128.png", "icons/128x128.png",

View File

@@ -29,6 +29,7 @@
"targets": ["deb", "rpm", "appimage"], "targets": ["deb", "rpm", "appimage"],
"createUpdaterArtifacts": true, "createUpdaterArtifacts": true,
"licenseFile": "../LICENSE", "licenseFile": "../LICENSE",
"category": "Utility",
"icon": [ "icon": [
"icons/32x32.png", "icons/32x32.png",
"icons/128x128.png", "icons/128x128.png",

View File

@@ -4,7 +4,7 @@ import { AppContext } from "@/providers/appContextProvider";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { arch, exeExtension } from "@tauri-apps/plugin-os"; import { arch, exeExtension } from "@tauri-apps/plugin-os";
import { downloadDir, join, resourceDir, tempDir } from "@tauri-apps/api/path"; import { downloadDir, join, resourceDir, tempDir } from "@tauri-apps/api/path";
import { useBasePathsStore, useCurrentVideoMetadataStore, useDownloaderPageStatesStore, useDownloadStatesStore, useKvPairsStatesStore, useSettingsPageStatesStore } from "@/services/store"; import { useBasePathsStore, useCurrentVideoMetadataStore, useDownloaderPageStatesStore, useDownloadStatesStore, useEnvironmentStore, useKvPairsStatesStore, useSettingsPageStatesStore } from "@/services/store";
import { isObjEmpty} from "@/utils"; import { isObjEmpty} from "@/utils";
import { Command } from "@tauri-apps/plugin-shell"; import { Command } from "@tauri-apps/plugin-shell";
import { useUpdateDownloadStatus } from "@/services/mutations"; import { useUpdateDownloadStatus } from "@/services/mutations";
@@ -27,6 +27,8 @@ import { useLogger } from "@/helpers/use-logger";
import useDownloader from "@/helpers/use-downloader"; import useDownloader from "@/helpers/use-downloader";
import usePotServer from "@/helpers/use-pot-server"; import usePotServer from "@/helpers/use-pot-server";
import { useLinuxRegisterer } from "@/helpers/use-linux-registerer"; import { useLinuxRegisterer } from "@/helpers/use-linux-registerer";
import { invoke } from "@tauri-apps/api/core";
export default function App({ children }: { children: React.ReactNode }) { export default function App({ children }: { children: React.ReactNode }) {
const { data: downloadStates, isSuccess: isSuccessFetchingDownloadStates } = useFetchAllDownloadStates(); const { data: downloadStates, isSuccess: isSuccessFetchingDownloadStates } = useFetchAllDownloadStates();
@@ -37,6 +39,10 @@ export default function App({ children }: { children: React.ReactNode }) {
const globalDownloadStates = useDownloadStatesStore((state) => state.downloadStates); const globalDownloadStates = useDownloadStatesStore((state) => state.downloadStates);
const setDownloadStates = useDownloadStatesStore((state) => state.setDownloadStates); const setDownloadStates = useDownloadStatesStore((state) => state.setDownloadStates);
const setPath = useBasePathsStore((state) => state.setPath); const setPath = useBasePathsStore((state) => state.setPath);
const isFlatpak = useEnvironmentStore(state => state.isFlatpak);
const setIsFlatpak = useEnvironmentStore((state) => state.setIsFlatpak);
const setIsAppimage = useEnvironmentStore((state) => state.setIsAppimage);
const setAppDirPath = useEnvironmentStore((state) => state.setAppDirPath);
const setIsUsingDefaultSettings = useSettingsPageStatesStore((state) => state.setIsUsingDefaultSettings); const setIsUsingDefaultSettings = useSettingsPageStatesStore((state) => state.setIsUsingDefaultSettings);
const setSettingsKey = useSettingsPageStatesStore((state) => state.setSettingsKey); const setSettingsKey = useSettingsPageStatesStore((state) => state.setSettingsKey);
@@ -120,6 +126,26 @@ export default function App({ children }: { children: React.ReactNode }) {
}; };
}, [stopPotServer]); }, [stopPotServer]);
// Detect sandboxed environments
useEffect(() => {
const detectEnvironment = async () => {
try {
const flatpak = await invoke<boolean>('is_flatpak');
setIsFlatpak(flatpak);
const appimage = await invoke<string | null>('is_appimage');
if (appimage) {
setIsAppimage(true);
setAppDirPath(appimage);
} else {
setIsAppimage(false);
}
} catch (e) {
console.error('Failed to detect environment:', e);
}
};
detectEnvironment();
}, [setIsFlatpak, setIsAppimage, setAppDirPath]);
// Listen for websocket messages // Listen for websocket messages
useEffect(() => { useEffect(() => {
const unlisten = listen<WebSocketMessage>('websocket-message', (event) => { const unlisten = listen<WebSocketMessage>('websocket-message', (event) => {
@@ -187,7 +213,7 @@ export default function App({ children }: { children: React.ReactNode }) {
const resourceDirPath = await resourceDir(); const resourceDirPath = await resourceDir();
const ffmpegPath = await join(resourceDirPath, 'binaries', `ffmpeg-${currentArch}${currentExeExtension ? '.' + currentExeExtension : ''}`); const ffmpegPath = await join(resourceDirPath, 'binaries', `ffmpeg-${currentArch}${currentExeExtension ? '.' + currentExeExtension : ''}`);
const tempDownloadDirPath = await join(tempDirPath, config.appPkgName, 'downloads'); const tempDownloadDirPath = isFlatpak ? await join(downloadDirPath, config.appName, '.tempdownloads') : await join(tempDirPath, config.appPkgName, 'downloads');
const appDownloadDirPath = await join(downloadDirPath, config.appName); const appDownloadDirPath = await join(downloadDirPath, config.appName);
if (!await fs.exists(tempDownloadDirPath)) fs.mkdir(tempDownloadDirPath, { recursive: true }).then(() => { console.log(`Created DIR: ${tempDownloadDirPath}`) }); if (!await fs.exists(tempDownloadDirPath)) fs.mkdir(tempDownloadDirPath, { recursive: true }).then(() => { console.log(`Created DIR: ${tempDownloadDirPath}`) });
@@ -206,7 +232,7 @@ export default function App({ children }: { children: React.ReactNode }) {
} }
}; };
initPaths(); initPaths();
}, [DOWNLOAD_DIR, setPath]); }, [DOWNLOAD_DIR, setPath, isFlatpak]);
// Fetch app version // Fetch app version
useEffect(() => { useEffect(() => {
@@ -278,6 +304,10 @@ export default function App({ children }: { children: React.ReactNode }) {
console.log("Auto-update check already performed in this session, skipping"); console.log("Auto-update check already performed in this session, skipping");
return; return;
} }
if (isFlatpak) {
console.log("Flatpak detected! Skipping yt-dlp auto-update");
return;
}
hasRunYtDlpAutoUpdateRef.current = true; hasRunYtDlpAutoUpdateRef.current = true;
console.log("Checking yt-dlp auto-update with loaded config values:", { console.log("Checking yt-dlp auto-update with loaded config values:", {
autoUpdate: YTDLP_AUTO_UPDATE, autoUpdate: YTDLP_AUTO_UPDATE,

View File

@@ -1,9 +1,7 @@
import { join, resourceDir, homeDir } from "@tauri-apps/api/path"; import { join, resourceDir, homeDir } from "@tauri-apps/api/path";
import * as fs from "@tauri-apps/plugin-fs"; import * as fs from "@tauri-apps/plugin-fs";
import { useKvPairs } from "@/helpers/use-kvpairs"; import { useKvPairs } from "@/helpers/use-kvpairs";
import { useSettingsPageStatesStore } from "@/services/store"; import { useEnvironmentStore, useSettingsPageStatesStore } from "@/services/store";
import { invoke } from "@tauri-apps/api/core";
import { useLogger } from "@/helpers/use-logger";
import { Command } from "@tauri-apps/plugin-shell"; import { Command } from "@tauri-apps/plugin-shell";
interface FileMap { interface FileMap {
@@ -16,11 +14,10 @@ interface FileMap {
export function useLinuxRegisterer() { export function useLinuxRegisterer() {
const { saveKvPair } = useKvPairs(); const { saveKvPair } = useKvPairs();
const appVersion = useSettingsPageStatesStore(state => state.appVersion); const appVersion = useSettingsPageStatesStore(state => state.appVersion);
const LOG = useLogger();
const registerToLinux = async () => { const registerToLinux = async () => {
try { try {
const isFlatpak = await invoke<boolean>('is_flatpak'); const isFlatpak = useEnvironmentStore(state => state.isFlatpak);
const resourceDirPath = isFlatpak ? '/app/lib/neodlp' : await resourceDir(); const resourceDirPath = isFlatpak ? '/app/lib/neodlp' : await resourceDir();
const homeDirPath = await homeDir(); const homeDirPath = await homeDir();
const flatpakChromeManifestContent = { const flatpakChromeManifestContent = {
@@ -51,8 +48,6 @@ export function useLinuxRegisterer() {
{ source: 'neodlp-msghost', destination: '.local/bin/neodlp-msghost', dir: '.local/bin/' }, { source: 'neodlp-msghost', destination: '.local/bin/neodlp-msghost', dir: '.local/bin/' },
]; ];
LOG.info("LINUX REGISTERER", `Is Flatpak: ${isFlatpak}, Resource dir: ${resourceDirPath}, Home dir: ${homeDirPath}`);
if (isFlatpak) { if (isFlatpak) {
for (const file of filesToCopyFlatpak) { for (const file of filesToCopyFlatpak) {
const sourcePath = await join(resourceDirPath, file.source); const sourcePath = await join(resourceDirPath, file.source);
@@ -65,20 +60,16 @@ export function useLinuxRegisterer() {
const writeOutput = await writeCommand.execute(); const writeOutput = await writeCommand.execute();
if (writeOutput.code === 0) { if (writeOutput.code === 0) {
console.log(`File ${file.destination} created successfully at ${destinationPath}`); console.log(`File ${file.destination} created successfully at ${destinationPath}`);
LOG.info("LINUX REGISTERER", `File ${file.destination} created successfully at ${destinationPath}`);
} else { } else {
console.error(`Failed to create file ${file.destination} at ${destinationPath}:`, writeOutput.stderr); console.error(`Failed to create file ${file.destination} at ${destinationPath}:`, writeOutput.stderr);
LOG.error("LINUX REGISTERER", `Failed to create file ${file.destination} at ${destinationPath}: ${writeOutput.stderr}`);
return { success: false, message: 'Failed to register' }; return { success: false, message: 'Failed to register' };
} }
} else { } else {
const copyOutput = await copyCommand.execute(); const copyOutput = await copyCommand.execute();
if (copyOutput.code === 0) { if (copyOutput.code === 0) {
console.log(`File ${file.source} copied successfully to ${destinationPath}`); console.log(`File ${file.source} copied successfully to ${destinationPath}`);
LOG.info("LINUX REGISTERER", `File ${file.source} copied successfully to ${destinationPath}`);
} else { } else {
console.error(`Failed to copy file ${file.source} to ${destinationPath}:`, copyOutput.stderr); console.error(`Failed to copy file ${file.source} to ${destinationPath}:`, copyOutput.stderr);
LOG.error("LINUX REGISTERER", `Failed to copy file ${file.source} to ${destinationPath}: ${copyOutput.stderr}`);
return { success: false, message: 'Failed to register' }; return { success: false, message: 'Failed to register' };
} }
} }
@@ -93,14 +84,11 @@ export function useLinuxRegisterer() {
if (dirExists) { if (dirExists) {
await fs.copyFile(sourcePath, destinationPath); await fs.copyFile(sourcePath, destinationPath);
console.log(`File ${file.source} copied successfully to ${destinationPath}`); console.log(`File ${file.source} copied successfully to ${destinationPath}`);
LOG.info("LINUX REGISTERER", `File ${file.source} copied successfully to ${destinationPath}`);
} else { } else {
await fs.mkdir(destinationDir, { recursive: true }) await fs.mkdir(destinationDir, { recursive: true })
console.log(`Created dir ${destinationDir}`); console.log(`Created dir ${destinationDir}`);
LOG.info("LINUX REGISTERER", `Created dir ${destinationDir}`);
await fs.copyFile(sourcePath, destinationPath); await fs.copyFile(sourcePath, destinationPath);
console.log(`File ${file.source} copied successfully to ${destinationPath}`); console.log(`File ${file.source} copied successfully to ${destinationPath}`);
LOG.info("LINUX REGISTERER", `File ${file.source} copied successfully to ${destinationPath}`);
} }
} }
} }
@@ -108,7 +96,6 @@ export function useLinuxRegisterer() {
return { success: true, message: 'Registered successfully' } return { success: true, message: 'Registered successfully' }
} catch (error) { } catch (error) {
console.error('Error copying files:', error); console.error('Error copying files:', error);
LOG.error("LINUX REGISTERER", `Error copying files: ${error}`);
return { success: false, message: 'Failed to register' } return { success: false, message: 'Failed to register' }
} }
} }

View File

@@ -1,4 +1,4 @@
import { useSettingsPageStatesStore } from "@/services/store"; import { useEnvironmentStore, useSettingsPageStatesStore } from "@/services/store";
import { useKvPairs } from "@/helpers/use-kvpairs"; import { useKvPairs } from "@/helpers/use-kvpairs";
import { Command } from "@tauri-apps/plugin-shell"; import { Command } from "@tauri-apps/plugin-shell";
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
@@ -17,7 +17,7 @@ export function useYtDlpUpdater() {
const updateYtDlp = async () => { const updateYtDlp = async () => {
const CURRENT_TIMESTAMP = Date.now(); const CURRENT_TIMESTAMP = Date.now();
const isFlatpak = await invoke<boolean>('is_flatpak'); const isFlatpak = useEnvironmentStore(state => state.isFlatpak);
setIsUpdatingYtDlp(true); setIsUpdatingYtDlp(true);
LOG.info('NEODLP', 'Updating yt-dlp to latest version'); LOG.info('NEODLP', 'Updating yt-dlp to latest version');
try { try {

View File

@@ -1,4 +1,4 @@
import { BasePathsStore, CurrentVideoMetadataStore, DownloadActionStatesStore, DownloaderPageStatesStore, DownloadStatesStore, KvPairsStatesStore, LibraryPageStatesStore, LogsStore, SettingsPageStatesStore } from '@/types/store'; import { BasePathsStore, CurrentVideoMetadataStore, DownloadActionStatesStore, DownloaderPageStatesStore, DownloadStatesStore, EnvironmentStore, KvPairsStatesStore, LibraryPageStatesStore, LogsStore, SettingsPageStatesStore } from '@/types/store';
import { create } from 'zustand'; import { create } from 'zustand';
export const useBasePathsStore = create<BasePathsStore>((set) => ({ export const useBasePathsStore = create<BasePathsStore>((set) => ({
@@ -352,3 +352,12 @@ export const useLogsStore = create<LogsStore>((set) => ({
addLog: (log) => set((state) => ({ logs: [...state.logs, log] })), addLog: (log) => set((state) => ({ logs: [...state.logs, log] })),
clearLogs: () => set(() => ({ logs: [] })) clearLogs: () => set(() => ({ logs: [] }))
})); }));
export const useEnvironmentStore = create<EnvironmentStore>((set) => ({
isFlatpak: false,
isAppimage: false,
appDirPath: null,
setIsFlatpak: (isFlatpak) => set(() => ({ isFlatpak })),
setIsAppimage: (isAppimage) => set(() => ({ isAppimage })),
setAppDirPath: (path) => set(() => ({ appDirPath: path }))
}));

View File

@@ -152,3 +152,12 @@ export interface LogsStore {
addLog: (log: Log) => void; addLog: (log: Log) => void;
clearLogs: () => void; clearLogs: () => void;
} }
export interface EnvironmentStore {
isFlatpak: boolean;
isAppimage: boolean;
appDirPath: string | null;
setIsFlatpak: (isFlatpak: boolean) => void;
setIsAppimage: (isAppimage: boolean) => void;
setAppDirPath: (path: string) => void;
}