2026-02-23 06:31:59 +00:00
|
|
|
const express = require('express');
|
|
|
|
|
const multer = require('multer');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const { authMiddleware } = require('../middleware/auth');
|
|
|
|
|
const projectService = require('../services/projectService');
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
const storage = multer.diskStorage({
|
|
|
|
|
destination: (req, file, cb) => {
|
|
|
|
|
const uploadDir = path.join(__dirname, '../../uploads');
|
|
|
|
|
if (!fs.existsSync(uploadDir)) {
|
|
|
|
|
fs.mkdirSync(uploadDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
cb(null, uploadDir);
|
|
|
|
|
},
|
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
|
|
|
|
cb(null, uniqueSuffix + '-' + file.originalname);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const upload = multer({
|
|
|
|
|
storage,
|
|
|
|
|
limits: { fileSize: 100 * 1024 * 1024 }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get('/', authMiddleware, (req, res) => {
|
|
|
|
|
const projects = projectService.getAllProjects();
|
|
|
|
|
res.json(projects);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get('/:id', authMiddleware, (req, res) => {
|
|
|
|
|
const project = projectService.getProjectById(req.params.id);
|
|
|
|
|
if (!project) {
|
|
|
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
|
|
|
}
|
|
|
|
|
res.json(project);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/', authMiddleware, upload.array('files'), (req, res) => {
|
2026-02-27 04:31:28 +00:00
|
|
|
const { name, description, path: projectPath } = req.body;
|
2026-02-23 06:31:59 +00:00
|
|
|
const files = req.files;
|
|
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
return res.status(400).json({ error: 'Project name is required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!files || files.length === 0) {
|
|
|
|
|
return res.status(400).json({ error: 'At least one file is required' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 04:31:28 +00:00
|
|
|
let normalizedPath = projectPath ? projectPath.trim() : '';
|
|
|
|
|
if (normalizedPath && !normalizedPath.startsWith('/')) {
|
|
|
|
|
normalizedPath = '/' + normalizedPath;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 06:31:59 +00:00
|
|
|
const project = projectService.createProject({
|
|
|
|
|
name,
|
|
|
|
|
description: description || '',
|
2026-02-27 04:31:28 +00:00
|
|
|
path: normalizedPath,
|
2026-02-23 06:31:59 +00:00
|
|
|
files
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(201).json(project);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.put('/:id', authMiddleware, (req, res) => {
|
|
|
|
|
const { name, description } = req.body;
|
|
|
|
|
const project = projectService.updateProject(req.params.id, { name, description });
|
|
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(project);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.delete('/:id', authMiddleware, (req, res) => {
|
|
|
|
|
const success = projectService.deleteProject(req.params.id);
|
|
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json({ message: 'Project deleted successfully' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/:id/files', authMiddleware, upload.array('files'), (req, res) => {
|
|
|
|
|
const files = req.files;
|
|
|
|
|
|
|
|
|
|
if (!files || files.length === 0) {
|
|
|
|
|
return res.status(400).json({ error: 'No files uploaded' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const project = projectService.addFilesToProject(req.params.id, files);
|
|
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(project);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|