新增頭像圖片上傳
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
// =====================================================
|
||||
// 調試競賽數據 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { DatabaseServiceBase } from '@/lib/services/database-service';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const competitionId = searchParams.get('competitionId') || '07e2303e-9647-11f0-b5d9-6e36c63cdb98';
|
||||
|
||||
console.log('🔍 開始調試競賽數據...');
|
||||
console.log('競賽ID:', competitionId);
|
||||
|
||||
// 1. 檢查競賽是否存在
|
||||
const competitionCheck = await DatabaseServiceBase.safeQuery(
|
||||
'SELECT * FROM competitions WHERE id = ?',
|
||||
[competitionId]
|
||||
);
|
||||
|
||||
console.log('📊 競賽檢查結果:', competitionCheck);
|
||||
|
||||
// 2. 檢查競賽應用關聯
|
||||
const competitionAppsCheck = await DatabaseServiceBase.safeQuery(
|
||||
'SELECT * FROM competition_apps WHERE competition_id = ?',
|
||||
[competitionId]
|
||||
);
|
||||
|
||||
console.log('📊 競賽應用關聯檢查結果:', competitionAppsCheck);
|
||||
|
||||
// 3. 檢查所有應用程式
|
||||
const allAppsCheck = await DatabaseServiceBase.safeQuery(
|
||||
'SELECT id, name, is_active FROM apps WHERE is_active = 1 LIMIT 10',
|
||||
[]
|
||||
);
|
||||
|
||||
console.log('📊 所有應用程式檢查結果:', allAppsCheck);
|
||||
|
||||
// 4. 檢查競賽規則
|
||||
const competitionRulesCheck = await DatabaseServiceBase.safeQuery(
|
||||
'SELECT * FROM competition_rules WHERE competition_id = ?',
|
||||
[competitionId]
|
||||
);
|
||||
|
||||
console.log('📊 競賽規則檢查結果:', competitionRulesCheck);
|
||||
|
||||
// 5. 檢查評審
|
||||
const judgesCheck = await DatabaseServiceBase.safeQuery(
|
||||
'SELECT id, name, title, department FROM judges WHERE is_active = 1 LIMIT 5',
|
||||
[]
|
||||
);
|
||||
|
||||
console.log('📊 評審檢查結果:', judgesCheck);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '調試數據獲取成功',
|
||||
data: {
|
||||
competition: competitionCheck,
|
||||
competitionApps: competitionAppsCheck,
|
||||
allApps: allAppsCheck,
|
||||
competitionRules: competitionRulesCheck,
|
||||
judges: judgesCheck
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 調試競賽數據失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '調試競賽數據失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
// =====================================================
|
||||
// 強制清理連線 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
import { connectionMonitor } from '@/lib/connection-monitor';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
console.log('🧹 開始強制清理資料庫連線...');
|
||||
|
||||
// 獲取清理前的連線狀態
|
||||
const beforeStats = await connectionMonitor.getConnectionStats();
|
||||
console.log(`清理前連線數: ${beforeStats.activeConnections}`);
|
||||
|
||||
// 強制關閉連線池
|
||||
try {
|
||||
await db.close();
|
||||
console.log('✅ 主要資料庫連線池已關閉');
|
||||
} catch (error) {
|
||||
console.error('❌ 關閉主要連線池失敗:', error);
|
||||
}
|
||||
|
||||
// 等待一段時間讓連線完全關閉
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 重新初始化連線池
|
||||
try {
|
||||
// 重新創建連線池實例
|
||||
const { Database } = await import('@/lib/database');
|
||||
const newDb = Database.getInstance();
|
||||
console.log('✅ 資料庫連線池已重新初始化');
|
||||
} catch (error) {
|
||||
console.error('❌ 重新初始化連線池失敗:', error);
|
||||
}
|
||||
|
||||
// 獲取清理後的連線狀態
|
||||
const afterStats = await connectionMonitor.getConnectionStats();
|
||||
console.log(`清理後連線數: ${afterStats.activeConnections}`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制清理完成',
|
||||
data: {
|
||||
before: {
|
||||
activeConnections: beforeStats.activeConnections,
|
||||
usagePercentage: beforeStats.usagePercentage
|
||||
},
|
||||
after: {
|
||||
activeConnections: afterStats.activeConnections,
|
||||
usagePercentage: afterStats.usagePercentage
|
||||
},
|
||||
cleaned: beforeStats.activeConnections - afterStats.activeConnections
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('強制清理失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '強制清理失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
// =====================================================
|
||||
// 強制終止連線 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
console.log('💀 開始強制終止所有資料庫連線...');
|
||||
|
||||
// 獲取所有 AI_Platform 的連線
|
||||
const connections = await db.query(`
|
||||
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
console.log(`找到 ${connections.length} 個 AI_Platform 連線`);
|
||||
|
||||
const killedConnections = [];
|
||||
|
||||
// 終止每個連線
|
||||
for (const conn of connections) {
|
||||
try {
|
||||
await db.query(`KILL CONNECTION ${conn.ID}`);
|
||||
killedConnections.push({
|
||||
id: conn.ID,
|
||||
host: conn.HOST,
|
||||
time: conn.TIME,
|
||||
command: conn.COMMAND
|
||||
});
|
||||
console.log(`✅ 已終止連線 ${conn.ID} (閒置 ${conn.TIME} 秒)`);
|
||||
} catch (error) {
|
||||
console.error(`❌ 終止連線 ${conn.ID} 失敗:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// 等待連線完全關閉
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 檢查剩餘連線
|
||||
const remainingConnections = await db.query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
const remainingCount = remainingConnections[0]?.count || 0;
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制終止連線完成',
|
||||
data: {
|
||||
totalFound: connections.length,
|
||||
killed: killedConnections.length,
|
||||
remaining: remainingCount,
|
||||
killedConnections: killedConnections
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('強制終止連線失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '強制終止連線失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// =====================================================
|
||||
// 連線測試 API - 驗證連線釋放
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { connectionMonitor } from '@/lib/connection-monitor';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// 獲取測試前的連線狀態
|
||||
const beforeStats = await connectionMonitor.getConnectionStats();
|
||||
|
||||
// 執行一些測試查詢
|
||||
const testQueries = [
|
||||
'SELECT 1 as test1',
|
||||
'SELECT 2 as test2',
|
||||
'SELECT 3 as test3',
|
||||
'SELECT COUNT(*) as user_count FROM users',
|
||||
'SELECT COUNT(*) as app_count FROM apps'
|
||||
];
|
||||
|
||||
console.log('🧪 開始連線測試...');
|
||||
console.log(`測試前連線數: ${beforeStats.activeConnections}`);
|
||||
|
||||
// 執行測試查詢
|
||||
for (let i = 0; i < testQueries.length; i++) {
|
||||
const { db } = await import('@/lib/database');
|
||||
await db.query(testQueries[i]);
|
||||
console.log(`✅ 完成測試查詢 ${i + 1}`);
|
||||
}
|
||||
|
||||
// 等待一小段時間讓連線釋放
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
// 獲取測試後的連線狀態
|
||||
const afterStats = await connectionMonitor.getConnectionStats();
|
||||
|
||||
console.log(`測試後連線數: ${afterStats.activeConnections}`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '連線測試完成',
|
||||
data: {
|
||||
before: {
|
||||
activeConnections: beforeStats.activeConnections,
|
||||
usagePercentage: beforeStats.usagePercentage
|
||||
},
|
||||
after: {
|
||||
activeConnections: afterStats.activeConnections,
|
||||
usagePercentage: afterStats.usagePercentage
|
||||
},
|
||||
difference: {
|
||||
connectionChange: afterStats.activeConnections - beforeStats.activeConnections,
|
||||
isReleased: afterStats.activeConnections <= beforeStats.activeConnections
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('連線測試失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '連線測試失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
103
app/api/upload/avatar/route.ts
Normal file
103
app/api/upload/avatar/route.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
// =====================================================
|
||||
// 個人頭像上傳 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { writeFile, mkdir } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { UserService } from '@/lib/services/database-service';
|
||||
import { generateUniqueFileName, isValidImageType, isValidImageSize } from '@/lib/image-utils';
|
||||
import sharp from 'sharp';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('avatar') as File;
|
||||
const userId = formData.get('userId') as string;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '請選擇要上傳的圖片' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '用戶ID不能為空' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 驗證文件類型
|
||||
if (!isValidImageType(file)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '只支援 JPEG、PNG、WebP 格式的圖片' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 驗證文件大小(限制 5MB)
|
||||
if (!isValidImageSize(file, 5)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '圖片大小不能超過 5MB' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 創建上傳目錄
|
||||
const uploadDir = join(process.cwd(), 'public', 'uploads', 'avatars');
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
|
||||
// 生成唯一文件名
|
||||
const fileName = generateUniqueFileName(file.name, userId);
|
||||
const filePath = join(uploadDir, fileName);
|
||||
|
||||
// 讀取文件緩衝區
|
||||
const bytes = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(bytes);
|
||||
|
||||
// 使用 Sharp 優化圖片
|
||||
const optimizedBuffer = await sharp(buffer)
|
||||
.resize(200, 200, {
|
||||
fit: 'cover',
|
||||
position: 'center'
|
||||
})
|
||||
.jpeg({ quality: 85 })
|
||||
.toBuffer();
|
||||
|
||||
// 保存優化後的圖片
|
||||
await writeFile(filePath, optimizedBuffer);
|
||||
|
||||
// 生成相對路徑(自動適應不同環境)
|
||||
const imageUrl = `/uploads/avatars/${fileName}`;
|
||||
|
||||
// 更新用戶頭像
|
||||
const userService = new UserService();
|
||||
await userService.update(userId, { avatar: imageUrl });
|
||||
|
||||
console.log(`✅ 用戶 ${userId} 頭像上傳成功: ${imageUrl}`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '頭像上傳成功',
|
||||
data: {
|
||||
imageUrl,
|
||||
fileName,
|
||||
fileSize: optimizedBuffer.length,
|
||||
originalSize: file.size
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('頭像上傳失敗:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: '頭像上傳失敗',
|
||||
details: error instanceof Error ? error.message : '未知錯誤'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user