refactor: Update type annotations and error handling for improved type safety in route and download components
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { RowDataPacket, ResultSetHeader } from 'mysql2/promise';
|
||||
import { RowDataPacket } from 'mysql2/promise';
|
||||
|
||||
interface FileData extends RowDataPacket {
|
||||
token_hash: string;
|
||||
|
||||
@@ -7,19 +7,19 @@ interface FilePart extends RowDataPacket {
|
||||
}
|
||||
|
||||
interface RouteContext {
|
||||
params: {
|
||||
params: Promise<{
|
||||
file_id: string;
|
||||
part_index: string;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
|
||||
if (!process.env.DISCORD_BOT_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
|
||||
throw new Error('Discord bot token or channel ID is not configured');
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest, context: { params: Record<string, string | string[]> }) {
|
||||
export async function GET(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { file_id, part_index } = params;
|
||||
const { file_id, part_index } = await context.params;
|
||||
|
||||
// NOTE: In a real-world scenario, you MUST validate if the user has permission to download this part.
|
||||
// This would involve checking the download_token against the hash in the `files` table.
|
||||
|
||||
@@ -16,9 +16,15 @@ interface FilePartMetadata extends RowDataPacket {
|
||||
size: number;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: { file_id: string; } }) {
|
||||
interface RouteContext {
|
||||
params: Promise<{
|
||||
file_id: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const file_id = params.file_id;
|
||||
const { file_id } = await context.params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const token = searchParams.get('token');
|
||||
|
||||
|
||||
@@ -153,9 +153,8 @@ export default function DownloadPage() {
|
||||
|
||||
setDownloadState('complete');
|
||||
|
||||
} catch (e: any) {
|
||||
setError(e.message || 'An unknown error occurred.');
|
||||
setDownloadState('error');
|
||||
} catch (e: unknown) {
|
||||
setError(e instanceof Error ? e.message : 'An unknown error occurred.'); setDownloadState('error');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -186,9 +185,9 @@ export default function DownloadPage() {
|
||||
|
||||
setDeleteState('deleted');
|
||||
|
||||
} catch (e: any) {
|
||||
} catch (e: unknown) {
|
||||
setDeleteState('error');
|
||||
setDeleteError(e.message || 'An unknown error occurred during deletion.');
|
||||
setDeleteError(e instanceof Error ? e.message : 'An unknown error occurred during deletion.');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user