fix: cannot move data dir in linux (#7643)

* fix: cannot move data dir in linux

* delete verion info in path

---------

Co-authored-by: beyondkmp <beyondkmp@debian12.beyondkmp.com>
This commit is contained in:
beyondkmp 2025-07-03 13:07:13 +08:00 committed by GitHub
parent 01fc98b221
commit c79ea7d5ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import fs from 'node:fs'
import { arch } from 'node:os' import { arch } from 'node:os'
import path from 'node:path' import path from 'node:path'
import { isMac, isWin } from '@main/constant' import { isLinux, isMac, isWin } from '@main/constant'
import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process' import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process'
import { handleZoomFactor } from '@main/utils/zoom' import { handleZoomFactor } from '@main/utils/zoom'
import { UpgradeChannel } from '@shared/config/constant' import { UpgradeChannel } from '@shared/config/constant'
@ -306,6 +306,17 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
// Relaunch app // Relaunch app
ipcMain.handle(IpcChannel.App_RelaunchApp, (_, options?: Electron.RelaunchOptions) => { ipcMain.handle(IpcChannel.App_RelaunchApp, (_, options?: Electron.RelaunchOptions) => {
// Fix for .AppImage
if (isLinux && process.env.APPIMAGE) {
log.info('Relaunching app with options:', process.env.APPIMAGE, options)
// On Linux, we need to use the APPIMAGE environment variable to relaunch
// https://github.com/electron-userland/electron-builder/issues/1727#issuecomment-769896927
options = options || {}
options.execPath = process.env.APPIMAGE
options.args = options.args || []
options.args.unshift('--appimage-extract-and-run')
}
app.relaunch(options) app.relaunch(options)
app.exit(0) app.exit(0)
}) })

View File

@ -2,7 +2,7 @@ import * as fs from 'node:fs'
import os from 'node:os' import os from 'node:os'
import path from 'node:path' import path from 'node:path'
import { isPortable } from '@main/constant' import { isLinux, isPortable } from '@main/constant'
import { audioExts, documentExts, imageExts, textExts, videoExts } from '@shared/config/constant' import { audioExts, documentExts, imageExts, textExts, videoExts } from '@shared/config/constant'
import { FileType, FileTypes } from '@types' import { FileType, FileTypes } from '@types'
import { app } from 'electron' import { app } from 'electron'
@ -59,6 +59,13 @@ function getAppDataPathFromConfig() {
return null return null
} }
let executablePath = app.getPath('exe')
if (isLinux && process.env.APPIMAGE) {
// 如果是 AppImage 打包的应用,直接使用 APPIMAGE 环境变量
// 这样可以确保获取到正确的可执行文件路径
executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage')
}
let appDataPath = null let appDataPath = null
// 兼容旧版本 // 兼容旧版本
if (config.appDataPath && typeof config.appDataPath === 'string') { if (config.appDataPath && typeof config.appDataPath === 'string') {
@ -67,7 +74,7 @@ function getAppDataPathFromConfig() {
appDataPath && updateAppDataConfig(appDataPath) appDataPath && updateAppDataConfig(appDataPath)
} else { } else {
appDataPath = config.appDataPath.find( appDataPath = config.appDataPath.find(
(item: { executablePath: string }) => item.executablePath === app.getPath('exe') (item: { executablePath: string }) => item.executablePath === executablePath
)?.dataPath )?.dataPath
} }
@ -90,11 +97,13 @@ export function updateAppDataConfig(appDataPath: string) {
// config.json // config.json
// appDataPath: [{ executablePath: string, dataPath: string }] // appDataPath: [{ executablePath: string, dataPath: string }]
const configPath = path.join(getConfigDir(), 'config.json') const configPath = path.join(getConfigDir(), 'config.json')
let executablePath = app.getPath('exe')
if (isLinux && process.env.APPIMAGE) {
executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage')
}
if (!fs.existsSync(configPath)) { if (!fs.existsSync(configPath)) {
fs.writeFileSync( fs.writeFileSync(configPath, JSON.stringify({ appDataPath: [{ executablePath, dataPath: appDataPath }] }, null, 2))
configPath,
JSON.stringify({ appDataPath: [{ executablePath: app.getPath('exe'), dataPath: appDataPath }] }, null, 2)
)
return return
} }
@ -104,13 +113,13 @@ export function updateAppDataConfig(appDataPath: string) {
} }
const existingPath = config.appDataPath.find( const existingPath = config.appDataPath.find(
(item: { executablePath: string }) => item.executablePath === app.getPath('exe') (item: { executablePath: string }) => item.executablePath === executablePath
) )
if (existingPath) { if (existingPath) {
existingPath.dataPath = appDataPath existingPath.dataPath = appDataPath
} else { } else {
config.appDataPath.push({ executablePath: app.getPath('exe'), dataPath: appDataPath }) config.appDataPath.push({ executablePath, dataPath: appDataPath })
} }
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)) fs.writeFileSync(configPath, JSON.stringify(config, null, 2))