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
@@ -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<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
);
// 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;
@@ -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<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: '' });
};