mirror of
https://github.com/skidoodle/erettsegi-browser.git
synced 2025-02-15 05:39:15 +01:00
nagec
This commit is contained in:
parent
36a8fd8780
commit
64c6ab2041
3 changed files with 96 additions and 89 deletions
|
@ -2,89 +2,93 @@ import type { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { subjects } from '@/utils/subjects'
|
import { subjects } from '@/utils/subjects'
|
||||||
|
|
||||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { vizsgatargy, ev, idoszak, szint } = req.query as {
|
try {
|
||||||
vizsgatargy: string
|
const { vizsgatargy, ev, idoszak, szint } = req.query as {
|
||||||
ev: string
|
vizsgatargy: string
|
||||||
idoszak: string
|
ev: string
|
||||||
szint: string
|
idoszak: string
|
||||||
|
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=${encodeURI(
|
||||||
|
baseUrl
|
||||||
|
)}`
|
||||||
|
|
||||||
|
const missingParams = []
|
||||||
|
if (!ev) missingParams.push('ev')
|
||||||
|
if (!szint) missingParams.push('szint')
|
||||||
|
if (!idoszak) missingParams.push('idoszak')
|
||||||
|
if (!vizsgatargy) missingParams.push('vizsgatargy')
|
||||||
|
|
||||||
|
if (missingParams.length > 0) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ error: `Hiányzó paraméterek: ${missingParams.join(', ')}` })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ev <= '2012') {
|
||||||
|
return res.status(400).json({ error: 'Érvénytelen év' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const validSubjects = subjects.map((subject) => subject.value)
|
||||||
|
if (!vizsgatargy || !validSubjects.includes(vizsgatargy)) {
|
||||||
|
return res.status(400).json({ error: 'Érvénytelen vizsgatárgy' })
|
||||||
|
}
|
||||||
|
|
||||||
|
let honap: string
|
||||||
|
switch (idoszak) {
|
||||||
|
case 'osz':
|
||||||
|
honap = 'okt'
|
||||||
|
break
|
||||||
|
case 'tavasz':
|
||||||
|
honap = 'maj'
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return res.status(400).json({ error: 'Érvénytelen időszak' })
|
||||||
|
}
|
||||||
|
|
||||||
|
let prefix: string
|
||||||
|
switch (szint) {
|
||||||
|
case 'emelt':
|
||||||
|
prefix = `e_${vizsgatargy}`
|
||||||
|
break
|
||||||
|
case 'kozep':
|
||||||
|
prefix = `k_${vizsgatargy}`
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return res.status(400).json({ error: 'Érvénytelen szint' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const feladat = 'fl'
|
||||||
|
const utmutato = 'ut'
|
||||||
|
const forras = 'for'
|
||||||
|
const megoldas = 'meg'
|
||||||
|
const shortev = ev.slice(-2)
|
||||||
|
|
||||||
|
let flPdfUrl, utPdfUrl, flZipUrl, utZipUrl
|
||||||
|
switch (vizsgatargy) {
|
||||||
|
case 'inf':
|
||||||
|
case 'infoism':
|
||||||
|
case 'digkult':
|
||||||
|
flZipUrl = `${baseUrl}${prefix}${forras}_${shortev}${honap}_${feladat}.zip`
|
||||||
|
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||||
|
utZipUrl = `${baseUrl}${prefix}${megoldas}_${shortev}${honap}_${utmutato}.zip`
|
||||||
|
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
||||||
|
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
res.setHeader('Cache-Control', 's-maxage=31536000')
|
||||||
|
res.status(200).json({ flPdfUrl, utPdfUrl, flZipUrl, utZipUrl })
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Internal Server Error', message: error })
|
||||||
}
|
}
|
||||||
|
|
||||||
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=${encodeURI(
|
|
||||||
baseUrl
|
|
||||||
)}`
|
|
||||||
|
|
||||||
const missingParams = []
|
|
||||||
if (!ev) missingParams.push('ev')
|
|
||||||
if (!szint) missingParams.push('szint')
|
|
||||||
if (!idoszak) missingParams.push('idoszak')
|
|
||||||
if (!vizsgatargy) missingParams.push('vizsgatargy')
|
|
||||||
|
|
||||||
if (missingParams.length > 0) {
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ error: `Hiányzó paraméterek: ${missingParams.join(', ')}` })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ev <= '2012') {
|
|
||||||
return res.status(400).json({ error: 'Érvénytelen év' })
|
|
||||||
}
|
|
||||||
|
|
||||||
const validSubjects = subjects.map((subject) => subject.value)
|
|
||||||
if (!vizsgatargy || !validSubjects.includes(vizsgatargy)) {
|
|
||||||
return res.status(400).json({ error: 'Érvénytelen vizsgatárgy' })
|
|
||||||
}
|
|
||||||
|
|
||||||
let honap: string
|
|
||||||
switch (idoszak) {
|
|
||||||
case 'osz':
|
|
||||||
honap = 'okt'
|
|
||||||
break
|
|
||||||
case 'tavasz':
|
|
||||||
honap = 'maj'
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
return res.status(400).json({ error: 'Érvénytelen időszak' })
|
|
||||||
}
|
|
||||||
|
|
||||||
let prefix: string
|
|
||||||
switch (szint) {
|
|
||||||
case 'emelt':
|
|
||||||
prefix = `e_${vizsgatargy}`
|
|
||||||
break
|
|
||||||
case 'kozep':
|
|
||||||
prefix = `k_${vizsgatargy}`
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
return res.status(400).json({ error: 'Érvénytelen szint' })
|
|
||||||
}
|
|
||||||
|
|
||||||
const feladat = 'fl'
|
|
||||||
const utmutato = 'ut'
|
|
||||||
const forras = 'for'
|
|
||||||
const megoldas = 'meg'
|
|
||||||
const shortev = ev.slice(-2)
|
|
||||||
|
|
||||||
let flPdfUrl, utPdfUrl, flZipUrl, utZipUrl
|
|
||||||
switch (vizsgatargy) {
|
|
||||||
case 'inf':
|
|
||||||
case 'infoism':
|
|
||||||
case 'digkult':
|
|
||||||
flZipUrl = `${baseUrl}${prefix}${forras}_${shortev}${honap}_${feladat}.zip`
|
|
||||||
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
|
||||||
utZipUrl = `${baseUrl}${prefix}${megoldas}_${shortev}${honap}_${utmutato}.zip`
|
|
||||||
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
flPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${feladat}.pdf`
|
|
||||||
utPdfUrl = `${proxiedUrl}${prefix}_${shortev}${honap}_${utmutato}.pdf`
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
res.setHeader('Cache-Control', 's-maxage=31536000')
|
|
||||||
res.status(200).json({ flPdfUrl, utPdfUrl, flZipUrl, utZipUrl })
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,6 @@ export default async function handler(
|
||||||
.json({ error: 'Hiba történt a lekérés során.' })
|
.json({ error: 'Hiba történt a lekérés során.' })
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
res.status(500).json({ error: 'Internal Server Error', message: error })
|
||||||
res.status(500).json({ error: 'Internal Server Error' })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,11 @@ export default async function handler(
|
||||||
}
|
}
|
||||||
|
|
||||||
const domain = link.split('/')[2]
|
const domain = link.split('/')[2]
|
||||||
if (domain !== 'localhost:3000' && domain !== 'erettsegi.albert.lol') {
|
if (
|
||||||
|
domain !== 'localhost:3000' &&
|
||||||
|
domain !== 'erettsegi.albert.lol' &&
|
||||||
|
domain !== 'dload-oktatas.educatio.hu'
|
||||||
|
) {
|
||||||
return res.status(400).json({ error: 'Érvénytelen link' })
|
return res.status(400).json({ error: 'Érvénytelen link' })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +30,7 @@ export default async function handler(
|
||||||
return res.status(400).json({ error: 'Érvénytelen link' })
|
return res.status(400).json({ error: 'Érvénytelen link' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(link, { method: 'OPTIONS' })
|
const response = await fetch(link, { method: 'HEAD' })
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return res
|
return res
|
||||||
|
@ -37,6 +41,6 @@ export default async function handler(
|
||||||
const status = response.status
|
const status = response.status
|
||||||
res.status(200).json({ status })
|
res.status(200).json({ status })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: 'Internal Server Error' })
|
res.status(500).json({ error: 'Internal Server Error', message: error })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue