85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { access, stat } from 'fs/promises';
|
|
import path from 'path';
|
|
import { createDownloadError, DownloadErrorCode } from '@/lib/download-errors';
|
|
|
|
/**
|
|
* 测试下载接口(绕过登录检查)
|
|
* 仅用于开发和测试,生产环境应禁用
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
// 检查是否是测试环境
|
|
if (process.env.NODE_ENV === 'production') {
|
|
return NextResponse.json(
|
|
{ error: '此接口仅用于测试' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const materialId = searchParams.get('id');
|
|
|
|
if (!materialId) {
|
|
return NextResponse.json(
|
|
createDownloadError(DownloadErrorCode.INVALID_DOWNLOAD_TYPE, '缺少素材ID'),
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const material = await prisma.material.findUnique({
|
|
where: { id: materialId },
|
|
select: {
|
|
id: true,
|
|
productModel: true,
|
|
zipFilePath: true,
|
|
materialType: true,
|
|
},
|
|
});
|
|
|
|
if (!material) {
|
|
return NextResponse.json(
|
|
createDownloadError(DownloadErrorCode.MATERIAL_NOT_FOUND),
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// 检查文件是否存在
|
|
if (!material.zipFilePath) {
|
|
console.log('[Test Download] No zipFilePath for material:', material.id);
|
|
return NextResponse.json(
|
|
createDownloadError(DownloadErrorCode.FILE_MISSING, '没有源文件'),
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const filePath = path.join(process.cwd(), material.zipFilePath);
|
|
|
|
try {
|
|
await access(filePath);
|
|
const fileStats = await stat(filePath);
|
|
|
|
return NextResponse.json({
|
|
message: '文件存在',
|
|
materialId: material.id,
|
|
productModel: material.productModel,
|
|
filePath: material.zipFilePath,
|
|
fileSize: fileStats.size,
|
|
});
|
|
} catch (error) {
|
|
console.error('[Test Download] File not found:', filePath, 'error:', error);
|
|
return NextResponse.json(
|
|
createDownloadError(DownloadErrorCode.FILE_MISSING, '没有源文件'),
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error('[Test Download] Error:', error);
|
|
return NextResponse.json(
|
|
{ error: '查询失败' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|