Add animation keyframes for grid and shimmer effects, implement mouse position tracking for dynamic background, and create direct download API for video/audio files. Update types for download requests and responses.

This commit is contained in:
2025-12-21 21:18:08 -06:00
parent ce199be67b
commit 4fa44751f7
6 changed files with 200 additions and 3 deletions

View File

@@ -0,0 +1,82 @@
import { NextRequest, NextResponse } from 'next/server';
import { downloadVideo } from '@/lib/youtube';
import type { DownloadRequest, AudioFormat, VideoFormat, FormatType } from '@/lib/types';
import { createReadStream } from 'fs';
import { stat } from 'fs/promises';
import path from 'path';
import { Readable } from 'stream';
const VIDEO_MIME: Record<VideoFormat, string> = {
mp4: 'video/mp4',
webm: 'video/webm',
mkv: 'video/x-matroska',
avi: 'video/x-msvideo',
};
const AUDIO_MIME: Record<AudioFormat, string> = {
mp3: 'audio/mpeg',
wav: 'audio/wav',
m4a: 'audio/mp4',
opus: 'audio/ogg',
};
function getMimeType(formatType: FormatType, format: VideoFormat | AudioFormat): string {
if (formatType === 'video') {
return VIDEO_MIME[format as VideoFormat] || 'application/octet-stream';
}
return AUDIO_MIME[format as AudioFormat] || 'application/octet-stream';
}
async function resolveDownloadPath(downloadUrl: string) {
const sanitized = downloadUrl.startsWith('/') ? downloadUrl.slice(1) : downloadUrl;
const filePath = path.join(process.cwd(), 'public', sanitized);
const fileInfo = await stat(filePath);
return { filePath, size: fileInfo.size };
}
export async function POST(request: NextRequest) {
try {
const body: DownloadRequest = await request.json();
const { url, format, formatType } = body;
if (!url || !format || !formatType) {
return NextResponse.json(
{ success: false, error: 'Missing required fields' },
{ status: 400 }
);
}
if (!isValidYouTubeUrl(url)) {
return NextResponse.json(
{ success: false, error: 'Invalid YouTube URL' },
{ status: 400 }
);
}
const result = await downloadVideo(body);
if (!result.success || !result.downloadUrl || !result.filename) {
return NextResponse.json(
{ success: false, error: result.error || 'Failed to process download' },
{ status: 500 }
);
}
const { filePath, size } = await resolveDownloadPath(result.downloadUrl);
const nodeStream = createReadStream(filePath);
const webStream = Readable.toWeb(nodeStream) as unknown as ReadableStream;
const headers = new Headers();
headers.set('Content-Type', getMimeType(formatType, format));
headers.set('Content-Length', size.toString());
headers.set('Content-Disposition', `attachment; filename="${result.filename}"`);
return new NextResponse(webStream, { status: 200, headers });
} catch (error: any) {
console.error('Direct download error:', error);
return NextResponse.json(
{ success: false, error: error.message || 'Internal server error' },
{ status: 500 }
);
}
}