Some checks failed
Build and Push Docker Image / build (push) Failing after 10s
Full Next.js 15 platform with tRPC, Prisma, PostgreSQL, NextAuth. Includes production Dockerfile (multi-stage, port 7600), docker-compose with registry-based image pull, Gitea Actions CI workflow, nginx config for portal.monaco-opc.com, deployment scripts, and DEPLOYMENT.md guide. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
126 lines
3.1 KiB
TypeScript
126 lines
3.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { LocalStorageProvider } from '@/lib/storage/local-provider'
|
|
import { getContentType } from '@/lib/storage'
|
|
import * as fs from 'fs/promises'
|
|
|
|
const provider = new LocalStorageProvider()
|
|
|
|
/**
|
|
* Handle GET requests for file downloads
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = new URL(request.url)
|
|
const key = searchParams.get('key')
|
|
const action = searchParams.get('action')
|
|
const expires = searchParams.get('expires')
|
|
const sig = searchParams.get('sig')
|
|
|
|
// Validate required parameters
|
|
if (!key || !action || !expires || !sig) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required parameters' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Verify signature and expiry
|
|
const isValid = LocalStorageProvider.verifySignature(
|
|
key,
|
|
action,
|
|
parseInt(expires),
|
|
sig
|
|
)
|
|
|
|
if (!isValid) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid or expired signature' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
if (action !== 'download') {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid action for GET request' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
try {
|
|
const filePath = provider.getAbsoluteFilePath(key)
|
|
const data = await fs.readFile(filePath)
|
|
const contentType = getContentType(key)
|
|
|
|
return new NextResponse(data, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Cache-Control': 'private, max-age=3600',
|
|
},
|
|
})
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
return NextResponse.json({ error: 'File not found' }, { status: 404 })
|
|
}
|
|
console.error('Error serving file:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle PUT requests for file uploads
|
|
*/
|
|
export async function PUT(request: NextRequest) {
|
|
const { searchParams } = new URL(request.url)
|
|
const key = searchParams.get('key')
|
|
const action = searchParams.get('action')
|
|
const expires = searchParams.get('expires')
|
|
const sig = searchParams.get('sig')
|
|
|
|
// Validate required parameters
|
|
if (!key || !action || !expires || !sig) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required parameters' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Verify signature and expiry
|
|
const isValid = LocalStorageProvider.verifySignature(
|
|
key,
|
|
action,
|
|
parseInt(expires),
|
|
sig
|
|
)
|
|
|
|
if (!isValid) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid or expired signature' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
if (action !== 'upload') {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid action for PUT request' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
try {
|
|
const contentType = request.headers.get('content-type') || 'application/octet-stream'
|
|
const data = Buffer.from(await request.arrayBuffer())
|
|
|
|
await provider.putObject(key, data, contentType)
|
|
|
|
return NextResponse.json({ success: true }, { status: 200 })
|
|
} catch (error) {
|
|
console.error('Error uploading file:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|