mirror of
https://github.com/skidoodle/erettsegi-browser.git
synced 2025-02-15 05:39:15 +01:00
pdf proxy
This commit is contained in:
parent
6c591adb09
commit
15c1caf41f
4 changed files with 61 additions and 7 deletions
1
pnpm-lock.yaml
generated
1
pnpm-lock.yaml
generated
|
@ -3705,6 +3705,7 @@ packages:
|
|||
|
||||
/glob@7.2.3:
|
||||
resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==}
|
||||
requiresBuild: true
|
||||
dependencies:
|
||||
fs.realpath: 1.0.0
|
||||
inflight: 1.0.6
|
||||
|
|
|
@ -9,7 +9,14 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
szint: string
|
||||
}
|
||||
|
||||
const secure = req.headers['x-forwarded-proto'] === 'https'
|
||||
const protocol = secure ? 'https' : 'http'
|
||||
const address = req.headers.host
|
||||
|
||||
const baseUrl = `https://dload-oktatas.educatio.hu/erettsegi/feladatok_${ev}${idoszak}_${szint}/`
|
||||
const proxiedUrl = `${protocol}://${address}/api/proxy?link=${encodeURIComponent(
|
||||
baseUrl
|
||||
)}`
|
||||
|
||||
const missingParams = []
|
||||
if (!ev) missingParams.push('ev')
|
||||
|
@ -68,13 +75,13 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
case 'infoism':
|
||||
case 'digkult':
|
||||
flZipUrl = `${baseUrl}${prefix}${forras}_${shortev}${honap}_${feladat}.zip`
|
||||
flPdfUrl = `${baseUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||
utZipUrl = `${baseUrl}${prefix}${megoldas}_${shortev}${honap}_${utmutato}.zip`
|
||||
utPdfUrl = `${baseUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||
break
|
||||
default:
|
||||
flPdfUrl = `${baseUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||
utPdfUrl = `${baseUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||
break
|
||||
}
|
||||
|
||||
|
|
43
src/pages/api/proxy.ts
Normal file
43
src/pages/api/proxy.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const { link } = req.query as { link: string }
|
||||
let missingParam: string | null = null
|
||||
if (!link) {
|
||||
missingParam = 'link'
|
||||
}
|
||||
|
||||
if (missingParam) {
|
||||
return res.status(400).json({ error: `Hiányzó paraméter: ${missingParam}` })
|
||||
}
|
||||
|
||||
const domain = link.split('/')[2]
|
||||
if (domain !== 'dload-oktatas.educatio.hu') {
|
||||
return res.status(400).json({ error: 'Érvénytelen link' })
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(link, { method: 'GET' })
|
||||
|
||||
if (response.headers.get('content-type') !== 'application/pdf') {
|
||||
return res.status(400).json({ error: 'Érvénytelen link' })
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
res.setHeader('Content-Type', 'application/pdf')
|
||||
const arrayBuffer: ArrayBuffer = await response.arrayBuffer()
|
||||
const pdfBuffer: Buffer = Buffer.from(arrayBuffer)
|
||||
res.send(pdfBuffer)
|
||||
} else {
|
||||
res
|
||||
.status(response.status)
|
||||
.json({ error: 'Hiba történt a lekérés során.' })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(500).json({ error: 'Internal Server Error' })
|
||||
}
|
||||
}
|
|
@ -14,8 +14,11 @@ export default async function handler(
|
|||
return res.status(400).json({ error: `Hiányzó paraméter: ${MissingParam}` })
|
||||
}
|
||||
|
||||
const domain = link.split('/')[2]
|
||||
if (domain !== 'dload-oktatas.educatio.hu') {
|
||||
const secure = req.headers['x-forwarded-proto'] === 'https'
|
||||
const protocol = secure ? 'https' : 'http'
|
||||
const address = req.headers.host
|
||||
|
||||
if (!link.startsWith(`${protocol}://${address}`)) {
|
||||
return res.status(400).json({ error: 'Érvénytelen link' })
|
||||
}
|
||||
|
||||
|
@ -24,6 +27,6 @@ export default async function handler(
|
|||
const status = response.status
|
||||
res.status(200).json({ status })
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Hiányzó paraméterek:' })
|
||||
res.status(500).json({ error: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue