diff --git a/src/database.ts b/src/database.ts index 389dfdc..0de8f1e 100644 --- a/src/database.ts +++ b/src/database.ts @@ -115,15 +115,29 @@ export function getTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: str return File.find(filter); } -export function getTaskFile(bossAppID: string, taskID: string, name: string): Promise { +export function getTaskFile(bossAppID: string, taskID: string, name: string, country?: string, language?: string): Promise { verifyConnected(); - return File.findOne({ + const filter: mongoose.FilterQuery = { deleted: false, boss_app_id: bossAppID, task_id: taskID.slice(0, 7), name: name - }); + }; + + if (country) { + filter.supported_countries = { + $in: [country] + }; + } + + if (language) { + filter.supported_languages = { + $in: [language] + }; + } + + return File.findOne(filter); } export function getTaskFileByDataID(dataID: bigint): Promise { diff --git a/src/server.ts b/src/server.ts index ca3b85f..4ee8d1e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -15,6 +15,7 @@ import nppl from '@/services/nppl'; import npts from '@/services/npts'; import npdi from '@/services/npdi'; import npfl from '@/services/npfl'; +import npdl from '@/services/npdl'; const app = express(); @@ -32,6 +33,7 @@ app.use(nppl); app.use(npts); app.use(npdi); app.use(npfl); +app.use(npdl); LOG_INFO('Creating 404 status handler'); app.use((_request, response) => { diff --git a/src/services/npdl.ts b/src/services/npdl.ts new file mode 100644 index 0000000..eb4ab10 --- /dev/null +++ b/src/services/npdl.ts @@ -0,0 +1,46 @@ +import express from 'express'; +import subdomain from 'express-subdomain'; +import { getTaskFile } from '@/database'; +import { getCDNFileStream } from '@/util'; + +const npdl = express.Router(); + +npdl.get([ + '/p01/nsa/:appID/:taskID/:fileName', + '/p01/nsa/:appID/:taskID/:languageCode/:fileName', + '/p01/nsa/:appID/:taskID/:countryCode/:languageCode/:fileName' +], async (request: express.Request<{ + appID: string; + taskID: string; + countryCode?: string; + languageCode?: string; + fileName: string; +}, any, any, { + ap?: string; + tm?: string; +}>, response) => { + const { appID, taskID, countryCode, languageCode, fileName } = request.params; + + const file = await getTaskFile(appID, taskID, fileName, countryCode, languageCode); + + if (!file) { + response.sendStatus(404); + return; + } + + const key = `${appID}/${taskID}/${file.hash}`; + const readStream = await getCDNFileStream(key); + + if (!readStream) { + response.sendStatus(404); + return; + } + + readStream.pipe(response); +}); + +const router = express.Router(); + +router.use(subdomain('npdl.cdn', npdl)); + +export default router; \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index 012fe75..b2a56a2 100644 --- a/src/util.ts +++ b/src/util.ts @@ -102,10 +102,6 @@ export async function getUserDataByToken(token: string): Promise { - -} - export async function getCDNFileStream(key: string): Promise { try { if (disabledFeatures.s3) {