feat: Docker部署与CI/CD集成, 搜索栏修复, 上传目录改为data

This commit is contained in:
xiner
2025-11-28 18:42:30 +08:00
commit 8351d6bbfc
243 changed files with 13192 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import { NextApiResponse } from 'next';
import { AuthenticatedRequest, requireAuth, optionalAuth } from '../../../../lib/middleware/authMiddleware';
import { MaterialService } from '../../../../backend/services/materialService';
import { UserRole } from '../../../../types';
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
const { id } = req.query;
if (typeof id !== 'string') {
return res.status(400).json({ success: false, error: 'Invalid material ID' });
}
// GET: Get material by ID
if (req.method === 'GET') {
// Optional auth
await optionalAuth(req);
try {
const material = await MaterialService.getMaterialById(id);
if (!material) {
return res.status(404).json({ success: false, error: 'Material not found' });
}
return res.status(200).json({ success: true, data: material });
} catch (error) {
console.error('Error fetching material:', error);
return res.status(500).json({ success: false, error: 'Failed to fetch material' });
}
}
// DELETE: Delete material
if (req.method === 'DELETE') {
// Require authentication
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) {
return;
}
try {
// Get material to check authorization
const material = await MaterialService.getMaterialById(id);
if (!material) {
return res.status(404).json({ success: false, error: 'Material not found' });
}
// Check if user is author or admin
if (material.author.id !== req.user!.id && req.user!.role !== UserRole.ADMIN) {
return res.status(403).json({ success: false, error: 'Not authorized to delete this material' });
}
await MaterialService.deleteMaterial(id);
return res.status(200).json({ success: true, message: 'Material deleted successfully' });
} catch (error) {
console.error('Error deleting material:', error);
return res.status(500).json({ success: false, error: 'Failed to delete material' });
}
}
return res.status(405).json({ success: false, error: 'Method not allowed' });
}

View File

@@ -0,0 +1,34 @@
import { NextApiResponse } from 'next';
import { AuthenticatedRequest, requireAuth } from '../../../../../lib/middleware/authMiddleware';
import { MaterialService } from '../../../../../backend/services/materialService';
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ success: false, error: 'Method not allowed' });
}
// Require authentication
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) {
return;
}
const { id } = req.query;
const { content } = req.body;
if (typeof id !== 'string') {
return res.status(400).json({ success: false, error: 'Invalid material ID' });
}
if (!content || !content.trim()) {
return res.status(400).json({ success: false, error: 'Comment content is required' });
}
try {
const comment = await MaterialService.addComment(id, req.user!.id, content);
return res.status(201).json({ success: true, data: comment });
} catch (error) {
console.error('Error adding comment:', error);
return res.status(500).json({ success: false, error: 'Failed to add comment' });
}
}

View File

@@ -0,0 +1,29 @@
import { NextApiResponse } from 'next';
import { AuthenticatedRequest, requireAuth } from '../../../../../lib/middleware/authMiddleware';
import { MaterialService } from '../../../../../backend/services/materialService';
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ success: false, error: 'Method not allowed' });
}
// Require authentication
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) {
return;
}
const { id } = req.query;
if (typeof id !== 'string') {
return res.status(400).json({ success: false, error: 'Invalid material ID' });
}
try {
const newFavoriteCount = await MaterialService.toggleFavorite(id, req.user!.id);
return res.status(200).json({ success: true, data: { favorites: newFavoriteCount } });
} catch (error) {
console.error('Error toggling favorite:', error);
return res.status(500).json({ success: false, error: 'Failed to toggle favorite' });
}
}

View File

@@ -0,0 +1,43 @@
import { NextApiResponse } from 'next';
import { AuthenticatedRequest, requireAuth, optionalAuth } from '../../../../lib/middleware/authMiddleware';
import { MaterialService } from '../../../../backend/services/materialService';
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
// GET: List materials
if (req.method === 'GET') {
// Optional auth - attach user if logged in
await optionalAuth(req);
try {
const type = req.query.type as any;
const page = parseInt((req.query.page as string) || '1', 10) || 1;
const limit = parseInt((req.query.limit as string) || '20', 10) || 20;
const q = (req.query.q as string) || '';
const { items, total } = await MaterialService.getAllMaterials(type, page, limit, q);
const hasNext = page * limit < total;
return res.status(200).json({ success: true, data: { items, total, page, limit, hasNext } });
} catch (error) {
console.error('Error fetching materials:', error);
return res.status(500).json({ success: false, error: 'Failed to fetch materials' });
}
}
// POST: Create material
if (req.method === 'POST') {
// Require authentication
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) {
return; // requireAuth already sent the response
}
try {
const newMaterial = await MaterialService.createMaterial(req.user!.id, req.body);
return res.status(201).json({ success: true, data: newMaterial });
} catch (error) {
console.error('Error creating material:', error);
return res.status(500).json({ success: false, error: 'Failed to create material' });
}
}
return res.status(405).json({ success: false, error: 'Method not allowed' });
}

View File

@@ -0,0 +1,87 @@
import type { NextApiResponse } from 'next';
import type { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
import { requireAuth } from '../../../../lib/middleware/authMiddleware';
import { getServerConfig } from '../../../../lib/serverConfig';
import { UserRole } from '../../../../types';
import { MaterialService } from '../../../../backend/services/materialService';
import formidable, { Files, File } from 'formidable';
import fs from 'fs';
import path from 'path';
export const config = {
api: {
bodyParser: false,
},
};
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ success: false, error: 'Method not allowed' });
}
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) return;
if (req.user!.role !== UserRole.MANAGER && req.user!.role !== UserRole.ADMIN) {
return res.status(403).json({ success: false, error: 'Manager role required' });
}
const { uploadMaxMB } = getServerConfig();
const maxBytes = uploadMaxMB * 1024 * 1024;
const form = formidable({
maxFileSize: maxBytes,
multiples: false,
filter: ({ mimetype, originalFilename }) => {
// Allow common video types
const name = (originalFilename || '').toLowerCase();
return mimetype?.startsWith('video/') || name.endsWith('.mp4') || name.endsWith('.webm') || name.endsWith('.mov');
},
});
const getFirstFile = (files: Files | undefined): File | null => {
if (!files) return null;
const entries = Object.values(files || {});
if (!entries.length) return null;
const candidate = entries[0] as any;
if (Array.isArray(candidate)) return candidate[0] || null;
return candidate || null;
};
try {
const parsed: { fields: any; files: Files } = await new Promise((resolve, reject) => {
form.parse(req as any, (err, fields, files) => {
if (err) reject(err);
else resolve({ fields, files });
});
});
const { fields, files } = parsed;
const file = getFirstFile(files);
if (!file) return res.status(400).json({ success: false, error: 'No file provided' });
if ((file.size || 0) > maxBytes) return res.status(413).json({ success: false, error: `File too large. Max ${uploadMaxMB}MB` });
const uploadsDir = path.join(process.cwd(), getServerConfig().uploadDir);
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
const filename = `video_${Date.now()}_${path.basename(file.originalFilename || 'video.mp4')}`;
const destPath = path.join(uploadsDir, filename);
const srcPath = (file as any).filepath || (file as any).path;
if (!srcPath) return res.status(400).json({ success: false, error: 'Invalid upload temp path' });
await fs.promises.copyFile(srcPath, destPath);
const contentUrl = `/api/v1/files/${filename}`;
const newMaterial = await MaterialService.createMaterial(req.user!.id, {
title: fields?.title?.toString() || 'Video Asset',
description: fields?.description?.toString() || 'Uploaded video asset',
type: 'VIDEO',
contentUrl,
tags: (fields?.tags?.toString() || '').split(',').filter(Boolean),
});
return res.status(201).json({ success: true, data: newMaterial });
} catch (error: any) {
console.error('Upload VIDEO error:', error);
return res.status(500).json({ success: false, error: error.message || 'Upload failed' });
}
}

View File

@@ -0,0 +1,80 @@
import type { NextApiResponse } from 'next';
import type { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
import { requireAuth } from '../../../../lib/middleware/authMiddleware';
import { getServerConfig } from '../../../../lib/serverConfig';
import { MaterialService } from '../../../../backend/services/materialService';
import formidable, { Files, File } from 'formidable';
import fs from 'fs';
import path from 'path';
export const config = {
api: {
bodyParser: false,
},
};
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ success: false, error: 'Method not allowed' });
}
const isAuthenticated = await requireAuth(req, res);
if (!isAuthenticated) return;
const { uploadMaxMB } = getServerConfig();
const maxBytes = uploadMaxMB * 1024 * 1024;
const form = formidable({
maxFileSize: maxBytes,
multiples: false,
filter: ({ mimetype, originalFilename }) => {
return (mimetype === 'application/zip' || (originalFilename || '').toLowerCase().endsWith('.zip'));
},
});
const getFirstFile = (files: Files | undefined): File | null => {
if (!files) return null;
const entries = Object.values(files || {});
if (!entries.length) return null;
const candidate = entries[0] as any;
if (Array.isArray(candidate)) return candidate[0] || null;
return candidate || null;
};
try {
const parsed: { fields: any; files: Files } = await new Promise((resolve, reject) => {
form.parse(req as any, (err, fields, files) => {
if (err) reject(err);
else resolve({ fields, files });
});
});
const { fields, files } = parsed;
const file = getFirstFile(files);
if (!file) return res.status(400).json({ success: false, error: 'No file provided' });
if ((file.size || 0) > maxBytes) return res.status(413).json({ success: false, error: `File too large. Max ${uploadMaxMB}MB` });
const uploadsDir = path.join(process.cwd(), getServerConfig().uploadDir);
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
const filename = `zip_${Date.now()}_${path.basename(file.originalFilename || 'asset.zip')}`;
const destPath = path.join(uploadsDir, filename);
const srcPath = (file as any).filepath || (file as any).path;
if (!srcPath) return res.status(400).json({ success: false, error: 'Invalid upload temp path' });
await fs.promises.copyFile(srcPath, destPath);
const contentUrl = `/api/v1/files/${filename}`;
const newMaterial = await MaterialService.createMaterial(req.user!.id, {
title: fields?.title?.toString() || 'ZIP Asset',
description: fields?.description?.toString() || 'Uploaded ZIP asset',
type: 'ASSET_ZIP',
contentUrl,
tags: (fields?.tags?.toString() || '').split(',').filter(Boolean),
});
return res.status(201).json({ success: true, data: newMaterial });
} catch (error: any) {
console.error('Upload ZIP error:', error);
return res.status(500).json({ success: false, error: error.message || 'Upload failed' });
}
}