refactor: Extract file path utility functions

Move hardcoded file path generation logic to dedicated utility functions in file.ts, improving code modularity and reducing duplication across services and IPC handlers
This commit is contained in:
kangfenmao 2025-03-09 17:33:46 +08:00
parent 262213cc8b
commit 9e9c954560
4 changed files with 18 additions and 8 deletions

View File

@ -1,5 +1,4 @@
import fs from 'node:fs' import fs from 'node:fs'
import path from 'node:path'
import { MCPServer, Shortcut, ThemeMode } from '@types' import { MCPServer, Shortcut, ThemeMode } from '@types'
import { BrowserWindow, ipcMain, ProxyConfig, session, shell } from 'electron' import { BrowserWindow, ipcMain, ProxyConfig, session, shell } from 'electron'
@ -20,6 +19,7 @@ import { TrayService } from './services/TrayService'
import { windowService } from './services/WindowService' import { windowService } from './services/WindowService'
import { getResourcePath } from './utils' import { getResourcePath } from './utils'
import { decrypt, encrypt } from './utils/aes' import { decrypt, encrypt } from './utils/aes'
import { getFilesDir } from './utils/file'
import { compress, decompress } from './utils/zip' import { compress, decompress } from './utils/zip'
const fileManager = new FileStorage() const fileManager = new FileStorage()
@ -34,7 +34,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
version: app.getVersion(), version: app.getVersion(),
isPackaged: app.isPackaged, isPackaged: app.isPackaged,
appPath: app.getAppPath(), appPath: app.getAppPath(),
filesPath: path.join(app.getPath('userData'), 'Data', 'Files'), filesPath: getFilesDir(),
appDataPath: app.getPath('userData'), appDataPath: app.getPath('userData'),
resourcesPath: getResourcePath(), resourcesPath: getResourcePath(),
logsPath: log.transports.file.getFile().path logsPath: log.transports.file.getFile().path

View File

@ -1,9 +1,8 @@
import { getFileType } from '@main/utils/file' import { getFilesDir, getFileType, getTempDir } from '@main/utils/file'
import { documentExts, imageExts } from '@shared/config/constant' import { documentExts, imageExts } from '@shared/config/constant'
import { FileType } from '@types' import { FileType } from '@types'
import * as crypto from 'crypto' import * as crypto from 'crypto'
import { import {
app,
dialog, dialog,
OpenDialogOptions, OpenDialogOptions,
OpenDialogReturnValue, OpenDialogReturnValue,
@ -21,8 +20,8 @@ import { chdir } from 'process'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
class FileStorage { class FileStorage {
private storageDir = path.join(app.getPath('userData'), 'Data', 'Files') private storageDir = getFilesDir()
private tempDir = path.join(app.getPath('temp'), 'CherryStudio') private tempDir = getTempDir()
constructor() { constructor() {
this.initStorageDir() this.initStorageDir()

View File

@ -1,9 +1,10 @@
import { is } from '@electron-toolkit/utils' import { is } from '@electron-toolkit/utils'
import { isLinux, isWin } from '@main/constant' import { isLinux, isWin } from '@main/constant'
import { getFilesDir } from '@main/utils/file'
import { app, BrowserWindow, ipcMain, Menu, MenuItem, shell } from 'electron' import { app, BrowserWindow, ipcMain, Menu, MenuItem, shell } from 'electron'
import Logger from 'electron-log' import Logger from 'electron-log'
import windowStateKeeper from 'electron-window-state' import windowStateKeeper from 'electron-window-state'
import path, { join } from 'path' import { join } from 'path'
import icon from '../../../build/icon.png?asset' import icon from '../../../build/icon.png?asset'
import { titleBarOverlayDark, titleBarOverlayLight } from '../config' import { titleBarOverlayDark, titleBarOverlayLight } from '../config'
@ -196,7 +197,7 @@ export class WindowService {
if (url.includes('http://file/')) { if (url.includes('http://file/')) {
const fileName = url.replace('http://file/', '') const fileName = url.replace('http://file/', '')
const storageDir = path.join(app.getPath('userData'), 'Data', 'Files') const storageDir = getFilesDir()
const filePath = storageDir + '/' + fileName const filePath = storageDir + '/' + fileName
shell.openPath(filePath).catch((err) => Logger.error('Failed to open file:', err)) shell.openPath(filePath).catch((err) => Logger.error('Failed to open file:', err))
} else { } else {

View File

@ -3,6 +3,7 @@ import path from 'node:path'
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 { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
export function getFileType(ext: string): FileTypes { export function getFileType(ext: string): FileTypes {
@ -14,6 +15,7 @@ export function getFileType(ext: string): FileTypes {
if (documentExts.includes(ext)) return FileTypes.DOCUMENT if (documentExts.includes(ext)) return FileTypes.DOCUMENT
return FileTypes.OTHER return FileTypes.OTHER
} }
export function getAllFiles(dirPath: string, arrayOfFiles: FileType[] = []): FileType[] { export function getAllFiles(dirPath: string, arrayOfFiles: FileType[] = []): FileType[] {
const files = fs.readdirSync(dirPath) const files = fs.readdirSync(dirPath)
@ -54,3 +56,11 @@ export function getAllFiles(dirPath: string, arrayOfFiles: FileType[] = []): Fil
return arrayOfFiles return arrayOfFiles
} }
export function getTempDir() {
return path.join(app.getPath('temp'), 'CherryStudio')
}
export function getFilesDir() {
return path.join(app.getPath('userData'), 'Data', 'Files')
}