From 378be55e8fb2e6ce102264f2ddd5dda671a416c6 Mon Sep 17 00:00:00 2001 From: Baobhan Sith <80159437+Heavrnl@users.noreply.github.com> Date: Sat, 26 Apr 2025 15:26:14 +0800 Subject: [PATCH] update --- packages/backend/Dockerfile | 36 ++-------- .../src/appearance/appearance.controller.ts | 23 ++++++ .../src/appearance/appearance.routes.ts | 6 +- .../src/services/appearance.service.ts | 70 +++++++++++++++++++ packages/backend/tsconfig.json | 24 +++---- .../frontend/src/stores/appearance.store.ts | 20 +++++- 6 files changed, 135 insertions(+), 44 deletions(-) diff --git a/packages/backend/Dockerfile b/packages/backend/Dockerfile index 9dd72ee..1600d35 100644 --- a/packages/backend/Dockerfile +++ b/packages/backend/Dockerfile @@ -1,63 +1,41 @@ -# Stage 1: Build the application using npm workspaces FROM node:20-alpine AS builder WORKDIR /app -# Copy root package.json and package-lock.json + COPY package.json package-lock.json* ./ -# Copy backend package definition and frontend (needed for workspace install) -# We need frontend's package.json for npm ci to resolve the workspace correctly + COPY packages/backend/package.json ./packages/backend/ COPY packages/frontend/package.json ./packages/frontend/ -# If there are other packages backend depends on, copy their package.json too -# COPY packages/types/package.json ./packages/types/ # Example if types existed and was a package -# Install ALL workspace dependencies using the root lock file -# This ensures all dependencies are correctly installed according to the lock file RUN npm ci -# Copy the rest of the source code needed for the backend build COPY packages/backend/src ./packages/backend/src COPY packages/backend/tsconfig.json ./packages/backend/ -# Build only the backend package -# Use --workspace flag to specify which package to build RUN npm run build --workspace=@nexus-terminal/backend -# Prune devDependencies for the entire workspace might be complex. -# Instead, we'll copy only the necessary production files in the next stage. - -# Stage 2: Create the final production image FROM node:20-alpine -# Install build dependencies needed for native addons like sqlite3 on Alpine -# python3, make, g++ are common requirements. -# We install them temporarily, run npm install, then remove them. + RUN apk add --no-cache --virtual .build-deps python3 make g++ WORKDIR /app -# Copy built backend code and package definitions from builder stage + COPY --from=builder /app/packages/backend/dist ./dist COPY --from=builder /app/packages/backend/package.json ./package.json -# Copy the root lock file for consistent installs COPY --from=builder /app/package-lock.json ./package-lock.json -# Install ONLY production dependencies. Native addons like sqlite3 should be rebuilt here. -# Removed --ignore-scripts to allow sqlite3's install script to run. -# Using --omit=dev is the modern equivalent of --production. RUN npm install --omit=dev --prefer-offline -# Remove temporary build dependencies after npm install + RUN apk del .build-deps -# Expose the port the app runs on + EXPOSE 3001 -# Define the command to run the application -CMD ["node", "dist/index.js"] -# Note: Environment variables like ENCRYPTION_KEY, SESSION_SECRET, and PORT -# should be provided when running the container. \ No newline at end of file +CMD ["node", "dist/index.js"] diff --git a/packages/backend/src/appearance/appearance.controller.ts b/packages/backend/src/appearance/appearance.controller.ts index eb47d93..d323b6a 100644 --- a/packages/backend/src/appearance/appearance.controller.ts +++ b/packages/backend/src/appearance/appearance.controller.ts @@ -124,3 +124,26 @@ export const uploadTerminalBackgroundController = async (req: Request, res: Resp // 导出 multer 中间件以便在路由中使用 export const uploadPageBackgroundMiddleware = backgroundUpload.single('pageBackgroundFile'); export const uploadTerminalBackgroundMiddleware = backgroundUpload.single('terminalBackgroundFile'); +/** + * 移除页面背景图片 + */ +export const removePageBackgroundController = async (req: Request, res: Response): Promise => { + try { + await appearanceService.removePageBackground(); + res.status(200).json({ message: '页面背景已移除' }); + } catch (error: any) { + res.status(500).json({ message: '移除页面背景失败', error: error.message }); + } +}; + +/** + * 移除终端背景图片 + */ +export const removeTerminalBackgroundController = async (req: Request, res: Response): Promise => { + try { + await appearanceService.removeTerminalBackground(); + res.status(200).json({ message: '终端背景已移除' }); + } catch (error: any) { + res.status(500).json({ message: '移除终端背景失败', error: error.message }); + } +}; diff --git a/packages/backend/src/appearance/appearance.routes.ts b/packages/backend/src/appearance/appearance.routes.ts index 1c79109..c31c670 100644 --- a/packages/backend/src/appearance/appearance.routes.ts +++ b/packages/backend/src/appearance/appearance.routes.ts @@ -27,6 +27,10 @@ router.post( appearanceController.uploadTerminalBackgroundController ); -// TODO: 可能需要添加删除背景图片的路由 +// DELETE /api/v1/appearance/background/page - 删除页面背景图片 +router.delete('/background/page', appearanceController.removePageBackgroundController); + +// DELETE /api/v1/appearance/background/terminal - 删除终端背景图片 +router.delete('/background/terminal', appearanceController.removeTerminalBackgroundController); export default router; diff --git a/packages/backend/src/services/appearance.service.ts b/packages/backend/src/services/appearance.service.ts index a3e0763..aa232ed 100644 --- a/packages/backend/src/services/appearance.service.ts +++ b/packages/backend/src/services/appearance.service.ts @@ -1,3 +1,5 @@ +import fs from 'fs/promises'; // 使用 promises API +import path from 'path'; import * as appearanceRepository from '../repositories/appearance.repository'; import { AppearanceSettings, UpdateAppearanceDto } from '../types/appearance.types'; import * as terminalThemeRepository from '../repositories/terminal-theme.repository'; @@ -66,5 +68,73 @@ export const updateSettings = async (settingsDto: UpdateAppearanceDto): Promise< return appearanceRepository.updateAppearanceSettings(settingsDto); }; +/** + * 移除页面背景图片 + * 1. 获取当前设置中的文件路径 + * 2. 如果路径存在,删除文件系统中的文件 + * 3. 更新数据库中的路径为空字符串 + */ +export const removePageBackground = async (): Promise => { + const currentSettings = await getSettings(); + const filePath = currentSettings.pageBackgroundImage; + + if (filePath) { + // 构建文件的绝对路径 + // 注意:这里的路径拼接逻辑需要与上传时的逻辑一致 + // 假设 filePath 是相对于项目根目录的 /uploads/backgrounds/xxx + const absolutePath = path.join(__dirname, '../../', filePath); // 调整相对路径层级 + + try { + await fs.unlink(absolutePath); + console.log(`[AppearanceService] 已删除页面背景文件: ${absolutePath}`); + } catch (error: any) { + // 如果文件不存在或其他删除错误,记录日志但继续执行以清空数据库记录 + if (error.code === 'ENOENT') { + console.warn(`[AppearanceService] 尝试删除页面背景文件但未找到: ${absolutePath}`); + } else { + console.error(`[AppearanceService] 删除页面背景文件时出错 (${absolutePath}):`, error); + // 可以选择抛出错误,或者仅记录并继续 + // throw new Error(`删除页面背景文件失败: ${error.message}`); + } + } + } else { + console.log('[AppearanceService] 没有页面背景文件路径需要删除。'); + } + + // 无论文件删除是否成功(或文件是否存在),都尝试清空数据库记录 + return updateSettings({ pageBackgroundImage: '' }); +}; + +/** + * 移除终端背景图片 + * 1. 获取当前设置中的文件路径 + * 2. 如果路径存在,删除文件系统中的文件 + * 3. 更新数据库中的路径为空字符串 + */ +export const removeTerminalBackground = async (): Promise => { + const currentSettings = await getSettings(); + const filePath = currentSettings.terminalBackgroundImage; + + if (filePath) { + const absolutePath = path.join(__dirname, '../../', filePath); // 调整相对路径层级 + + try { + await fs.unlink(absolutePath); + console.log(`[AppearanceService] 已删除终端背景文件: ${absolutePath}`); + } catch (error: any) { + if (error.code === 'ENOENT') { + console.warn(`[AppearanceService] 尝试删除终端背景文件但未找到: ${absolutePath}`); + } else { + console.error(`[AppearanceService] 删除终端背景文件时出错 (${absolutePath}):`, error); + // throw new Error(`删除终端背景文件失败: ${error.message}`); + } + } + } else { + console.log('[AppearanceService] 没有终端背景文件路径需要删除。'); + } + + // 无论文件删除是否成功(或文件是否存在),都尝试清空数据库记录 + return updateSettings({ terminalBackgroundImage: '' }); +}; diff --git a/packages/backend/tsconfig.json b/packages/backend/tsconfig.json index 5d0823e..0b4cece 100644 --- a/packages/backend/tsconfig.json +++ b/packages/backend/tsconfig.json @@ -1,16 +1,16 @@ { "compilerOptions": { - "target": "ES2016", // Or a newer target like ES2020 - "module": "NodeNext", // Use NodeNext module system - "moduleResolution": "NodeNext", // Use NodeNext resolution strategy - "outDir": "./dist", // Output directory for compiled JS - "rootDir": "./src", // Root directory of source files - "esModuleInterop": true, // Enables compatibility with CommonJS modules - "forceConsistentCasingInFileNames": true, // Enforce consistent file casing - "strict": true, // Enable all strict type-checking options - "skipLibCheck": true, // Skip type checking of declaration files - "resolveJsonModule": true // Allow importing JSON files + "target": "ES2016", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "outDir": "./dist", + "rootDir": "./src", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "resolveJsonModule": true }, - "include": ["src/**/*"], // Include all files in the src directory - "exclude": ["node_modules", "dist"] // Exclude node_modules and dist + "include": ["src/**/*"], + "exclude": ["node_modules", "dist"] } diff --git a/packages/frontend/src/stores/appearance.store.ts b/packages/frontend/src/stores/appearance.store.ts index a5073ac..2a3396e 100644 --- a/packages/frontend/src/stores/appearance.store.ts +++ b/packages/frontend/src/stores/appearance.store.ts @@ -427,14 +427,30 @@ export const useAppearanceStore = defineStore('appearance', () => { * 移除页面背景 */ async function removePageBackground() { - await updateAppearanceSettings({ pageBackgroundImage: '' }); // 设置为空字符串或其他表示移除的值 + try { + // 先调用后端删除接口 + await apiClient.delete('/appearance/background/page'); + // 成功后再更新数据库记录 + await updateAppearanceSettings({ pageBackgroundImage: '' }); + } catch (err: any) { + console.error('移除页面背景失败:', err); + throw new Error(err.response?.data?.message || err.message || '移除页面背景失败'); + } } /** * 移除终端背景 */ async function removeTerminalBackground() { - await updateAppearanceSettings({ terminalBackgroundImage: '' }); + try { + // 先调用后端删除接口 + await apiClient.delete('/appearance/background/terminal'); + // 成功后再更新数据库记录 + await updateAppearanceSettings({ terminalBackgroundImage: '' }); + } catch (err: any) { + console.error('移除终端背景失败:', err); + throw new Error(err.response?.data?.message || err.message || '移除终端背景失败'); + } }