mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 06:39:18 +00:00
优化文件模块
This commit is contained in:
parent
806099a116
commit
7ffdeca847
@ -2,7 +2,7 @@ import express from 'express';
|
||||
import logger from './utils/core/logger';
|
||||
import paths from './utils/core/path';
|
||||
import sampleController from './modules/sample/sample.controller';
|
||||
import imageController from './modules/image/image.controller';
|
||||
import imageController from './modules/image/file.controller';
|
||||
import config from './utils/core/config';
|
||||
import './services/ws/wsServer';
|
||||
|
||||
@ -20,7 +20,7 @@ const apps = {
|
||||
|
||||
const modules = [
|
||||
{ path: '/api/sample', name: '测试模块', controller: sampleController },
|
||||
{ path: '/images', name: '图像模块', controller: imageController },
|
||||
{ path: '/files', name: '文件模块', controller: imageController },
|
||||
];
|
||||
|
||||
modules.forEach((module) => {
|
||||
|
@ -10,14 +10,13 @@ const PORT = config.get('PORT') || 3000;
|
||||
apps
|
||||
.createApp()
|
||||
.then(async (app) => {
|
||||
const isUpdated = await autoUpdater.checkForUpdates();
|
||||
if (isUpdated) {
|
||||
logger.warn(`检测到更新,正在自动重启..`);
|
||||
process.exit(0);
|
||||
}
|
||||
app.listen(PORT, () => {
|
||||
logger.info(`Crystelf-core listening on ${PORT}`);
|
||||
});
|
||||
const isUpdated = await autoUpdater.checkForUpdates();
|
||||
if (isUpdated) {
|
||||
logger.warn(`检测到更新,请重启..`);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error('Crystelf-core启动失败:', err);
|
||||
|
@ -1,15 +1,15 @@
|
||||
import express from 'express';
|
||||
import ImageService from './image.service';
|
||||
import FileService from './file.service';
|
||||
import logger from '../../utils/core/logger';
|
||||
import response from '../../utils/core/response';
|
||||
|
||||
class ImageController {
|
||||
class FileController {
|
||||
private readonly router: express.Router;
|
||||
private readonly imageService: ImageService;
|
||||
private readonly imageService: FileService;
|
||||
|
||||
constructor() {
|
||||
this.router = express.Router();
|
||||
this.imageService = new ImageService();
|
||||
this.imageService = new FileService();
|
||||
this.initializeRoutes();
|
||||
}
|
||||
|
||||
@ -18,14 +18,14 @@ class ImageController {
|
||||
}
|
||||
|
||||
private initializeRoutes(): void {
|
||||
this.router.get('*', this.handleGetImage);
|
||||
this.router.get('*', this.handleGetFile);
|
||||
}
|
||||
|
||||
private handleGetImage = async (req: express.Request, res: express.Response): Promise<void> => {
|
||||
private handleGetFile = async (req: express.Request, res: express.Response): Promise<void> => {
|
||||
try {
|
||||
const fullPath = req.params[0];
|
||||
logger.debug(`有个小可爱正在请求${fullPath}噢..`);
|
||||
const filePath = await this.imageService.getImage(fullPath);
|
||||
const filePath = await this.imageService.getFile(fullPath);
|
||||
if (!filePath) {
|
||||
logger.warn(`${fullPath}:文件不存在..`);
|
||||
await response.error(res, '文件不存在啦!', 404);
|
||||
@ -35,10 +35,10 @@ class ImageController {
|
||||
res.sendFile(filePath);
|
||||
logger.info(`成功投递文件: ${filePath}`);
|
||||
} catch (error) {
|
||||
await response.error(res, '晶灵服务处理图像请求时出错..', 500);
|
||||
logger.error('晶灵图像请求处理失败:', error);
|
||||
await response.error(res, '晶灵服务处理文件请求时出错..', 500);
|
||||
logger.error('晶灵数据请求处理失败:', error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default new ImageController();
|
||||
export default new FileController();
|
@ -3,20 +3,20 @@ import fs from 'fs';
|
||||
import paths from '../../utils/core/path';
|
||||
import logger from '../../utils/core/logger';
|
||||
|
||||
class ImageService {
|
||||
private readonly imagePath: string;
|
||||
class FileService {
|
||||
private readonly filePath: string;
|
||||
|
||||
constructor() {
|
||||
this.imagePath = paths.get('images');
|
||||
logger.info(`晶灵云图数据中心初始化..数据存储在: ${this.imagePath}`);
|
||||
this.filePath = paths.get('files');
|
||||
logger.info(`晶灵云图数据中心初始化..数据存储在: ${this.filePath}`);
|
||||
}
|
||||
|
||||
public async getImage(relativePath: string): Promise<string | null> {
|
||||
public async getFile(relativePath: string): Promise<string | null> {
|
||||
if (!this.isValidPath(relativePath) && !this.isValidFilename(path.basename(relativePath))) {
|
||||
throw new Error('非法路径请求');
|
||||
}
|
||||
|
||||
const filePath = path.join(this.imagePath, relativePath);
|
||||
const filePath = path.join(this.filePath, relativePath);
|
||||
logger.debug(`尝试访问图像路径: ${filePath}`);
|
||||
|
||||
return fs.existsSync(filePath) ? filePath : null;
|
||||
@ -39,4 +39,4 @@ class ImageService {
|
||||
}
|
||||
}
|
||||
|
||||
export default ImageService;
|
||||
export default FileService;
|
@ -29,11 +29,13 @@ class PathManager {
|
||||
const mappings: Record<PathType, string> = {
|
||||
root: this.baseDir,
|
||||
public: path.join(this.baseDir, 'public'),
|
||||
images: path.join(this.baseDir, 'public/images'),
|
||||
images: path.join(this.baseDir, 'public/files/image'),
|
||||
log: path.join(this.baseDir, 'logs'),
|
||||
config: path.join(this.baseDir, 'config'),
|
||||
temp: path.join(this.baseDir, 'temp'),
|
||||
userData: path.join(this.baseDir, 'private/data'),
|
||||
files: path.join(this.baseDir, 'public/files'),
|
||||
media: path.join(this.baseDir, 'public/files/media'),
|
||||
};
|
||||
|
||||
return type ? mappings[type] : this.baseDir;
|
||||
@ -46,11 +48,13 @@ class PathManager {
|
||||
const logPath = this.get('log');
|
||||
const imagePath = this.get('images');
|
||||
const dataPath = this.get('userData');
|
||||
const mediaPath = this.get('media');
|
||||
fc.createDir(logPath, false);
|
||||
fc.createDir(imagePath, false);
|
||||
fc.createDir(mediaPath, false);
|
||||
fc.createDir(dataPath, false);
|
||||
logger.debug(`日志目录初始化: ${logPath}`);
|
||||
logger.debug(`图像目录初始化: ${imagePath}`);
|
||||
logger.debug(`图像目录初始化: ${imagePath};${mediaPath}`);
|
||||
logger.debug(`用户数据目录初始化: ${dataPath}`);
|
||||
}
|
||||
|
||||
@ -74,7 +78,16 @@ class PathManager {
|
||||
}
|
||||
}
|
||||
|
||||
type PathType = 'root' | 'public' | 'images' | 'log' | 'config' | 'temp' | 'userData';
|
||||
type PathType =
|
||||
| 'root'
|
||||
| 'public'
|
||||
| 'images'
|
||||
| 'log'
|
||||
| 'config'
|
||||
| 'temp'
|
||||
| 'userData'
|
||||
| 'files'
|
||||
| 'media';
|
||||
|
||||
const paths = PathManager.getInstance();
|
||||
export default paths;
|
||||
|
Loading…
x
Reference in New Issue
Block a user