This commit is contained in:
Baobhan Sith
2025-04-26 15:26:14 +08:00
parent e269f40754
commit 378be55e8f
6 changed files with 135 additions and 44 deletions
+7 -29
View File
@@ -1,63 +1,41 @@
# Stage 1: Build the application using npm workspaces
FROM node:20-alpine AS builder FROM node:20-alpine AS builder
WORKDIR /app WORKDIR /app
# Copy root package.json and package-lock.json
COPY package.json 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/backend/package.json ./packages/backend/
COPY packages/frontend/package.json ./packages/frontend/ 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 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/src ./packages/backend/src
COPY packages/backend/tsconfig.json ./packages/backend/ 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 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 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++ RUN apk add --no-cache --virtual .build-deps python3 make g++
WORKDIR /app 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/dist ./dist
COPY --from=builder /app/packages/backend/package.json ./package.json 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 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 RUN npm install --omit=dev --prefer-offline
# Remove temporary build dependencies after npm install
RUN apk del .build-deps RUN apk del .build-deps
# Expose the port the app runs on
EXPOSE 3001 EXPOSE 3001
# Define the command to run the application
CMD ["node", "dist/index.js"]
# Note: Environment variables like ENCRYPTION_KEY, SESSION_SECRET, and PORT CMD ["node", "dist/index.js"]
# should be provided when running the container.
@@ -124,3 +124,26 @@ export const uploadTerminalBackgroundController = async (req: Request, res: Resp
// 导出 multer 中间件以便在路由中使用 // 导出 multer 中间件以便在路由中使用
export const uploadPageBackgroundMiddleware = backgroundUpload.single('pageBackgroundFile'); export const uploadPageBackgroundMiddleware = backgroundUpload.single('pageBackgroundFile');
export const uploadTerminalBackgroundMiddleware = backgroundUpload.single('terminalBackgroundFile'); export const uploadTerminalBackgroundMiddleware = backgroundUpload.single('terminalBackgroundFile');
/**
* 移除页面背景图片
*/
export const removePageBackgroundController = async (req: Request, res: Response): Promise<void> => {
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<void> => {
try {
await appearanceService.removeTerminalBackground();
res.status(200).json({ message: '终端背景已移除' });
} catch (error: any) {
res.status(500).json({ message: '移除终端背景失败', error: error.message });
}
};
@@ -27,6 +27,10 @@ router.post(
appearanceController.uploadTerminalBackgroundController 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; export default router;
@@ -1,3 +1,5 @@
import fs from 'fs/promises'; // 使用 promises API
import path from 'path';
import * as appearanceRepository from '../repositories/appearance.repository'; import * as appearanceRepository from '../repositories/appearance.repository';
import { AppearanceSettings, UpdateAppearanceDto } from '../types/appearance.types'; import { AppearanceSettings, UpdateAppearanceDto } from '../types/appearance.types';
import * as terminalThemeRepository from '../repositories/terminal-theme.repository'; import * as terminalThemeRepository from '../repositories/terminal-theme.repository';
@@ -66,5 +68,73 @@ export const updateSettings = async (settingsDto: UpdateAppearanceDto): Promise<
return appearanceRepository.updateAppearanceSettings(settingsDto); return appearanceRepository.updateAppearanceSettings(settingsDto);
}; };
/**
* 移除页面背景图片
* 1. 获取当前设置中的文件路径
* 2. 如果路径存在,删除文件系统中的文件
* 3. 更新数据库中的路径为空字符串
*/
export const removePageBackground = async (): Promise<boolean> => {
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<boolean> => {
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: '' });
};
+12 -12
View File
@@ -1,16 +1,16 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2016", // Or a newer target like ES2020 "target": "ES2016",
"module": "NodeNext", // Use NodeNext module system "module": "NodeNext",
"moduleResolution": "NodeNext", // Use NodeNext resolution strategy "moduleResolution": "NodeNext",
"outDir": "./dist", // Output directory for compiled JS "outDir": "./dist",
"rootDir": "./src", // Root directory of source files "rootDir": "./src",
"esModuleInterop": true, // Enables compatibility with CommonJS modules "esModuleInterop": true,
"forceConsistentCasingInFileNames": true, // Enforce consistent file casing "forceConsistentCasingInFileNames": true,
"strict": true, // Enable all strict type-checking options "strict": true,
"skipLibCheck": true, // Skip type checking of declaration files "skipLibCheck": true,
"resolveJsonModule": true // Allow importing JSON files "resolveJsonModule": true
}, },
"include": ["src/**/*"], // Include all files in the src directory "include": ["src/**/*"],
"exclude": ["node_modules", "dist"] // Exclude node_modules and dist "exclude": ["node_modules", "dist"]
} }
@@ -427,14 +427,30 @@ export const useAppearanceStore = defineStore('appearance', () => {
* 移除页面背景 * 移除页面背景
*/ */
async function removePageBackground() { 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() { 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 || '移除终端背景失败');
}
} }